facet_grid() for multiple y variables?

1,652 views
Skip to first unread message

Vikram Chhatre

unread,
Apr 8, 2012, 1:34:56 PM4/8/12
to ggplot2
Hello -

I want to make stacked plots, something akin to what par(mfrow=c(6,1))
will produce. My dataset includes 5 variables (5 columns) that I want
to put on y axis and one x variable. Essentially, these y variables
are independent of each other. Thus, what I am trying to achieve is
simply stack different plots atop each other, but label the x axis
only on the bottom plot.

So far, the facet_grid() examples I have seen use a variable or
variables that are dependent on each other to split the data into
several plots on a grid. So I am wondering if this function is
suitable for my purposes. If not, is there another way to accomplish
this in ggplot2?

Thanks
Vikram

Winston Chang

unread,
Apr 8, 2012, 4:18:42 PM4/8/12
to Vikram Chhatre, ggplot2
I think you want to use melt() from the reshape2 package. See ?melt.data.frame for more information on how to use it.


set.seed(32)
dat <- data.frame(id=LETTERS[1:10],
                  x  = 1:10,
                  y1 = rnorm(10),
                  y2 = rnorm(10),
                  y3 = rnorm(10)
                 )

# print the data frame
dat

# convert to long format
library(reshape2)
datl <- melt(dat, id.vars=c("id", "x"))
# print
datl

library(ggplot2)
ggplot(datl, aes(x=x, y=value)) + geom_point() + facet_grid(variable ~ .)



Vikram

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442

To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Vikram Chhatre

unread,
Apr 8, 2012, 11:34:04 PM4/8/12
to Winston Chang, ggplot2
Thank you Winston. That was helpful. I was able to prepare the plot
by using melt() function.

Vikram

Reply all
Reply to author
Forward
0 new messages