【问题标题】:Create a unique legend based on a contingency (2x2) table in geom_map or ggplot2?根据 geom_map 或 ggplot2 中的列联 (2x2) 表创建一个独特的图例?
【发布时间】:2018-02-14 05:50:27
【问题描述】:

如何根据此列联表执行此操作?我不完全确定如何根据我制作的指标表(犯罪)在 R 中创建自定义图例。

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)

【问题讨论】:

    标签: r ggplot2 rstudio gis tidyverse


    【解决方案1】:

    我更改了您的一些数据设置以简化示例。

    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")
    

    根据数据生成独立图例

    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
    

    然后在原图中作为自定义注解使用

    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)
    

    您必须手动调整注释的位置,或者:

    使用gridExtra(或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))
    

    【讨论】:

    • 是的 - 这非常有效!非常感谢,Brian - 简化了我的代码和令人敬畏的传奇。
    【解决方案2】:

    一种方法是创建自定义图例的 PNG 文件,然后使用 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)
    

    【讨论】:

    • 这也太棒了!这是包含图例的一种很棒的简单方法。我也试试这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 1970-01-01
    • 2017-11-27
    相关资源
    最近更新 更多