2014-01-15 43 views
6

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?

回答

4

cex即字符扩展,是由文字被放大相对默认,cin指定的因素 - 0.2在我安装0.15设置:见?par了解更多详情。

@hadley explains ggplot2 size s以mm为单位进行测量。因此,cex=1将对应于size=3.81size=5.08,具体取决于是否按宽度或高度缩放。当然,字体选择可能会导致差异。

此外,使用绝对尺寸,你需要有aes否则就认为这是一个变量映射到和选择本身的规模,例如,超出尺寸规格:

ggplot(textdf,aes(x,y))+geom_text(aes(label = label),size = textdf$size*3.81) 
+0

谢谢。我只是做了你所建议的改变(我已经得到了和你一样的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

+0

也许位置也是基于不同的单位 – James

+0

实际上,它看起来可能是textplot使用中心的位置,而ggplot可能会使用文本左边的位置? – James

4

可悲的是我想你'要找到简短的答案是不!我认为这个包处理文本向量映射与ggplot2不同,所以你可以修改大小和字体面/家族等,但是很难精确地复制包所做的事情。

我试了几件事情:

1)尝试使用annotation_custom

require(plyr) 
require(grid) 

# FIRST TRY PLOT INDIVIDUAL TEXT GROBS 
qplot(0:1000,0:1000,geom="blank") + 
    alply(textdf,1,function(x){ 
    annotation_custom(textGrob(label=x$label,0,0,c("center","center"),gp=gpar(cex=x$size)),x$x,x$x,x$y,x$y) 
}) 

enter image description here

2绘制从的TextData的grobs)运行,应重新调整文本wordlayout()函数,但很难看出什么字体(类似地不起作用)

# THEN USE wordcloud() TO GET CO-ORDS 
plot.new() 
wordlayout(textdf$x,textdf$y,words=textdf$label,cex=textdf$size,xlim=c(min(textdf$x),max(textdf$x)),ylim=c(min(textdf$y),max(textdf$y))) 
plotdata<-cbind(data.frame(rownames(w)),w) 
colnames(plotdata)=c("word","x","y","w","h") 

# PLOT WORDCLOUD DATA 
qplot(0:1000,0:1000,geom="blank") + 
    alply(plotdata,1,function(x){ 
    annotation_custom(textGrob(label=x$word,0,0,c("center","center"),gp=gpar(cex=x$h*40)),x$x,x$x,x$y,x$y) 
    }) 

enter image description here

这里是个骗子,如果你只是想overplot在它上面的其他ggplot功能(虽然共同ORDS似乎没有数据和剧情之间并不完全一致)。它基本上图像wordcloud,删除页边距,下图是在相同的规模:

# make a png file of just the panel 
plot.new() 
png(filename="bgplot.png") 
par(mar=c(0.01,0.01,0.01,0.01)) 
textplot(textdf$x,textdf$y,textdf$label,textdf$size,xaxt="n",yaxt="n",xlab="",ylab="",asp=1) 
dev.off() 

# library to get PNG file 
require(png) 

# then plot it behind the panel 
qplot(0:1000,0:1000,geom="blank") + 
    annotation_custom(rasterGrob(readPNG("bgplot.png"),0,0,1,1,just=c("left","bottom")),0,1000,0,1000) + 
    coord_fixed(1,c(0,1000),c(0,1000)) 

enter image description here