R 绘图出现中文乱码

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

以上来自知乎王雨晨: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()
}

你可能感兴趣的:(R 绘图出现中文乱码)