How to reverse the order of a legend so as to have the lowest value at the top

187 views
Skip to first unread message

gafna jeff

unread,
Oct 20, 2021, 3:52:00 AM10/20/21
to ggplot2
Hi everyone,
 I would really appreciate any help. How can I reverse the order of my legend so as to have the lowest value (ie 0 - 400) at the top and the highest value (  i.e > 2400) at the bottom. Here is a brief part of my script and my map in pdf. 


library (ggplot2)

#Create a data frame from the raster
rdf <- as.data.frame(elevation_raster, xy=TRUE)
names(rdf)

##create the breaks on the raster
my_breaks <- c(400,800,1200,1600,2000, 2400,2794)


ggplot()  + geom_raster(data = rdf, aes(x,y, fill = layer)) +
  scale_fill_viridis_c(direction = 1, na.value = 'white', name="Elevation", breaks = my_breaks , labels = c("0 - 400m ","400 - 800m", "800 - 1200m", "1200 - 1600m", "1600 - 2000m", "2000 - 2400m", "> 2400m"))


My map.pdf

Roman Luštrik

unread,
Oct 20, 2021, 4:05:40 AM10/20/21
to gafna jeff, ggplot2
Would reversing breaks and labels work?

Cheers,
Roman

--
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/ggplot2/CAABPRJdVcwWRDjSm6ZmCHCCDTfLYoYoBte1si%2BZVK1G7wAVQ0g%40mail.gmail.com.


--
In God we trust, all others bring data.

Ron Crump

unread,
Oct 20, 2021, 4:18:22 AM10/20/21
to ggp...@googlegroups.com
Hi,

> How can I reverse the order of my
> legend so as to have the lowest value (ie 0 - 400) at the top and the
> highest value (  i.e > 2400) at the bottom.

> ggplot()  + geom_raster(data = rdf, aes(x,y, fill = layer)) +
>   scale_fill_viridis_c(direction = 1, na.value = 'white',
> name="Elevation", breaks = my_breaks , labels = c("0 - 400m ","400 -
> 800m", "800 - 1200m", "1200 - 1600m", "1600 - 2000m", "2000 - 2400m", ">
> 2400m"))

Try adding the option:
guide = guide_colourbar(reverse = TRUE)
into your scale_fill_viridis_c call.

Hope this helps. I think it should do it, it worked on a very
simple example without the other options you have.

Ron.

gafna jeff

unread,
Oct 20, 2021, 4:24:03 AM10/20/21
to Ron Crump, ggplot2
Dear Roman,

Thanks for your suggestion. I really appreciate it. However, reversing breaks and labels work does not solve the problem

Jeff

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

gafna jeff

unread,
Oct 20, 2021, 4:27:11 AM10/20/21
to Ron Crump, ggplot2
Thank you Ron. I really appreciate it. Adding this to the viridis scale solves the problem perfectly:   guide = guide_colourbar(reverse = TRUE)


Regards,
Jeff

On Wed, Oct 20, 2021 at 10:18 AM 'Ron Crump' via ggplot2 <ggp...@googlegroups.com> wrote:

Roman Luštrik

unread,
Oct 20, 2021, 4:33:02 AM10/20/21
to gafna jeff, Ron Crump, ggplot2
What I usually do is I set up all the variables used in plotting in the data.frame. That way I can manully adjust the order of levels in a factor which dictates the way they appear in the legend.

Cheers,
Roman

Reply all
Reply to author
Forward
0 new messages