How to turn off lines in raster fill label mixed legends ggplot

14 views Asked by At

I'm plotting points and two sets of spatial lines (roads and rivers) on top of a categorical raster. I can define the colors and labels of the rasters and give that legend a title and it looks great. But when I add the spatial lines on top and try to add a second legend for the two sets of spatial lines, black lines are now introduced into the raster category boxes. How do I remove these lines?

colors_6 =
  c("darkolivegreen1", # Crop
    "#FDE333", # Open Canopy
    "#CCAA50", # Barren/fallow
    "cadetblue1", # Water
    "#A4A4A4", # Developed
    "#0A6B22") # Closed Canopy

ggplot(my_raster)+
 geom_spatraster(data = my_raster, aes(fill = Classes))+
 geom_point(data = the_points, aes(x = X, y = Y))+
 scale_fill_manual(values = colors_6,
                   labels = c("Crop", "Open Canopy", "Barren/Fallow",
                              "Water", "Developed", "Closed Canopy")+
 geom_sf(data = roads, aes(color = "Roads"), alpha = 0.5, show.legend = 'line')+
 geom_sf(data = rivers, aes(color = "Rivers"), alpha = 0.5, show.legend = 'line')+
 scale_color_manual(name = "", values = c("Roads" = "black", "Flowlines" = "blue")

enter image description here

I also tried defining the categorical raster legend with scale_fill_discrete() and the lines with scale_color_discrete(). I tried using inherit.aes = FALSE and creating new scales with ggnewscale::new_scale_color and new_scale_fill but it doesn't work.

Thanks/

1

There are 1 answers

0
Jade131621 On

I swear it's always two seconds after I breakdown and actually post a question that I figure out the problem I've been mulling over for twelve hours:

Here's how I fixed it, specify the key_glyph.

    colors_6 = c("darkolivegreen1", # Crop
                 "#FDE333", # Open Canopy
                 "#CCAA50", # Barren/fallow
                 "cadetblue1", # Water
                 "#A4A4A4", # Developed
                 "#0A6B22") # Closed Canopy
     ggplot(my_raster)+
        geom_spatraster(data = my_raster, aes(fill = Classes), 
                        key_glyph = "rect")+
        geom_point(data = the_points, aes(x = X, y = Y))+
        scale_fill_manual(values = colors_6,
                          labels = c("Crop", "Open Canopy", "Barren/Fallow",
                                     "Water", "Developed", "Closed Canopy")+
        geom_sf(data = roads, aes(color = "Roads"), alpha = 0.5, key_glyph = 
                "smooth")+
        geom_sf(data = rivers, aes(color = "Rivers"), alpha = 0.5, key_glyph = 
                "smooth")+
        scale_color_manual(name = "", 
                          values = c("Roads" = "black", "Flowlines" = "blue"))