sup*_*mbo 6 size r ggplot2 word-cloud
R包wordcloud有一个非常有用的功能,叫做wordlayout.它采用单词的初始位置及其各自的大小,以不重叠的方式重新排列它们.我想用这个函数的结果在ggplot中做一个geom_text图.我提出了以下示例,但很快就意识到,在cex(wordlayout)和size(geom_plot)之间似乎存在很大差异,因为图形包中的单词看起来更大.这是我的示例代码.Plot 1是原始的wordcloud图,没有重叠:
library(wordcloud)
library(tm)
library(ggplot2)
samplesize=100
textdf <- data.frame(label=sample(stopwords("en"),samplesize,replace=TRUE),x=sample(c(1:1000),samplesize,replace=TRUE),y=sample(c(1:1000),samplesize,replace=TRUE),size=sample(c(1:5),samplesize,replace=TRUE))
#plot1
plot.new()
pdf(file="plot1.pdf")
textplot(textdf$x,textdf$y,textdf$label,textdf$size)
dev.off()
#plot2
ggplot(textdf,aes(x,y))+geom_text(aes(label = label, size = size))
ggsave("plot2.pdf")
#plot3
new_pos <- wordlayout(x=textdf$x,y=textdf$y,words=textdf$label,cex=textdf$size)
textdf$x <- new_pos[,1]
textdf$y <- new_pos[,2]
ggplot(textdf,aes(x,y))+geom_text(aes(label = label, size = size))
ggsave("plot3.pdf")
#plot4
textdf$x <- new_pos[,1]+0.5*new_pos[,3]#this is the way the wordcloud package rearranges the positions. I took this out of the textplot function
textdf$y <- new_pos[,2]+0.5*new_pos[,4]
ggplot(textdf,aes(x,y))+geom_text(aes(label = label, size = size))
ggsave("plot4.pdf")
Run Code Online (Sandbox Code Playgroud)
有没有办法克服这个cex /大小差异,并重新使用wordlayout为ggplots?
cex代表字符扩展,是文本相对于默认值放大的系数,由cin- 在我的安装中设置为 0.15 英寸 x 0.2 英寸来指定:请参阅 获取?par更多详细信息。
@hadley 解释说 ggplot2size的单位是毫米。因此cex=1将对应于size=3.81或size=5.08取决于它是否按宽度或高度缩放。当然,字体选择可能会造成差异。
此外,要使用绝对尺寸,您需要在 之外具有尺寸规范,aes否则它会将其视为要映射到并选择比例本身的变量,例如:
ggplot(textdf,aes(x,y))+geom_text(aes(label = label),size = textdf$size*3.81)
Run Code Online (Sandbox Code Playgroud)