Multiple color scales in faceted graphs

101 views
Skip to first unread message

Kevin Stierhoff

unread,
Mar 22, 2012, 12:54:38 AM3/22/12
to ggp...@googlegroups.com
I'm plotting different variables in a faceted graph, and the variables have different scales. Is it possible for each graph to have its own color scale? Using the code below, the color variation for most of the panels is uniform since one of the variable's data range (e.g., depth_r) is much broader than the others.

Here's a link to the data:
http://dl.dropbox.com/u/16369868/nav_gg.Rdata

Here is the code:
m <- ggplot(nav.gg,aes(lon_dd_r,lat_dd_r))
m + geom_point(aes(colour = value)) + facet_wrap(~variable)

Thanks,
Kevin

--
Kevin Stierhoff, PhD
Research Fisheries Biologist
NOAA Fisheries Service - Southwest Fisheries Science Center

Ben Bond-Lamberty

unread,
Mar 22, 2012, 9:38:09 AM3/22/12
to ggp...@googlegroups.com
Kevin,

Cool-looking data!

As far as I know--unless 0.9 has new capabilities in this
area--ggplot2 can't do what you want here. You have two options:
first, use viewport and set up multiple independent plots, each with
an independent color scale; second, rescale "value" so that they're
all in the same range, e.g.

nav.gg.rescaled <- ddply(nav.gg,.(variable),function(d){d$value <-
rescale(d$value); d})

and then plot exactly as before. This puts the data (subsetted by
variable) on a common 0-1 scale.

Ben

> --
> 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

Kevin Stierhoff

unread,
Mar 22, 2012, 11:43:09 AM3/22/12
to ggp...@googlegroups.com
Thanks Ben,
Seems the code you proposed isn't working for me. I modified the 'rescale' section of code somewhat, suspecting a problem there, but had no luck. Were you able to get the lines below to run and output what you expected?
Kevin

Ben Bond-Lamberty

unread,
Mar 22, 2012, 12:28:15 PM3/22/12
to kevin.s...@noaa.gov, ggp...@googlegroups.com
Yes, it worked just fine for me:

> load("nav_gg.Rdata")
> library(ggplot2)
> library(plyr) # just in case


> nav.gg.rescaled <- ddply(nav.gg,.(variable),function(d){d$value <- rescale(d$value); d})

> m1 <- ggplot(nav.gg.rescaled,aes(lon_dd_r,lat_dd_r))
> m1 + geom_point(aes(colour = value)) + facet_wrap(~variable)
> sessionInfo()
R version 2.14.1 (2011-12-22)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] grid stats graphics grDevices utils datasets
methods base

other attached packages:
[1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.6

loaded via a namespace (and not attached):
[1] digest_0.5.1

On Thu, Mar 22, 2012 at 11:43 AM, Kevin Stierhoff

Art

unread,
Mar 22, 2012, 12:41:44 PM3/22/12
to ggp...@googlegroups.com, kevin.s...@noaa.gov

Hey Kevin,


You need to load the 'scales' package if you are using ggplot2 v 0.9.

library(scales) which has the rescale function.

Best,
Art 

Kevin Stierhoff

unread,
Mar 22, 2012, 1:03:00 PM3/22/12
to Art, ggp...@googlegroups.com
Thanks all. I think this was the problem. Adding the scales package seems to have fixed it, since I'm using v0.9 of ggplot2. Thanks Art and Ben! Now I'm on my way!
Reply all
Reply to author
Forward
0 new messages