6

R 包 wordcloud 有一个非常有用的功能,称为 wordlayout。它采用单词的初始位置和它们各自的大小,并以它们不重叠的方式重新排列它们。我想用这个函数的结果在 ggplot 中做一个 geom_text 绘图。我想出了以下示例,但很快意识到 cex (wordlayout) 和 size (geom_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")

有没有办法克服这种 cex/size 差异并为 ggplots 重用 wordlayout?

4

2 回答 2

4

cex代表字符扩展,是文本相对于默认值放大的因子,由cin- 在我的安装中设置为 0.15 英寸 x 0.2 英寸指定:?par有关更多详细信息,请参阅。

@hadley 解释说 ggplot2size以毫米为单位。因此cex=1将对应于size=3.81size=5.08取决于它是否按宽度或高度进行缩放。当然,字体选择可能会造成差异。

此外,要使用绝对大小,您需要将大小规范放在之外,aes否则它会将其视为要映射到的变量并选择比例本身,例如:

ggplot(textdf,aes(x,y))+geom_text(aes(label = label),size = textdf$size*3.81)
于 2014-01-15T11:47:48.153 回答
4

可悲的是,我认为您会发现简短的答案是否定的!我认为该包处理文本向量映射的方式与 ggplot2 不同,因此您可以修改大小和字体/系列等,但很难准确复制该包的功能。

我尝试了几件事:

1) 尝试使用 annotation_custom 从 textdata 中绘制 grobs

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)  
})  

在此处输入图像描述

2) 运行 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)  
  })  

在此处输入图像描述

如果您只想在其上过度绘制其他 ggplot 函数,这是一个作弊(尽管数据和绘图之间的坐标似乎并不完全匹配)。它基本上对 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))

在此处输入图像描述

于 2014-01-15T13:27:48.747 回答