【问题标题】:Use wordlayout results for ggplot geom_text使用 ggplot geom_text 的 wordlayout 结果
【发布时间】:2014-01-15 11:12:35
【问题描述】:

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?

【问题讨论】:

    标签: r ggplot2 size word-cloud


    【解决方案1】:

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

    @hadley explains ggplot2 sizes 以毫米为单位。因此cex=1 将对应于size=3.81size=5.08,具体取决于它是按宽度还是高度缩放。当然,字体选择可能会造成差异。

    此外,要使用绝对尺寸,您需要在aes 之外指定尺寸,否则它会将其视为要映射到的变量并自行选择比例,例如:

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

    【讨论】:

    • 谢谢。我刚刚进行了您建议的更改(我的 par()$cin 默认值与您相同)。现在这些单词似乎大小相同,但我的 ggplot 中的单词完全重叠。可见文本图的单词较少。见plot1plot3plot4
    • 实际上看起来可能是textplot使用了中心位置,而ggplot可能使用了文本左边缘的位置?
    • 我想我应该根据您在第一条评论中给我的信息编辑 wordlayout 函数中的第 23 到 24 行。在这些线上,每个单词的宽度和高度是根据 cex 值使用 strwidth 和 strheight 计算得出的。
    【解决方案2】:

    遗憾的是,我认为您会发现简短的答案是否定的!我认为该包处理文本矢量映射的方式与 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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-23
      • 1970-01-01
      • 2015-05-18
      相关资源
      最近更新 更多