Create a unique legend based on a contingency (2x2) table in geom_map or ggplot2?

风格不统一 提交于 2019-12-23 09:57:18

问题


How can I do this based on this contingency table? I'm not entirely sure how to create a custom legend in R based on the indicator table I made (crimes).

Reproducible code in R:

       require(maps)

  set.seed(123)
  # randomly assign 2 variables to each state
  mappingData <- data.frame(state = tolower(rownames(USArrests)), 
                       iceCream = (sample(c("Likes Ice Cream","Doesn't Like Ice Cream"),50, replace=T)),
                       hotDogs = (sample(c("Likes Hot Dogs","Doesn't Like Hot Dogs"),50, replace=T)))

  # create a 'legend' key for an indicator variable
  mappingDataDF<-data.frame(
    expand.grid(iceCream=c("Likes Ice Cream","Doesn't Like Ice Cream"),
                        hotDogs=c("Likes Hot Dogs","Doesn't Like Hot Dogs")),
                        indicator=c("0","1","2","3")) 

  mappingData<-mappingData %>% inner_join(mappingDataDF)

  mappingDatam <- reshape2::melt(mappingData, id = 1)

  states_map <- map_data("state")

  ggplot(mappingData, aes(map_id = state)) +
    geom_map(aes(fill = indicator), map = states_map) +
    expand_limits(x = states_map$long, y = states_map$lat)

回答1:


I altered some of your data setup to simplify the example.

library(maps)
library(dplyr)
library(ggplot2)

set.seed(123)
# randomly assign 2 variables to each state
mappingData <- data.frame(state = tolower(rownames(USArrests)), 
                          iceCream = (sample(c("No", "Yes"), 50, replace=T)),
                          hotDogs = (sample(c("No", "Yes"), 50, replace=T))) %>% 
  mutate(indicator = interaction(iceCream, hotDogs, sep = ":"))

mappingData
            state iceCream hotDogs indicator
1         alabama       No      No     No:No
2          alaska      Yes      No    Yes:No
3         arizona       No     Yes    No:Yes
4        arkansas      Yes      No    Yes:No
...
states_map <- map_data("state")

Generate an independent legend from the data

legend_ic.hd <- ggplot(mappingData, aes(iceCream, hotDogs, fill = indicator)) +
  geom_tile(show.legend = F) + 
  scale_x_discrete("Ice cream?", expand = c(0,0)) +
  scale_y_discrete("Hot dogs?", expand = c(0,0)) + 
  theme_minimal() +
  theme(axis.text.y = element_text(angle = 90, hjust = 0.5)) +
  coord_equal()

legend_ic.hd

Then use it as a custom annotation in the original map

ggplot(mappingData, aes(map_id = state)) +
  geom_map(aes(fill = indicator), map = states_map, show.legend = F) +
  expand_limits(x = states_map$long, y = states_map$lat) +
  coord_quickmap() +
  annotation_custom(grob = ggplotGrob(legend_ic.hd), 
                    xmin = -79, xmax = Inf,
                    ymin = -Inf, ymax = 33)

You have to adjust the location of the annotation manually, or:

Use gridExtra (or cowplot):

plot_ic.hd <- ggplot(mappingData, aes(map_id = state)) +
  geom_map(aes(fill = indicator), map = states_map, show.legend = F) +
  expand_limits(x = states_map$long, y = states_map$lat) +
  coord_quickmap()

gridExtra::grid.arrange(grobs = list(plot_ic.hd, legend_ic.hd), 
                        ncol = 2, widths = c(1,0.33))




回答2:


One approach would be to create a PNG file of your custom legend, then add it to the plot using annotation_raster:

library(png)
legend <- readPNG("full/path/to/legend.png")

ggplot(mappingData, aes(map_id = state)) +
  geom_map(aes(fill = indicator), map = states_map) +
  expand_limits(x = states_map$long, y = states_map$lat) +
  guides(fill = FALSE) + 
  annotation_raster(legend,
                    xmin = -75, 
                    xmax = -65, 
                    ymin = 25, 
                    ymax = 30,
                    interpolate = TRUE)



来源:https://stackoverflow.com/questions/46063337/create-a-unique-legend-based-on-a-contingency-2x2-table-in-geom-map-or-ggplot2

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