rggplot2legendspatialgeom-sf

Add custom legend in ggplot map


I face difficulties to add a custom legend on the Sardinia Island map below. This legend should include both color lines (NUTS2 and NUTS3 borders) and color points which represent the initiation points from where I performed atmospehir simulations.

Here is the code for the Sardinia map construction with ggplot2:

# gets NUTS2 & NUTS3 regions of Italy
nuts2_italy <- gisco_get_nuts(year="2021",nuts_level = 2,country = "Italy")
sardinia_nuts2 <- nuts2_italy[nuts2_italy$NUTS_NAME=='Sardegna',]

nuts3_italy <- gisco_get_nuts(year="2021",nuts_level = 3,country = "Italy")

# load initiation point map
start_pt<- st_read("data/HYSPLIT_start_points/start_points.shp")

# map starting points and NUTS boundaries of Sardinia
install.packages("ggspatial")
library(ggspatial)


map<-ggplot(sardinia_nuts2) +
  geom_sf(data=nuts3_italy,fill = "antiquewhite", color = "#887e6a" ,show.legend = "line")+
  geom_sf(data=sardinia_nuts2, fill = NA ,color = "blue",show.legend = "line") +
  geom_sf(data=start_pt,color="red", size=1,show.legend = "points")+
    coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
  
  
  annotation_scale(location = "bl", width_hint = 0.5,text_family = "serif",bar_cols = c("#887e6a", "#fffff3")) +
  
  annotation_north_arrow(location = "br", which_north = "true", 
                         pad_x = unit(0.75, "cm"), pad_y = unit(0.2, "cm"),
                         style = north_arrow_fancy_orienteering(
                           fill = c("#887e6a", "#fffff3"),
                           line_col = "#887e6a",
                           text_family = "serif"
                         ))+
  
  annotate(geom = "text", x = 9, y = 40.7, label = "Sardinia", 
           fontface = "italic", size = 5, fontface = "bold",
           family = "serif",color="#887e6a" )+
  
 
  labs(x="Longitude", y="Latitude") +
  
   theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5), 
       panel.background = element_rect(fill = "aliceblue"),
        panel.border = element_rect(colour = "#887e6a", fill = NA,size = 1.5),
       axis.text=element_text(family = "serif",colour = "#887e6a")) 
  

I obtain this map

enter image description here

I tried to add "legend" in my ggplot map as this:

map<-ggplot(sardinia_nuts2) +
  geom_sf(data=nuts3_italy,fill = "antiquewhite", color = "#887e6a" ,show.legend = "line")+
  geom_sf(data=sardinia_nuts2, fill = NA ,color = "blue",show.legend = "line") +
  geom_sf(data=start_pt,color="red", size=1,show.legend = "points")+
    coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +
  
  
  annotation_scale(location = "bl", width_hint = 0.5,text_family = "serif",bar_cols = c("#887e6a", "#fffff3")) +
  
  annotation_north_arrow(location = "br", which_north = "true", 
                         pad_x = unit(0.75, "cm"), pad_y = unit(0.2, "cm"),
                         style = north_arrow_fancy_orienteering(
                           fill = c("#887e6a", "#fffff3"),
                           line_col = "#887e6a",
                           text_family = "serif"
                         ))+
  
  annotate(geom = "text", x = 9, y = 40.7, label = "Sardinia", 
           fontface = "italic", size = 5, fontface = "bold",
           family = "serif",color="#887e6a" )+
  
 
  labs(x="Longitude", y="Latitude") +
  
   theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5), 
       panel.background = element_rect(fill = "aliceblue"),
        panel.border = element_rect(colour = "#887e6a", fill = NA,size = 1.5),
       axis.text=element_text(family = "serif",colour = "#887e6a")) +
  
  
 
legend('topright', cex=1.2, legend=c('NUTS2 border', 'NUTS3 borders','study grid', 'Initiation points'),
lty = c(1, 1, 2, NA), # tell are which objects to be drawn as a line in the legend
pch=c(NA, NA, NA, 21), # tell are which objects to be drawn as a symbol
col=c('black', 'black', 'black', 'black'), # color of the legend
pt.bg=c('blue', "#887e6a", "#887e6a", "red"), # color of the symbol
bty='n')  #turn off the legend BORDER



map

Actually this doesn't work and I obtained: Error: (converted from warning) Duplicated aesthetics after name standardisation: fontface

The intention is to obtain a legend with

Any idea to help me? Thanks!


Solution

  • Here the full code and the map legend thanks to the help of @stefan

     map <-ggplot() +
      geom_sf(data=nuts3_italy, aes(color = "nuts3" ), fill = "antiquewhite", show.legend = "line")+
      geom_sf(data=sardinia_nuts2, aes(color = "nuts2"),fill= NA, show.legend = "line") +
      geom_sf(data=start_pt,aes(color="points"),show.legend = "point")+
        coord_sf(xlim = c(7.5, 10.5), ylim = c(38.7, 41.3), expand = TRUE) +  
       scale_color_manual(values = c(points = "red", nuts3= "#887e6a", nuts2 = "blue"),
                          label = c(points = 'Initiation points',
                                    nuts3 = 'NUTS3 ',
                                    nuts2 = 'NUTS2'))+  
      annotation_scale(location = "br", 
                       width_hint = 0.5,
                       text_family = "serif",
                       bar_cols = c("#887e6a", "#fffff3")
                       ) +  
      annotation_north_arrow(location = "tr", which_north = "true", 
                             pad_x = unit(0.75, "cm"), pad_y = unit(0.2, "cm"),
                             style = north_arrow_fancy_orienteering(
                               fill = c("#887e6a", "#fffff3"),
                               line_col = "#887e6a",
                               text_family = "serif"
                             ))+  
      annotate(geom = "text", x = 9, y = 40.7, label = "Sardinia", 
               fontface = "italic", size = 5, fontface = "bold",
               family = "serif",color="#887e6a" )+
      labs(x="Longitude", y="Latitude") +  
       theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5), 
           panel.background = element_rect(fill = "aliceblue"),
            panel.border = element_rect(colour = "#887e6a", fill = NA,size = 1.5),
           axis.text=element_text(family = "serif",colour = "#887e6a"),
           legend.key = element_rect(fill = "white"),
           legend.title=element_blank()) +
        guides(colour= guide_legend(override.aes = list(shape= c(NA, NA, 19),linetype=c(1,1,NA))))
    

    Here is the final map with the legend: enter image description here