【R】R画图不显示中文问题

系统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

https://github.com/yixuan/showtext/issues/13

https://www.runoob.com/r/r-charts-cn.html

  • 9
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值