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()
}
画图的时候字体是需要设置的,因为默认的是英文字体。可以任意改你喜欢的中文字体来美化图片。
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()
}
相关文章推荐
- 向大家推荐一个收集整理正则表达式的网站
- 最严谨的校验email地址的正则表达式及各种语言对应版
- PHP中的print_r 与 var_dump 输出数组
- JAVA中正则表达式匹配,替换,查找,切割的方法
- iOS中使用正则表达式NSRegularExpression 来验证textfiled输入的内容
- 处理Plot rendering error
- R语言书籍的学习路线图
- R语言学习笔记 三
- R语言学习笔记 四
- R语言学习笔记 五
- 验证码识别技术 一
- PHP中的print_r 与 var_dump 输出数组
- 20120919_01
- 20120919_02
- 20120919_3
- Review JDBC
- Win2003SN
- 解决rJava无法找到jvm.dll的问题
- System Center 2012
- How to configure a RPC Client Access