r - 使用 ggplot geom_text 的 wordlayout 结果

标签 r ggplot2 size word-cloud

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

最佳答案

cex代表字符扩展,是文本相对于默认值的放大系数,由 cin 指定。 - 在我的安装中设置为 0.15 英寸乘 0.2 英寸:见 ?par更多细节。

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

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

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

关于r - 使用 ggplot geom_text 的 wordlayout 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21135882/

相关文章:

r - 如何根据另一列条件删除重复行?

r - 如何计算R中具有负坐标和正坐标的点之间的距离

r - 为什么功能序列不适用于 ggplot2?

swift - 在 Swift 中根据 HTTP 图像响应调整 UIImageView 的大小

android - 如果我将所有图像都留在 drawable-hdpi 文件夹中会怎样?

file-upload - 如何为 Alfresco 4.2.c 中上传的文件设置大小限制?

r - 在R中将两个数据框制作成相同的形式

javascript - Highcharter plot 仅在第二次点击后更新 - R Shiny

r - 如何将刻度标签设置为连续 ggplot2 图例的边缘

r - 调整 facet_grid 中 One Line 的线型