reverse legend of continuous colour scale

2,855 views
Skip to first unread message

silke

unread,
Sep 3, 2014, 9:42:28 AM9/3/14
to ggp...@googlegroups.com
Hey all,

I would like to reverse the legend of a continuous variable, so that small numbers are on top of the large numbers. Here is an example of the legend I would like to reverse:

df <- data.frame(x=1:20, y=runif(20), depth=runif(20, min=1, max=10))
pl <- ggplot(df, aes(x,y,colour=depth))
pl + geom_point() +
scale_colour_gradient(low="#5EB7F8", high="#1A334B")

I can create the reverse legend with "-depth" (and reverse high/low values), so it looks like how I want it to look like, but the negative numbers in the label are not really correct:

pl <- ggplot(df, aes(x,y,colour=-depth))
pl + geom_point() +
scale_colour_gradient(high="#5EB7F8", low="#1A334B")

I experimented with "guide_legend", which orders the labels as I want them, but it seems to change depth into a discrete factor; I would however like to keep the continuous scale:

pl <- ggplot(df, aes(x,y,colour=depth))
pl + geom_point() +
scale_colour_gradient(guide=guide_legend(reverse = FALSE), low="#5EB7F8", high="#1A334B")



I would be grateful for any help and suggestions!

Thanks,
Silke

Brian Diggs

unread,
Sep 3, 2014, 11:43:36 AM9/3/14
to silke, ggplot2
You are almost there. Using reverse in the guide specification is
correct, but you don't want a legend (discrete) guide but a colourbar
(continuous colour) guide

pl <- ggplot(df, aes(x,y,colour=depth)) +
geom_point() +
scale_colour_gradient(guide=guide_colourbar(reverse = TRUE),
low="#5EB7F8", high="#1A334B")



> I would be grateful for any help and suggestions!
>
> Thanks,
> Silke
>


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

silke

unread,
Sep 3, 2014, 12:23:02 PM9/3/14
to ggp...@googlegroups.com, silkita-Re5JQEe...@public.gmane.org
Thanks a lot, exactly what I wanted! - I'm not sure I've really understood the guide-commands (obviously)...
Reply all
Reply to author
Forward
0 new messages