您的位置:首页 > 其它

R 绘图出现中文乱码

2016-06-28 10:55 1036 查看
在使用RStudio画图时,workspace browser内存中显示正常,但是画图时出现中文乱码,网上找了一下问题原因,得到:

画图的时候字体是需要设置的,因为默认的是英文字体。可以任意改你喜欢的中文字体来美化图片。

OS X 下,用的是 STXihei,Ubuntu 你自己找到文泉驿之类的字体的 family name 改进去就行了。

两种改法,全局的和局部的:

par(family='STXihei')

plot(d, family='STXihei')

导出是用 RStudio 的菜单如果不行的话就只能写代码导出试试了。


以上来自知乎王雨晨:https://www.zhihu.com/question/21576848/answer/19339398

以画词云为例,代码如下:

sample.cloud <- function(cluster, maxwords = 100) {

  words <- sample.words[which(csv$type==cluster)]

  allwords <- unlist(words)

  wordsfreq <- sort(table(allwords), decreasing = T)

  wordsname <- names(wordsfreq)

  png(paste("sample_", cluster, ".png", sep = ""), width = 600, height = 600 )

  wordcloud(wordsname, wordsfreq, scale = c(6, 1.5), min.freq = 2, max.words = maxwords,

            colors = rainbow(100),family='STXihei')

  title(main = paste("cluster:", cluster))

  dev.off()

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息