Secondary legend on chloropleth for points and polygons, ggplot

允我心安 提交于 2019-12-24 17:13:09

问题


I have a map with polygons and points on it - showing countries of interest in the world.

I want legends for both items (points and polygons), but can't add them. The polygon is plotted first (and so has a legend), while the points do not appear on the legend. To try and address this i add

show.legend = T

However the legend then adds the dots on top of the polygon colors as below:

What i want is another legend item with a yellow dot, where i can set the label as i want.

At the moment i am generating the points layer using a separate file. Perhaps i need to do this all from one df with points and polygons included - to generate the points and polygons from a single aes. But i can't think how to do this given my points have no group number.

Here is my code as it stands:

world <- map_data("world")
countries <- read_excel("country_table.xlsx", sheet = 3) #table of coutries with interest

world3 <- merge(world, countries, all.x = TRUE) %>%
  arrange(order)

world4 <- world3 %>%
  filter(!is.na(interest))

city <- read_excel("country_table.xlsx", sheet = 4) #point data
city$long <- as.numeric(city$long)
city$lat <- as.numeric(city$lat)

ggplot() + 
  geom_polygon(data = world3, aes(x = long, y = lat, group = group),
               fill = "light grey") +
  geom_polygon(data = world4, aes(x = long, y = lat, group = group, fill = interest),
               col = "white") +
  scale_fill_manual(name = "Interest/Support:",
                    breaks = c("interest", "past", "current"),
                    values = c(interest = "#a7ef88", past = "#3a7f1d", current = "#1b5104"), 
                    labels = c("interest", "past", "current")) +
  theme_map() +
  theme(legend.position = "bottom") +
  coord_fixed(xlim = c(-130, 160),
              ylim = c(-50, 75), 
              ratio = 1.4) +
  geom_point(data = city, aes(x= long, y = lat),
             shape = 21, inherit.aes = F, size = 2, col = "black", fill = "yellow", show.legend = T)

Any thoughts?


回答1:


Final code for the ggplot section posted below. Thanks to aosmith.

ggplot() + 
  #create base plot all polygons in grey
  geom_polygon(data = world3, aes(x = long, y = lat, group = group),
               fill = "light grey") +
  #create chloropleth layer for countries with data
  geom_polygon(data = world4, aes(x = long, y = lat, group = group, fill = interest),
               col = "white") +
  #add map theme from ggthemes
  theme_map() +
  #Set the zoom
  coord_fixed(xlim = c(-130, 160),
              ylim = c(-50, 75), ratio = 1.4) +
  #Add city layer - include col in aes() to get city as a separate legend item
  geom_point(data = city, aes(x= long, y = lat, col = interest),
             shape = 21, inherit.aes = F, size = 3, fill = "yellow") +
  #set fill for countries by interest (include city (special) to have the correct number of aesthetics)
  scale_fill_manual(name = NULL,
                    breaks = c("interest", "past", "current", "special"),
                    values = c(interest = "#a7ef88", past = "#3a7f1d", current = "#1b5104", special = "yellow"), 
                    labels = c("interest", "past", "current", "city")) +
  #set color for cities and labels for cities legend
  scale_color_manual(name = NULL, 
                     breaks = c("special"),
                     values = c(special = "black"),
                     labels = c("cities")) +
  #set order of legend items (fill first)
  guides(fill = guide_legend(order = 1),  color = guide_legend(order = 2)) +
  #set legend position and vertical arrangement
  theme(legend.text = element_text(size = 9), legend.position = "bottom", legend.box = "vertical")

Gives the following:



来源:https://stackoverflow.com/questions/51369515/secondary-legend-on-chloropleth-for-points-and-polygons-ggplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!