Question about facets

58 views
Skip to first unread message

David Parkhurst

unread,
Feb 28, 2014, 10:54:56 PM2/28/14
to ggp...@googlegroups.com
I'm new to ggplot2, and I'm looking at the description of facets in the
2012 edition of R Graphics Cookbook, by Winston Chang. From what I find
there, I'm unclear whether facets can do what I want. Specifically, I
have a data frame with five variables, Date, y1, y2, y3, and y4. The
y's all have different scales---they're different types of variables,
but they were all measured on the same dates. I'd like to plot each of
the y's against Date, with the four graphs arranged one above the other.
Is that possible with facets, as it is with lattice graphs?
David

Vivek Patil

unread,
Mar 1, 2014, 12:14:07 AM3/1/14
to David Parkhurst, ggp...@googlegroups.com
You can reshape the data frame to long format   using melt and plot the 4 graphs using facets. Package reshape2 is required for the melt function. See: http://www.cookbook-r.com/Manipulating_data/Converting_data_between_wide_and_long_format/. 

A reproducible example helps.
--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

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

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

David Parkhurst

unread,
Mar 1, 2014, 9:49:47 AM3/1/14
to ggp...@googlegroups.com
I have a situation that is much like that in the code below.  I'd like to use facets to stack the four point plots, one above the other.  I'm completely new to ggplot, and would appreciate some help.  Two questions:

1. How do I use the melt function to reshape my data frame?  Do I need to add some sort of “subject,”, and if so, what would that look like?

2. Once the data frame is reshaped, what command would facet the four plots?  (I'd like only the bottom one to have dates shown on the x axis.)

Thank you for any help.
David  Here's the code:

date = 33817:34303   ## My actual dates
y1 = runif(487,0,5)
y2 = rnorm(487,100,30)
y3 = runif(487,0,1000)
y4 = runif(487,0,100)
df = data.frame(y1,y2,y3,y4,date)
library(ggplot2)
library(reshape2)
ggplot(df, aes(x=date, y=y1)) + geom_point()
ggplot(df, aes(x=date, y=y2)) + geom_point()
ggplot(df, aes(x=date, y=y3)) + geom_point()
ggplot(df, aes(x=date, y=y4)) + geom_point()




Vivek Patil

unread,
Mar 1, 2014, 10:03:42 AM3/1/14
to David Parkhurst, ggplot2
Is this the objective? Please also check-out the documentation for facet_wrap at: http://docs.ggplot2.org/0.9.3.1/facet_wrap.html

library(ggplot2)
library(reshape2)
dfmelt=melt(df,id="date")
ggplot(dfmelt,aes(x=date,y=value))+geom_point()+facet_wrap(~ variable,scales = "free_y",ncol=1)


Martin Bel

unread,
Mar 1, 2014, 2:18:24 PM3/1/14
to David Parkhurst, ggp...@googlegroups.com

try using melt() to covert your data.frame into long format and then try using facet_grid or facet_wrap for plotting. Take a look at the documentation for examples.

good luck

El mar 1, 2014 12:55 AM, "David Parkhurst" <dfp...@gmail.com> escribió:
I'm new to ggplot2, and I'm looking at the description of facets in the 2012 edition of R Graphics Cookbook, by Winston Chang.  From what I find there, I'm unclear whether facets can do what I want.  Specifically, I have a data frame with five variables, Date, y1, y2, y3, and y4.  The y's all have different scales---they're different types of variables, but they were all measured on the same dates.  I'd like to plot each of the y's against Date, with the four graphs arranged one above the other.  Is that possible with facets, as it is with lattice graphs?
David

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility

To post: email ggp...@googlegroups.com

More options: http://groups.google.com/group/ggplot2

--- You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+unsubscribe@googlegroups.com.

Dennis Murphy

unread,
Mar 2, 2014, 4:29:42 PM3/2/14
to David Parkhurst, ggplot2
Hi:

Vivek's reply looks pretty good to me, but since you are new to this
game, I'll explain a bit how melt() works and why it's useful in this
situation for ggplot2.

The reshape2 package is designed to manipulate data from wide to long
form (melt()) or long to wide form (acast() or dcast()). In your case,
the right call is to reshape your data frame from wide to long. The
melt() function behaves somewhat like the stack() function in base R;
when the non-id (or 'measure') variables are stacked, two variables
are created: a factor named variable, which takes the original
variable names in long form and converts them to factor levels, and a
corresponding numeric variable named value. The 'constant' variable in
your original data frame is date, so it becomes the id variable. In
the melted data frame, it is recycled as many times as the number of
stacked variables. The resulting melted data frame has three
variables: date, variable and value.

The melted data frame is passed to ggplot(). The reason why this
approach works is because the faceting variable is normally a factor.
Also notice that the code is set up to plot value vs. date by
variable; the data structure returned by melt() made this possible.
The remaining options to facet_wrap() allow for vertical stacking of
the plots (ncol = 1) and free y-scales (scales = "free_y").

You can also plot dates on the horizontal axis in ggplot2; it is smart
enough to deduce if the x-variable is of class Date and plot a
set of default labels. I don't know what the time origin is of your
dates - I guessed it was Excel's default of 1 Jan 1900 based on a
little trial and error - let's pretend it is. Using Vivek's notation,

dfmelt$Date <- as.Date(dfmelt$date, origin = "1900-01-01")
ggplot(dfmelt, aes(x = Date, y = value)) +
geom_point() +
facet_wrap(~ variable, ncol = 1, scales = "free_y")

If the dates are wrong, modify the origin. If you want to change the
date scale, use the scale_x_date() function to change the breaks. To
change the labels, use the date_format() function from the scales
package. There are a few examples here; see section 7.1:
http://cloud.github.com/downloads/hadley/ggplot2/guide-col.pdf

You can take your set of original ggplots, assign them to distinct
objects and use the grid.arrange() function in the gridExtra package
to accomplish the same task, but the differences in the magnitudes of
the y-axis labels will cause the left-side plot boundaries to be
misaligned. There are documented ways to fix the problem using the
gtable package, but the ggplot2 solution in this case is simpler and a
bit cleaner visually. Since you are new to ggplot2, simpler is better.

Dennis

On Sat, Mar 1, 2014 at 6:49 AM, David Parkhurst <dfp...@gmail.com> wrote:

IU

unread,
Mar 2, 2014, 5:52:23 PM3/2/14
to Dennis Murphy, David Parkhurst, ggplot2
Thank you very much for the detailed help. I'll have a go at fixing my work tomorrow.

And a small detail for the record, I've learned that the true origin for Excel's date numbers is 1899/12/30. They were off from the first day of 1900 somehow because of leap years. I don't know the details.

DFP (iPad)
Reply all
Reply to author
Forward
0 new messages