Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing multiple axis labels using ggplot2 with facet_wrap in R

I've got a nice facet_wrap density plot that I have created with ggplot2. I would like for each panel to have x and y axis labels instead of only having the y axis labels along the left side and the x labels along the bottom. What I have right now looks like this:

library(ggplot2)
myGroups <- sample(c("Mo", "Larry", "Curly"), 100, replace=T)
myValues <- rnorm(300)
df <- data.frame(myGroups, myValues)


p <- ggplot(df)  + 
  geom_density(aes(myValues), fill = alpha("#335785", .6)) + 
  facet_wrap(~ myGroups)
p

Which returns:

alt text
(source: cerebralmastication.com)

It seems like this should be simple, but my Google Fu has been too poor to find an answer.

like image 738
JD Long Avatar asked Oct 07 '09 15:10

JD Long


1 Answers

You can do this by including the scales="free" option in your facet_wrap call:

myGroups <- sample(c("Mo", "Larry", "Curly"), 100, replace=T)
myValues <- rnorm(300)
df <- data.frame(myGroups, myValues)


p <- ggplot(df)  + 
  geom_density(aes(myValues), fill = alpha("#335785", .6)) + 
  facet_wrap(~ myGroups, scales="free")
p

enter image description here

like image 61
robert Avatar answered Oct 09 '22 14:10

robert