R語言適配問題集錦
阿新 • • 發佈:2017-06-20
red dom enc ava unit -a true ngs 編碼
畫圖時的中文亂碼問題
我這是Mac Yousemite 10.10.5,在兩個地方遇到了中文亂碼
1、使用wordcloud包繪制中文標簽雲時。
library(wordcloud)
mydata <- read.csv(‘word-count.txt‘,header=T,stringsAsFactors=F)
jpeg(filename=‘wordcloud.jpg‘, width=800,height=800,units=‘px‘)
mycolor <- colorRampPalette(c("gray", "red"))(200)
par(family=‘STKaiti‘ )#解決方式是加上這一句。關於詳細的字體名稱,能夠通過這個命令查看:fc-list :lang=zh-cn
wordcloud(mydata$name,mydata$count,c(6,0.8),random.order=FALSE,color=mycolor)
dev.off()
2、使用igraph繪制社交關系圖時
library(igraph)
#讀取數據,註意編碼格式是utf-8
singer <- read.csv(‘singers.txt‘, head=T,fileEncoding=‘UTF-8‘,encoding=‘UTF-8‘,stringsAsFactors=F)
#載入數據框
#g <- graph_from_data_frame(singer,directed=TRUE)
g <- graph.data.frame(singer,directed=TRUE)
#生成圖片,大小是800*800px
jpeg(filename=‘singers.jpg‘,width=800,height=800,units=‘px‘)
par(family=‘STKaiti‘)#這裏設置main屬性的中文字體。以免其出現中文亂碼。
plot(g,
vertex.size=5, #節點大小
layout=layout.fruchterman.reingold, #布局方式
vertex.shape=‘none‘, #不帶邊框
vertex.label.cex=1.5, #節點字體大小
vertex.label.color=‘red‘, #節點字體顏色
vertex.label.font=4,
main=‘歌星關系圖譜‘,
vertex.label.family=‘STKaiti‘,#這裏設置每一個vertex節點的中文字體,以免其出現中文亂碼。
edge.arrow.size=0.7) #連線的箭頭的大小
#關閉圖形設備。將緩沖區中的數據寫入文件
dev.off()
3、讀取本地文件時,本地文件務必使用UTF-8存儲。
由於R內部的默認編碼就是UTF-8
R語言適配問題集錦