Editing legends with ggplot

Is it possible to edit more than one legend in ggplot? I'm using ggplot to map a raster and a shape file with coordinates. I would like to place the legend of the raster at the bottom and place the legend of the coordinates at the right side of the map.

This is the code I'm using:


S_EFT_df %>%
select(x, y, 2001) %>%
geom_raster(aes(x = x, y = y, fill = 2001))+
scale_fill_continuous_sequential(name="EFT", palette="YlGn")+
labs(title = "2001")+
theme(plot.title = element_text(size=20, face= "bold", hjust = 0.5), legend.position = "bottom")+
theme_void() +
coord_equal()+ #minimize distortion
geom_sf(data = Guade, aes(color = Species))+
guides(legend.position="right", col = guide_legend("Species",nrow = 9, byrow = TRUE,override.aes = list(size = 1.5)))

Example data set: Example Data

Thank you.

To place the legend at the bottom the code is:

theme(legend.position = "bottom")

or you can specify coordinates:

theme(legend.position = c(2, 4))

This chunk of code isn't the best practice: aes(color = Species), col="red"
if you wish, you can post a small part of the dataframe to allow us to run your code and check what can be improved using dput(head(YOURDF, 15))

1 Like

I did try using theme(legend.position) but it moved both the raster legend and the shape file's legend to the same place. I would like to edit and move the legends independently; having the raster's legend at the bottom and the shape file's legend (Species) at the right side of the map.

I created a mini data set, hopefully this link works and is accessible: Example Data

Thank you.

To move the legends you could use this method r - How to place legends at different sides of plot (bottom and right side) with ggplot2? - Stack Overflow or this r - How do I position two legends independently in ggplot - Stack Overflow according to your preferences/knowledge of the packages

1 Like

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.