Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a vertical line with different intercept for each panel in ggplot2

I'm using ggplot2 to create panels of histograms, and I'd like to be able to add a vertical line at the mean of each group. But geom_vline() uses the same intercept for each panel (i.e. the global mean):

require("ggplot2")
# setup some sample data
N <- 1000
cat1 <- sample(c("a","b","c"), N, replace=T)
cat2 <- sample(c("x","y","z"), N, replace=T)
val <- rnorm(N) + as.numeric(factor(cat1)) + as.numeric(factor(cat2))
df <- data.frame(cat1, cat2, val)

# draws a single histogram with vline at mean
qplot(val, data=df, geom="histogram", binwidth=0.2) + 
  geom_vline(xintercept=mean(val), color="red")

# draws panel of histograms with vlines at global mean
qplot(val, data=df, geom="histogram", binwidth=0.2, facets=cat1~cat2) + 
  geom_vline(xintercept=mean(val), color="red")

How can I get it to use each panel's group mean as the x-intercept? (Bonus points if you can also add a text label by the line with the value of the mean.)

like image 368
yoyoyoyosef Avatar asked Oct 29 '09 15:10

yoyoyoyosef


People also ask

How do I add a vertical line in ggplot2?

To create a vertical line using ggplot2, we can use geom_vline function of ggplot2 package and if we want to have a wide vertical line with different color then lwd and colour argument will be used. The lwd argument will increase the width of the line and obviously colour argument will change the color.

How do I add a vertical line to a histogram in R?

The R function abline() can be used to add vertical, horizontal or regression lines to a graph. A simplified format of the abline() function is : abline(a=NULL, b=NULL, h=NULL, v=NULL, ...)

What is the difference between Facet_wrap and Facet_grid?

The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.

How do I add a horizontal line in ggplot2?

Example: To add the horizontal line on the plot, we simply add geom_hline() function to ggplot2() function and pass the yintercept, which basically has a location on the Y axis, where we actually want to create a vertical line.


2 Answers

I guess this is a reworking of @eduardo's really, but in one line.

ggplot(df) + geom_histogram(mapping=aes(x=val)) 
  + geom_vline(data=aggregate(df[3], df[c(1,2)], mean), 
      mapping=aes(xintercept=val), color="red") 
  + facet_grid(cat1~cat2)

alt text http://www.imagechicken.com/uploads/1264782634003683000.png

or using plyr (require(plyr) a package by the author of ggplot, Hadley):

ggplot(df) + geom_histogram(mapping=aes(x=val)) 
  + geom_vline(data=ddply(df, cat1~cat2, numcolwise(mean)), 
      mapping=aes(xintercept=val), color="red") 
  + facet_grid(cat1~cat2)

It seems unsatisfying that vline isn't cut on the facets, I'm not sure why.

like image 110
Alex Brown Avatar answered Oct 20 '22 04:10

Alex Brown


One way is to construct the data.frame with the mean values before hand.

library(reshape)
dfs <- recast(data.frame(cat1, cat2, val), cat1+cat2~variable, fun.aggregate=mean)
qplot(val, data=df, geom="histogram", binwidth=0.2, facets=cat1~cat2) + geom_vline(data=dfs, aes(xintercept=val), colour="red") + geom_text(data=dfs, aes(x=val+1, y=1, label=round(val,1)), size=4, colour="red")
like image 41
Eduardo Leoni Avatar answered Oct 20 '22 03:10

Eduardo Leoni