rm(list = ls())
library(ggplot2)
map <- map_data("world")
map$value <- setNames(sample(-50:50, length(unique(map$region)), TRUE), 
                      unique(map$region))[map$region]
map[map$region == "Russia", "value"] <- NA
###################################
map$discrete_value = cut(map$value, breaks=seq(from=-50, to=50, length.out=8))

p = ggplot() +
  geom_polygon(data=map, aes(long, lat, group=group, fill=discrete_value)) +
  scale_fill_brewer(palette="RdYlBu", na.value="black") +
  coord_quickmap()
p

#ggsave("map.png", plot=p, width=10, height=5, dpi=150)   
##################################
p2 <- ggplot() +
  geom_polygon(data=map, aes(long, lat, group=group, fill=value, colour="")) +
  scale_fill_gradient2(name = "AA", low="brown3", mid="cornsilk1", high="turquoise4",
                       limits=c(-50, 50), na.value="darkgreen") +
  scale_colour_manual(values= "red")               
p2

p3 <- p2 + 
  guides(colour = guide_legend("Missing value", override.aes = list(colour="darkred",fill="darkgreen")))
p3

#ggsave("map2.png", plot=p2, width=10, height=5, dpi=150) 

#ref https://stackoverflow.com/questions/42365483/add-a-box-for-the-na-values-to-the-ggplot-legend-for-a-continous-map
#https://www.edureka.co/community/52697/change-the-order-of-multiple-legends-in-ggplot
#https://aosmith.rbind.io/2020/07/09/ggplot2-override-aes/