Put ggplot text in every corner

I have a scatter plot with horizontal and vertical lines that show threshold values โ€‹โ€‹and therefore they divide the plot into four quadrants. I would call quadrants. I think the best way to do this is with a number in each of the four corners of the graph (alternative suggestions are welcome!).

I managed to put the text in the corner of each quadrant, but the positions are not perfect. I suppose the problem is that the scaling of the axes is different (the range of values โ€‹โ€‹is about the same, but the width of my figure is about three times the height).

I am currently doing the following path. First, I create a graph with dots and two lines, then I build it to get the range of two axes, which I use to adjust the position of the texts.

plot.build = ggplot_build(plot) xpos = numeric(4) xpos[2] = xpos[3] = plot.build$panel$ranges[[1]]$x.range[1] xpos[1] = xpos[4] = plot.build$panel$ranges[[1]]$x.range[2] ypos = numeric(4) ypos[1] = ypos[2] = plot.build$panel$ranges[[1]]$y.range[2] ypos[3] = ypos[4] = plot.build$panel$ranges[[1]]$y.range[1] plot = plot + geom_text(aes(x2,y2,label = texthere), data.frame(x2=xpos, y2=ypos, texthere=c("1", "2", "3", "4")), color="#4daf4a", size=4) 

This basically works, but due to scaling, the space between the numbers and the borders of the graph is not the same for both axes. I tried to adjust the x position of the text, but then ggplot just widens the range of values, the positions (relative to the borders) remain unchanged. Is there a way to move text without changing the range of values?

Thanks in advance!

+5
source share
2 answers

This example uses the values Inf and -Inf to place the text in the corners, and then the hjust and vjust in geom_text to place the text inside the graph. Use the hjustvar and vjustvar buttons to position them farther or out of the chart.

As @baptiste already mentioned, it is best to use a new dataset for annotations

 df <- data.frame(x2=rnorm(100),y2=rnorm(100));library(ggplot2) annotations <- data.frame( xpos = c(-Inf,-Inf,Inf,Inf), ypos = c(-Inf, Inf,-Inf,Inf), annotateText = c("Bottom Left (h0,v0)","Top Left (h0,v1)" ,"Bottom Right h1,v0","Top Right h1,v1"), hjustvar = c(0,0,1,1) , vjustvar = c(0,1,0,1)) #<- adjust ggplot(df, aes(x2, y2)) + geom_point()+ geom_text(aes(x=xpos,y=ypos,hjust=hjustvar,vjust=vjustvar,label=annotateText)) 

Example text annotations in the corner

If we wanted to change any of the text positions, we would adjust the horizontal positions using hjustvar and the vertical positions using vjustvar .

 # How To Adjust positions (away from corners) annotations$hjustvar<-c(0, -1.5, 1, 2.5) # higher values = right, lower values = left annotations$vjustvar<-c(0,1,0,1) # higher values = up, lower values = down ggplot(df, aes(x2, y2)) + geom_point()+ geom_text(data = annotations, aes(x=xpos,y=ypos,hjust=hjustvar, vjust=vjustvar,label=annotateText)) 

Corner Height Correction

Hope it works!

+13
source

when adding annotations, be sure to add a new dataset or use the annotation, otherwise several labels are superimposed, creating an uneven look. Here is the minimal change from another answer,

 df <- data.frame(x2=rnorm(100),y2=rnorm(100)) library(ggplot2) annotations <- data.frame( xpos = c(-Inf,-Inf,Inf,Inf), ypos = c(-Inf, Inf,-Inf,Inf), annotateText = c("Text","tExt","teXt","texT"), hjustvar = c(0,0,1,1) , vjustvar = c(0,1.0,0,1)) ggplot(df, aes(x2, y2)) + geom_point()+ geom_text(data = annotations, aes(x=xpos,y=ypos,hjust=hjustvar, vjust=vjustvar,label=annotateText)) 

enter image description here

+8
source

All Articles