Rstudio预览绘图可以显示中文,但输出为pdf中经常无法识别,如下图的x轴标签:
经验证,可通过添加export包实现中文字体图形输出,比较方便。
#安装export包
devtools::install_github("tomwenseleers/export")
library(ggplot2)
#生成ggplot图
p1<-ggplot(iris)+
geom_point(aes(x=Sepal.Length,y=Sepal.Width))+
labs(x="中国")
#基本图形
p2<-boxplot(iris$Sepal.Length,xlab="英国")
#输出ggplot2图
export::graph2pdf(x=p1,file="TEST1.pdf",font="STXihei")
#输出基础统计图
export::graph2pdf(file="TEST2.pdf",font="simsun") #基础作图函数不能传递对象,因此不需要p2。