1. Solve the problem that the Chinese font of the picture saved by R/ggplot2 is lost to PDF.
2. Solve the problem of failed import of Times New Roman and simsun fonts.
3. Be familiar with and master showtext operation.
1. Problem description
After drawing with ggplot2, use ggsave to save the picture. The Chinese font is lost. After self inspection and network search, it is determined that it has nothing to do with the saving method, mainly due to the font setting. After inquiry, there are mainly two packages to solve the font, one is extrafont and the other is showtext. The latter is recommended and used more, so bloggers choose the latter.
PS: there is no difference between the two. I choose to trust most people.
2. Full notes of showtext package usage process
2.1 case introduction - importing Google fonts online
There are cases in the original introduction of Showtext package, but the friendliness to newcomers is not high enough. This paper makes comprehensive comments and example operations. It also solves the problem that the Song typeface and New Roman typeface cannot be read.
Introduction of showtext package author
PS: I spent half an hour with my brain tired last night, but I didn't solve the problem of showtext bag. I got up this morning and finished everything quickly~
library(tidyverse) #For the convenience of later drawing, in fact, only importing ggplot2 can also complete the operation of this example library(showtext) #Import font settings package # font_add_google showtext is a function that downloads and imports fonts from Google fonts. # The font name in name is used for retrieval. It must strictly correspond to the name of the desired font. # family is followed by the name referenced after the code. You can choose it yourself. font_add_google(name = "Gochi Hand",family = "gochi") font_add_google(name = "Schoolbell",family = "bell") showtext_auto() # The imported font can be used for the following fonts # Draw a randomly distributed histogram, just pay attention to the font setting part # That is, family = "". You can change the font test by yourself. Pay attention to using the imported font. hist(rnorm(1000),breaks = 30,col = "steelblue",border = "white", main = "",xlab = "",ylab = "") title("Historgram of NRN",family = "bell",cex.main=2) title(ylab="Frequency",family="gochi",cex.lab=2) text(2,70,"N=1000 ffff",family="bell",cex = 2.5)
The code of this part is basically consistent with the author link. But I still have to test it myself before I can say it can be used (because I failed to import Song typeface and new Rome yesterday)
2.2 example test - Import local fonts, taking Song typeface and New Roman as examples
As described in the description, the import package method is as follows:
font_add(family = "<family_name>", regular = "/path/to/font/file")
family refers to the name of the code behind you, and you can define it yourself. The path + name of the package needs to be specified in the regular parameter. The default path is C:\Windows\Fonts.
You can use font_paths() view the current path or add a new path.
font_files() lists all available font files in the current path.
Font query has no New Roman, but Song typeface - "simsun.ttc" ttc means font installation set, including regular, bold, etc ttf is a single font. If the suffix is misspelled, it will fail to import the font.
Landlord iron, can't find the new Rome, just want to import, the error is shown in the figure. So I went to the file to see the New Roman font path.
But right clicking has no properties. Double click to enter the file
Select the font you want to import, right-click the attribute and copy the name. I selected general, and the code is as follows.
The results are as follows:
Here you can save the PDF directly with ggsave
2.3 expansion - differences between new Rome and Serif
See here for details
The landlord sees the showtext package and uses font_families() can call serif fonts directly. So we compared the code as follows:
font_families() ggplot(NULL,aes(x=1,y=1))+ ylim(0.5,1.0)+ theme_bw()+ annotate("text",x=0.7,y=0.7,family="serif",size=15, label="Hello,serif world")+ annotate("text",x=0.7,y=0.9,family="TNM",size=15, label="Hello,TNM world")+ annotate("text",x=0.7,y=0.8,family="songti",size=15, label="Hello,Song style") ggsave("Font test 1.pdf",width = 7,height = 4,dpi=96)
The result is shown in the figure below, with New Roman at the top, Song Dynasty in the middle and serif at the bottom. It can be seen that there are some differences between new Rome and serif.