using scale_size_manual

4,489 views
Skip to first unread message

Fred Iutzi

unread,
Nov 25, 2008, 11:59:43 AM11/25/08
to ggplot2
I'm using ggplot2 to make plots of wind speed data over 12 months of
the year, with a smoothed line for each "year" of data (e.g. a line
for 2006, a line for 2007, a line for a mean of multiple years, etc.),
and a plot for each geographical location, like this:
http://www.wiu.edu/users/fi101/wind.png .

I'd like to use scale_size_manual to tailor the widths of the lines,
in this case to make the line representing the mean wider than the
others -- but I can't seem to get a response of any kind when I invoke
it as below. Any thoughts? scale_colour-manual is working
beautifully, by the way.

ggplot(data=nwsdata,
aes(x=month, y=speed)) +
facet_grid(station~.) +
geom_smooth(aes(colour=year), se=F) +
scale_colour_manual(values=c("yellow", "blue", "green", "red")) +
scale_size_manual(values=c(.5, .5, .5, 1))


-Fred Iutzi, Western Illinois University

hadley wickham

unread,
Nov 25, 2008, 1:02:16 PM11/25/08
to Fred Iutzi, ggplot2
Hi Fred,

You just forgot to map size to year!

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

Fred Iutzi

unread,
Nov 25, 2008, 1:31:01 PM11/25/08
to ggplot2
And thus the problem is solved:

geom_smooth(aes(colour=year, size=year), se=F) +

Thanks for the tip. This might have also served as my critical
learning moment for understanding that scales are first mapped to
variables in a geom_ statement, and then controlled with a scale_
statement.

Separately, I just noticed that at http://had.co.nz/ggplot2/scale_linetype.html
, the following note appears:

> # The linetype scale currently has no options, so there's
> # no point in adding it manually

But I was just successful in using scale_linetype_manual to
selectively apply different line types, so you might be offering even
more functionality than you're advertising.

-Fred

hadley wickham

unread,
Nov 25, 2008, 1:42:48 PM11/25/08
to Fred Iutzi, ggplot2
> Thanks for the tip. This might have also served as my critical
> learning moment for understanding that scales are first mapped to
> variables in a geom_ statement, and then controlled with a scale_
> statement.

Great :)

>> # The linetype scale currently has no options, so there's
>> # no point in adding it manually
>
> But I was just successful in using scale_linetype_manual to
> selectively apply different line types, so you might be offering even
> more functionality than you're advertising.

The confusion is in what manually means - there's no point in doing +
scale_linetype(). I've improved that line for the next version.

Hadley

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

Fred Iutzi

unread,
Nov 25, 2008, 5:15:14 PM11/25/08
to ggplot2
So I've perfected my wind speed code on a dummy data set containing
three stations (http://www.wiu.edu/users/fi101/wind.png). I just
received the real data, and there are something like 70 stations -- so
my use of facet_grid to generate a single axis (i.e. vertical or
horizontal) stack of plots no longer works. What's my best pathway
for creating a grid of arbitrarily arranged plots, one plot for each
station?

Thanks,
Fred

Fred Iutzi

unread,
Nov 25, 2008, 6:00:14 PM11/25/08
to ggplot2
Thanks for pointing me back to facet_wrap(), Hadley. I had actually
started there, but then blundered around for a while and decided that
facet_wrap() must be for something else. It took about 15 minutes
after revisiting it for me to realize that I had to keep my facet_grid
() statement in place to make the whole system work (I had commented
it out when I inserted facet_wrap()). I ended up with this plot,
which is exactly what I needed (or will be after I fiddle with the
scales): http://www.wiu.edu/users/fi101/stationgrid.png . (It turns
out there were only 48 weather stations).

In case it's of any benefit to anyone else, here is the full ggplot
code used to make that graphic, preceded by a look at the dataset
used:

> > head(nwsdata)
station year month speed
1 720137 2005 1 8.972548
2 720137 2005 2 8.316971
3 720137 2005 3 9.437224
4 720137 2005 4 9.435749
5 720137 2005 5 9.590001
6 720137 2005 6 7.865271

ggplot(data=nwsdata,
aes(x=month, y=speed)) +
facet_grid(station~.) +
facet_wrap(~station, nrow=6) +
geom_smooth(
aes(colour=year,
size=year,
linetype=year),
se=F) +
scale_colour_manual(values=c("yellow", "blue", "green", "red")) +
scale_size_manual(values=c(.5, .5, .5, 1)) +
scale_linetype_manual(value=c(1, 1, 1, 4)) +
scale_x_continuous(breaks=1:12,
labels=c("J","F","M","A","M","J","J","A","S","O","N","D")) +
scale_y_continuous("wind speed (mph)")

Running this code does generate the following error messages, although
they're not inhibiting any of the layers I wanted on the plot as far
as I can tell:

Error in UseMethod("depth") : no applicable method for "depth"
In addition: Warning message:
In grid.Call.graphics("L_rect", x$x, x$y, x$width, x$height,
resolveHJust(x$just, :
semi-transparency is not supported on this device: reported only
once per page

This is a really great graphics package.

-Fred

hadley wickham

unread,
Nov 25, 2008, 6:27:17 PM11/25/08
to Fred Iutzi, ggplot2
> Running this code does generate the following error messages, although
> they're not inhibiting any of the layers I wanted on the plot as far
> as I can tell:
>
> Error in UseMethod("depth") : no applicable method for "depth"

I don't think that's a ggplot2 error message - I've seen it sometimes
when resizing plots, but I've never been able to reproduce it
reliably.

> In addition: Warning message:
> In grid.Call.graphics("L_rect", x$x, x$y, x$width, x$height,
> resolveHJust(x$just, :
> semi-transparency is not supported on this device: reported only
> once per page

And I'm not sure why you get this error, because you are using an
transparent colours.

> This is a really great graphics package.

Thanks :)

Hadley

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

Reply all
Reply to author
Forward
0 new messages