How to add an additional legend to the chart of a map in ggplot2 based on column records?

Data: Data

the code:

palette = brewer.pal(11,"RdYlGn")    # ColorBrewewr.org spectral palette, 11 colors
ggmap_byscen   = ggplot(wmap_byscen.df[wmap_byscen.df$variable !=c("AVG") &
                                     wmap_byscen.df$ID_1 !=c("0"),], aes(x=long, y=lat, group=group))
ggmap_byscen   = ggmap_byscen + geom_polygon(aes(fill=value)) + facet_wrap(~ variable)
ggmap_byscen   = ggmap_byscen + geom_path(colour="grey50", size=.1)
ggmap_byscen   = ggmap_byscen + geom_text(aes(x=c.long, y=c.lat, label=ID_1),size=5)
ggmap_byscen   = ggmap_byscen + scale_fill_gradientn(name="% Change",colours=palette)
ggmap_byscen   = ggmap_byscen + coord_fixed(xlim = longlimits, ylim = latlimits)
ggmap_byscen   = ggmap_byscen + scale_y_continuous(breaks=seq(-60,90,30), labels=c("60ºS","30ºS","0º","30ºN","60ºN","90ºN"))
ggmap_byscen   = ggmap_byscen + scale_x_continuous(breaks=seq(-180,180,45), labels=c("180ºW","135ºW","90ºW","45ºW","0º","45ºE","90ºE","135ºE","180ºE"))
ggmap_byscen   = ggmap_byscen + labs(x="",y="",title="Average yield impacts across all crops across\nby climate scenarios (% change)")
ggmap_byscen   = ggmap_byscen + theme(plot.title=element_text(size=rel(2), hjust=0.5, vjust=1.5, face="bold"),
                                  legend.text=element_text(size=17),
                                  legend.position="left",legend.text=element_text(size=rel(1.3)),
                                  legend.title=element_text(size=rel(1.4), hjust=0.5, vjust=1),
                                  panel.background = element_rect(fill = "white", colour = "gray95"),
                                  strip.text = element_text(size=18),
                                  axis.text.x = element_text(size=16),
                                  axis.text.y = element_text(size=16))
ggmap_byscen

Result: Map

Question: I want to add an additional legend defined by the "label" column in the dataframe to identify the region on the map. It is advisable that the legend be below the faceted map. I saw examples where you can add an entry to the table as a separate plot, and then combine the two. I could not figure out how to do this for my business.

Any help would be great, thanks.

+4
source share
1 answer

@jlhoward, longlimits latlimits . coord_fixed(xlim = longlimits, ylim = latlimits) . , , . , , . colour geom_text, , , a . , geom_point alpha = 0, colour aes. , , . annotate . @jlhoward , annotate(). , R 4000 . legend.key = element_rect(fill = NA), . , . . , .

library(dplyr)
library(ggplot2)

wmap_byscen.df <- read.csv("mydata.csv", header = T)

mydf <- wmap_byscen.df[wmap_byscen.df$variable != c("AVG") &
        wmap_byscen.df$ID_1 != c("0"),]

### This is for annotate()

mydf2 <- select(mydf, c.long, c.lat, ID_1, ID_name) %>%
         distinct()

### Color setting

palette = brewer.pal(11,"RdYlGn")


ggplot(mydf, aes(x = long, y = lat, group = group)) +
geom_polygon(aes(fill = value)) + 
facet_wrap(~ variable) +
geom_path(colour = "grey50", size = .1) +
geom_point(aes(x = c.long, y = c.lat, color=factor(ID_name, levels=unique(ID_name)), label = ID_1), size = 1, alpha = 0) +
annotate("text", x = mydf2$c.long, y = mydf2$c.lat, label = mydf2$ID_1) +
scale_fill_gradientn(name = "% Change",colours = palette) +
scale_color_discrete(name = "Regions") +
#coord_fixed(xlim = longlimits, ylim = latlimits) +
scale_y_continuous(breaks = seq(-60,90,30), labels = c("60ºS","30ºS","0º","30ºN","60ºN","90ºN")) +
scale_x_continuous(breaks = seq(-180,180,45), labels = c("180ºW","135ºW","90ºW","45ºW","0º","45ºE","90ºE","135ºE","180ºE")) +
labs(x = "",y = "",title = "Average yield impacts across all crops across\nby climate scenarios (% change)") +
theme(plot.title = element_text(size = rel(2), hjust = 0.5, vjust = 1.5, face = "bold"),
      legend.text = element_text(size = 8),
      legend.position = "bottom",
      legend.text = element_text(size = rel(1.3)),
      legend.title = element_text(size = rel(1.4), hjust = 0.5, vjust = 1),
      panel.background = element_rect(fill = "white", colour = "gray95"),
      strip.text = element_text(size = 18),
      axis.text.x = element_text(size = 16),
      axis.text.y = element_text(size = 16),
      legend.key = element_rect(fill = NA)) +
guides(col = guide_legend(nrow = 3, byrow = TRUE)) 

enter image description here

+3

All Articles