0

I'm mapping and I can't make the legend become unified.

This is my code:

ggplot() +
     geom_sf(data = all_boundaries$osm_multipolygons,
             inherit.aes = TRUE,
             color = "lightgrey",
             size = 0.7,
             alpha = 0.8) + 
     geom_sf(data = all_boundaries$osm_lines,
             inherit.aes = TRUE,
             linetype = "dotted",
             color = "#D55E00",
             size = 0.7,
             alpha = 0.8)  +
     geom_sf(data = coast$osm_lines,
             inherit.aes = TRUE,
             color = "#56B4E9",
             size = 12) +
     coord_sf(xlim = c(-113.517109, -117.823506), 
              ylim = c(30.742140, 33.961826),
              expand = FALSE) +
     theme_bw() + 
     labs(x = NULL, y = NULL) +
     geom_path(data = ros, aes(x = long, y = lat,linetype = "Mexican ducts"), alpha = 0.5) +
     geom_point( aes(x = -116.8476211, y = 31.9885162, color = "LNG terminal",shape="LNG terminal"), fill = "#CC79A7", size = 2) +
     geom_line(data = amp, aes(x = long, y = lat), alpha = 0.5) +
     geom_path(data = nbaja, aes(x = lat, y = long,linetype = "US ducts"), alpha = 0.5) +
     geom_path(data = sand, aes(x = long, y = lat), linetype = "longdash", alpha = 0.5) +
     geom_path(data = soca, aes(x = long, y = lat), linetype = "longdash", alpha = 0.5) +
     geom_point( aes(x = -115.428521, y = 32.669708), shape = 23, fill = "#000000") +
     geom_point( aes(x = -116.898, y = 32.5534), shape = 23, fill = "#000000") +
     geom_point( aes(x = -114.773898, y = 32.715056, shape = "Interconnectors"),  fill = "#000000") +
     geom_point(data = plants, aes(x = long, y = lat, color = "Power plants",shape = "Power plants"), fill = "#D55E00", alpha = 0.5, size = 1.5)+
     scale_color_manual(values = c("Power plants" = "#D55E00", "LNG terminal" = "#CC79A7", "Interconnectors" = "#000000", "Mexican ducts" = "#999999", "US ducts" = "#999999"),
                        labels = c("Power plants", "LNG terminal", "Interconnectors", "Mexican ducts", "US ducts"),
                        guide = guide_legend(override.aes = list(shape = NA, linetype = NA))) +
     
     scale_shape_manual(values = c("Power plants" = 21, "LNG terminal" = 15, "Interconnectors" = 23, "Mexican ducts" = NA, "US ducts" = NA),
                        labels = c("Power plants", "LNG terminal", "Interconnectors", "Mexican ducts", "US ducts"),
                        guide = guide_legend(override.aes = list(color = c("#D55E00", "#CC79A7", "#000000", "#999999", "#999999"),
                                                                 linetype = c(NA, NA, NA, "solid", "longdash")))) +
     guides(color = guide_legend(order = 1, title = "Legend"),
            shape = guide_legend(order = 2))

I get a legend where shape, colors, and lines are correctly identified but I can make it transform into just one legend. I want:

"LNG terminal" color = "#CC79A7" and shape=15;

"Power plants" color="#D55E00" and shape=21;

"Interconnectors" shape=23;

"Mexican ducts" color="#999999" and linetype="solid";

and US ducts color="#999999" and linetype="longdash" in a single legend.

enter image description here

Lilia
  • 109
  • 8
  • Since we don't have any sample data we can't run your code and test anything. The main idea though is that if you want a legend for something, it needs to be inside your aesthetics, `aes()`, and you can customize the result with the appropriate `scale_*()` function. See https://stackoverflow.com/questions/10349206/add-legend-to-ggplot2-line-plot for example. You'll need to move `color=`, `shape=`, and `linetype=` into an aes() on the appropriate layers. – MrFlick Aug 17 '23 at 16:58
  • My data is just geographic coordinates, and I'm using `aes` precisely the way it is asked in that post, for example, this part of my code: `geom_point(data = plants, aes(x = long, y = lat, color = "Power plants",shape = "Power plants"), fill = "#D55E00", alpha = 0.5, size = 1.5)` – Lilia Aug 17 '23 at 17:05
  • If you want to combine legends. you need to give them the same name. See https://stackoverflow.com/questions/12410908/combine-legends-for-color-and-shape-into-a-single-legend. `ggplot` doesn't just let you draw whatever you want. It needs to be data driven. Again, some sort of sample to test with would be helpful. – MrFlick Aug 17 '23 at 17:11

0 Answers0