R包wordcloud有一个非常有用的函数,称为wordlayout。它需要初始位置的单词和它们各自的大小,重新排列它们,使它们不重叠。我想用这个函数的结果在ggplot中做一个geom_text plot。 我想出了下面的例子,但很快就意识到,由于图形包中的单词显得更大,因此在cex(wordlayout)和size(geom_plot)之间似乎存在很大差异。 这里是我的示例代码。 1地块是原wordcloud曲线图,有没有重叠:对ggplot使用wordlayout结果geom_text
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")
是有办法解决这个CEX /大小差异和重用wordlayout为ggplots?
谢谢。我只是做了你所建议的改变(我已经得到了和你一样的par()$ cin默认值)。现在这些单词似乎有相同的大小,但我的ggplot中的单词完全重叠。有单词可见的文字图形较少。参见[plot1](http://homepage.univie.ac.at/stephan.schloegl/stuff/plot1.pdf)[plot3](http://homepage.univie.ac.at/stephan.schloegl/stuff/plot3 .pdf)[plot4](http://homepage.univie.ac.at/stephan.schloegl/stuff/plot4。pdf) – supersambo
也许位置也是基于不同的单位 – James
实际上,它看起来可能是textplot使用中心的位置,而ggplot可能会使用文本左边的位置? – James