Use wordlayout results for ggplot geom_text

Wordcloud R has a very useful feature called wordlayout. He takes the initial positions of words and their corresponding sizes, rearranges them so that they do not overlap. I would like to use the results of these functions to create a geom_text graph in ggplot. I came up with the following example, but soon realized that there seems to be a big difference between cex (wordlayout) and size (geom_plot), since the words in the graphics package look much larger. here is my sample code. Section 1 is the original wordcloud that has no overlap:

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

Is there a way to overcome this size / size difference and reuse wordlayout for ggplots?

+6
source share
2 answers

cex means character expansion and is a factor by which the text is incremented by default specified by cin - is set at my installation by 0.15 inches by 0.2 inches: see ?par for more details.

@hasley explains that ggplot2 size is measured in mm. Therefore, cex=1 corresponds to size=3.81 or size=5.08 , depending on whether the width or height is scaled. Of course, font selection can cause differences.

In addition, in order to use absolute sizes, you need to have a size specification outside of aes , otherwise it considers this to be a variable for displaying and selecting the scale itself, for example:

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

Unfortunately, I think you will find a short answer: no! I think the package handles text vector matching differently than ggplot2, so you can mess with the size and font of face / family, etc., but will try to accurately reproduce what the package does.

I have tried several things:

1) Try drawing grobs from textdata using 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) Run the wordlayout () function, which should correct the text, but it’s hard to see for which font (it doesn’t work in the same way)

 # 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

Here's a trickster if you just want to overlap other ggplot functions on top of it (although the coordinates don't exactly match between the data and the plot). It basically displays the word cloud, removes the fields and brings it to the same scale:

 # 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

+4
source

All Articles