:R language | showtext-font setting-Appreciation of masterpieces免费ppt模版下载-道格办公

R language | showtext-font setting

t('Hello, World!', family = 'Arial')

One thought


Have you ever thought about changing the font directly in R when plotting? Instead of manually modifying it in AI or PS after drawing?

A test


Execute the following code in R:

  1. plot.new( )

  2. text(0.5, 1, 'test', cex=3)

  3. text(0.5, 0.9, 'test', cex=3, family='Courier New' )

  4. text(0.5, 0.8, 'test', cex=3, family='Courier Old' )

  5. text(0.5, 0.7, 'Chinese', cex=3)

  6. text(0.5, 0.6, 'test', cex=3, family='Chinese Xingkai' )

Visible, except that the artificially non-existing font Courier Old warns (and draws with the last actually used font):

Warning messages:1: In text.default(0.5, 0.5, 'test', cex = 3, family = 'Courier Old') : font family 'Courier Old' has no font

Chinese is not displayed normally, and the Chinese Xingkai font cannot be found:

Warning messages:1: In text.default(0.5, 0.6, 'test', cex = 3, family = 'Hua Wen Xing Kai') : font family 'Hua Wen Xing Kai' has no font

The above picture can be usedpdf() Is the function saved in the pdf file as it is?

Actually, only the first test can be saved normally (because there is no default font, and the default font Helvetica is used), other errors are reported one after another:

Error in text.default(0.5, 0.9, 'test', cex = 3, family = 'Courier New') :

Error in typeface

In addition: Warning messages:1: In text.default(0.5, 0.9, 'test', cex = 3, family = 'Courier New') : 'Courier New' cannot be found in the PostScript font database font family

  1. names(pdfFonts()) # Available fonts for pdf graphics devices

https://stackoverflow.com/questions/50431809/extra-fonts-in-pdffonts-in-r

Therefore, there are three main problems:

1. Chinese cannot be displayed

2. Chinese fonts cannot be displayed (limited fonts available)

3. An error occurred when saving pdf files or the font was converted to the default font

showtext

showtext package can perfectly solve the above problems. The principle of its implementation is simply to convert text into graphics, and then use the system's default graphics device (pdf, png, jpeg, tif, etc.) to draw graphics.

Although the showtext package can use richer fonts, it is limited to the existing fonts in the system, so you can check whether the font exists before using it:

  1. library(showtext)

  2. font_paths() # Search path for fonts in the system

  3. font_files() # Existing available fonts

Use:
  1. showtext_auto(enable = TRUE) # Open showtext

  2. # Reopen a drawing interface for testing

  3. plot(NULL, type='n', axes=FALSE, ann=FALSE, xlim=c(0,1), ylim=c(0 ,1))

  4. text(0.4, 0.9, 'test', cex=3)

  5. text(0.4, 0.8, 'Chinese', cex=3)

At this point, both Chinese and English can be displayed normally! If you want to further set the font, you need to use the font_add function to load the font into R Middle:

  1. font_add('丽黑', '丽黑Pro.ttf') # From the font Import 'Lihei Pro.ttf' in the library, and rename it to 'Lihei'

  2. text(0.3, 0.7, 'Chinese', cex=3, family='Li Hei' ) # Set font

  3. font_add('Times New Roman', 'Times New Roman.ttf')

  4. text(0.5, 0.7, 'test', cex=3, family='Times New Roman ')

For libraryfont.paths()Fonts that do not exist, such as Chinese Xingkai, can be downloaded online (usually in .ttf/.ttc/.otf format)

http://www.fonts.net.cn/font-search-result-984339101-1.html, and store it infont.paths()In the folder, then use it:

  1. font_add('Chinese Xingkai', 'HuaWenXingKai-1.ttf')

  2. text(0.4, 0.6, 'Chinese', cex=3, family='Chinese Xingkai' )

Of course, if you want to change the font of multiple texts as a whole, you don’t need to use each text set separately in the function, directly in parSet it in:

  1. font_add('TNR', 'Times New Roman.ttf')

  2. par(family = 'TNR')

  3. text(0.3, 0.5, 'test', col = 'darkblue', cex=3< /span>)

  4. text(0.5, 0.5, 'test', cex=3)

What about setting different display effects for a line of text?

For example, for text log-rank p = 0.01, set it to Courier New font here, and put

p = 0.01 set to bold italics:

  1. font_add('CN', 'Courier New.ttf')

  2. font_add('CNBI', 'Courier New Bold Italic.ttf')

  3. text(0.42, 0.3, 'log-rank', cex=1, family = 'CN ')

  4. text(0.58, 0.3, 'p = 0.01', cex=1, family = 'CNBI ')

Since the bottom layers of different fonts are all converted into basic graphics. Then some special symbols or icons may also be drawn as text!

https://www.wfonts.com, http://www.fontspace.com

  1. font_add('PF', 'Packaging Funny.otf')

  2. font_add('HFS', 'Hand Faces St.ttf')

  3. font_add('GATB', 'go around the books - arrows.ttf')

  4. text(0.7, 0.6, 'k', cex=2, family = 'GATB'< /span>)

  5. text(0.8, 0.48, 'WeChat public account: Shengxinkong', cex=1.6, family='Chinese Xingkai')

  6. text(0.8, 0.4, 'SXK', cex=2, family = 'PF'< /span>)

  7. text(0.9, 0.4, 'p', cex=2, col = 'darkslateblue'< /span>, family = 'HFS')

Then, we put the above code in pdf( 'ceshi')anddev.off() The drawing results can be perfectly saved in the pdf file:

Articles are uploaded by users and are for non-commercial browsing only. Posted by: Lomu, please indicate the source: https://www.daogebangong.com/en/articles/detail/R%20language%20%20showtextfont%20setting.html

Like (810)
Reward 支付宝扫一扫 支付宝扫一扫
single-end

Related Suggestion