系统mac os 11
Rstutio Version 1.2.1335
R Version 3.6.2
画图里的中文一直显示成方框。
ggplot()+geom_point(aes(x=ta1$X2011年,y=ta1$X2012))+
xlab("2011年")+ylab("2021年")
解决方案:
使用showtext。
install.packages("showtext", repos = "https://mirrors.ustc.edu.cn/CRAN/")
install.packages("sysfonts", repos = "https://mirrors.ustc.edu.cn/CRAN/")
加载showtext包过程中遇到错误:
‘sysfonts’ Reason: image not found
解决方案:
重新安装XQuartz即可。https://dl.bintray.com/xquartz/downloads/XQuartz-2.7.11.dmg
library(showtext)
library(ggplot2)
font_files() # 查看字体
#ggplot
ggplot()+geom_point(aes(x=ta1$X2011年,y=ta1$X2012))+
labs(x="2011年",y="2012年",title="ggplot图")+
theme(text=element_text(family="ArialUnicodeMS",size=14))
#普通图,如 plot
plot(x=ta1$X2011年,y=ta1$X2012,family='ArialUnicodeMS',
xlab="2011年",ylab="2012年",main="普通图plot、barplot")
#换成pheatmap图又没办法显示了。。解决方案:用showtext,保存到png、pdf
png("~/Documents/hehe.png")
showtext_begin()
pheatmap(ta1,display_numbers = F,cluster_rows = T,cluster_cols = FALSE,
show_rownames = TRUE,cellwidth=20,family="ArialUnicodeMS",
angle_col=90,color = colorRampPalette(c('white','blue'))(50)) #乱七八糟画图code
showtext_end()
dev.off()
参考:
https://www.jianshu.com/p/f9a2f9b04c59?from=singlemessage