Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplaying text below the plot generated by ggplot2
    text
    copied!<p>I am trying to display some information about the data below the plot created in <strong>ggplot2</strong>. I would like to plot the N variable using the X axis coordinate of the plot but the Y coordinate needs to be 10% from the bottom of the screen . In fact, the desired Y coordinates are already in the data frame as y_pos variable. </p> <p>I can think of 3 approaches using <strong>ggplot2</strong>:</p> <p>1) Create an empty plot below the actual plot, use the same scale and then use geom_text to plot the data over the blank plot. <a href="http://learnr.wordpress.com/2009/04/29/ggplot2-labelling-data-series-and-adding-a-data-table/" rel="nofollow noreferrer">This approach</a> sort of works but is extremely complicated.</p> <p>2) Use <code>geom_text</code> to plot the data but somehow use y coordinate as percent of the screen (10%). This would force the numbers to be displayed below the plot. I can't figure out the proper syntax. </p> <p>3) Use grid.text to display the text. I can easily set it at the 10% from the bottom of the screen but I can't figure how set the X coordindate to match the plot. I tried to use grconvert to capture the initial X position but could not get that to work as well.</p> <p>Below is the basic plot with the dummy data:</p> <pre><code>graphics.off() # close graphics windows library(car) library(ggplot2) #load ggplot library(gridExtra) #load Grid library(RGraphics) # support of the "R graphics" book, on CRAN #create dummy data test= data.frame( Group = c("A", "B", "A","B", "A", "B"), x = c(1 ,1,2,2,3,3 ), y = c(33,25,27,36,43,25), n=c(71,55,65,58,65,58), y_pos=c(9,6,9,6,9,6) ) #create ggplot p1 &lt;- qplot(x, y, data=test, colour=Group) + ylab("Mean change from baseline") + geom_line()+ scale_x_continuous("Weeks", breaks=seq(-1,3, by = 1) ) + opts( legend.position=c(.1,0.9)) #display plot p1 </code></pre> <p>The modified gplot below displays numbers of subjects, however they are displayed WITHIN the plot. They force the Y scale to be extended. I would like to display these numbers BELOW the plot.</p> <pre><code> p1 &lt;- qplot(x, y, data=test, colour=Group) + ylab("Mean change from baseline") + geom_line()+ scale_x_continuous("Weeks", breaks=seq(-1,3, by = 1) ) + opts( plot.margin = unit(c(0,2,2,1), "lines"), legend.position=c(.1,0.9))+ geom_text(data = test,aes(x=x,y=y_pos,label=n)) p1 </code></pre> <p>A different approach of displaying the numbers involves creating a dummy plot below the actual plot. Here is the code:</p> <pre><code>graphics.off() # close graphics windows library(car) library(ggplot2) #load ggplot library(gridExtra) #load Grid library(RGraphics) # support of the "R graphics" book, on CRAN #create dummy data test= data.frame( group = c("A", "B", "A","B", "A", "B"), x = c(1 ,1,2,2,3,3 ), y = c(33,25,27,36,43,25), n=c(71,55,65,58,65,58), y_pos=c(15,6,15,6,15,6) ) p1 &lt;- qplot(x, y, data=test, colour=group) + ylab("Mean change from baseline") + opts(plot.margin = unit(c(1,2,-1,1), "lines")) + geom_line()+ scale_x_continuous("Weeks", breaks=seq(-1,3, by = 1) ) + opts(legend.position="bottom", legend.title=theme_blank(), title.text="Line plot using GGPLOT") p1 p2 &lt;- qplot(x, y, data=test, geom="blank")+ ylab(" ")+ opts( plot.margin = unit(c(0,2,-2,1), "lines"), axis.line = theme_blank(), axis.ticks = theme_segment(colour = "white"), axis.text.x=theme_text(angle=-90,colour="white"), axis.text.y=theme_text(angle=-90,colour="white"), panel.background = theme_rect(fill = "transparent",colour = NA), panel.grid.minor = theme_blank(), panel.grid.major = theme_blank() )+ geom_text(data = test,aes(x=x,y=y_pos,label=n)) p2 grid.arrange(p1, p2, heights = c(8.5, 1.5), nrow=2 ) </code></pre> <p>However, that is very complicated and would be hard to modify for different data. Ideally, I'd like to be able to pass Y coordinates as percent of the screen.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload