geom_ribbon in melted data frame

516 views
Skip to first unread message

Ivan Alves

unread,
Nov 23, 2008, 6:46:28 PM11/23/08
to ggplot2
Dear all,

I have a molte

Ivan Alves

unread,
Nov 23, 2008, 6:46:28 PM11/23/08
to ggplot2

Ivan Alves

unread,
Nov 23, 2008, 6:46:28 PM11/23/08
to ggplot2

Ivan Alves

unread,
Nov 23, 2008, 6:52:28 PM11/23/08
to ggplot2
Apologies. Just getting used to google groups..

I have a molten dataframe with 3 columns: Date, variable (two levels
for two variables) and value. I would like to apply geom_ribbon to
the area between the two time series variables. How do I get
geom_ribbon to understand ymax to be one variable and ymin the other?

I manage if I cast the variables to columns,, but then I have great
difficulties to add labels to the graph (the documentation that Hadley
has kindly provided is rather incomplete when it comes to modifying
labels.

Any help would be greatly appreciated.

Kind regards,

ivan

hadley wickham

unread,
Nov 23, 2008, 11:22:52 PM11/23/08
to Ivan Alves, ggplot2
> I have a molten dataframe with 3 columns: Date, variable (two levels
> for two variables) and value. I would like to apply geom_ribbon to
> the area between the two time series variables. How do I get
> geom_ribbon to understand ymax to be one variable and ymin the other?
>
> I manage if I cast the variables to columns,, but then I have great
> difficulties to add labels to the graph (the documentation that Hadley
> has kindly provided is rather incomplete when it comes to modifying
> labels.

Yes, casting the variables into columns is definitely the way to go,
but you shouldn't have any problems modifying axis labels. Either:

qplot(..., ylab = "My label")
or
ggplot(...) + ... + scale_y_continuous("My label")

I'm not sure how to make it easier to find this information - it's
documented in qplot, and it's the first hit for "axis labels" if you
search the ggplot2 site.

Hadley


--
http://had.co.nz/

Ivan Alves

unread,
Nov 24, 2008, 3:40:10 AM11/24/08
to ggplot2
Dear Hadley,

Many thanks for the guidance on labelling the axis with ggplot (I had
figured it out with qplot). In fact by "labelling" I meant the
legend. You see, I am using the following (the variables have been
casted to var1 and var2):

ggplot(test, aes(x=Date)) + geom_ribbon(aes(ymin=var1, ymax=var2),
fill = "light grey") + geom_line(aes(y=var1), colour = "light blue",
linetype = 1) + scale_y_continuous("Thanks for the label!") +
geom_line(aes(y=var2), colour = "blue", linetype = 1) + geom_point(aes
(y = var2), size = 1) + geom_point(aes(y = var1), size = 1) + opts
(axis.title.x = theme_blank())

This works really nice, however, I would like to create a legend with
the keys created by both the geom_lines and geom_point layers (or at
least one of them) for _both_ series var1 and var2. I have not
figured out how to create legends other than automatically indicating
a map to a variable (that means with the Molten dataset). The section
on how to customised legends seems to be missing in your draft book.

Again, many thanks for any help (and already for creating such a great
library!)

Ivan

hadley wickham

unread,
Nov 24, 2008, 8:50:31 AM11/24/08
to Ivan Alves, ggplot2
Hi Ivan,

Thanks for the clarification. Legends always come from the scales, so
you need to set up the plot in such a away that the scales know what
you want. Here's a plot that captures the essence of your problem:

huron <- data.frame(year = 1875:1972, level = as.vector(LakeHuron))
ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5), colour = "blue") +
geom_line(aes(y = level + 5), colour = "red")

You have two lines from the same dataset that you want to colour
differently, and include them in the legend. In most other plotting
systems, you'd just colour the lines as above, and then add a legend
that describes which colour maps to which variable. That doesn't work
in ggplot2 because it's the scales that are responsible for drawing
legends (the colour scale in this case), and they don't know about the
values the lines should take.

What you need to do is tell the colour the scale about the two
different lines by creating a mapping from the data to the colour
aesthetic. There's no variable present in the data, so you'll have to
create one:

ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5, colour = "below")) +
geom_line(aes(y = level + 5, colour = "above"))

This gets us basically what we want, but the legend isn't labelled
correctly, and has the wrong colours. That can be fixed with
scale_colour_manual:

ggplot(huron, aes(year)) +
geom_line(aes(y = level - 5, colour = "below")) +
geom_line(aes(y = level + 5, colour = "above")) +
scale_colour_manual("Direction", c("below" = "blue", "above" = "red"))

In your original example, this is equivalent to operating on the
molten data and using geom_line(aes(colour = variable), data = molten)

Does that help? I'll include something like this in the next version
of the book, probably in the manual scale section.

Regards,

Hadley
--
http://had.co.nz/

Ivan Alves

unread,
Nov 26, 2008, 5:17:43 PM11/26/08
to ggplot2
Many thanks, dear Hadley. The scale_colour_manual worked perfectly.
Many thanks!
Reply all
Reply to author
Forward
0 new messages