首页 文章

创建具有分组因子级别的ggplot

提问于
浏览
0

这是在这里提出的问题的变体:Group factor levels in ggplot .

我有一个数据帧:

df <- data.frame(respondent = factor(c(1, 2, 3, 4, 5, 6, 7)),
                 location = factor(c("California", "Oregon", "Mexico",
                                     "Texas", "Canada", "Mexico", "Canada")))

有三个与美国相关的单独级别 . 我不想破坏它们,因为状态之间的区别对数据分析很有用 . 然而,我希望有一个基本的条形图,它结合了美国的三个州并将它们叠加在一起,这样在条形图中有三个条形图 - 加拿大,墨西哥和美国 - 美国酒吧分开进入三个州如此:
Plot

如果状态因子级别的名称中包含“US”,例如“US:California”,我可以使用

library(tidyverse)
with_states <- df %>%
separate(location, into = c("Country", "State"), sep = ": ") %>%
  replace_na(list(State = "Other")) %>%
  mutate(State = as.factor(State)
         %>% fct_relevel("Other", after = Inf))

达到预期的结果 . 但是当R不知道这三个州在美国时,怎么办呢?

1 回答

  • 1

    如果查看前面的示例,所有 separatereplace_na 函数都将 location 变量分隔为 countrystate 变量:

    df
    
      respondent       location
    1          1 US: California
    2          2     US: Oregon
    3          3         Mexico
    ...
    
    df %>%
        separate(location, into = c("Country", "State"), sep = ": ") %>%
        replace_na(list(State = "Other"))
    
      respondent Country      State
    1          1      US California
    2          2      US     Oregon
    3          3  Mexico      Other
    ...
    

    因此,如果您的数据采用以下格式,您真的需要做的就是:使用国家/地区列和州/普罗旺斯列 .

    有很多方法可以自己做到这一点 . 很多时候,您的数据已经采用这种格式 . 如果不是,最简单的解决方法是连接到一个将位置映射到国家/地区的表格:

    df
      respondent   location
    1          1 California
    2          2     Oregon
    3          3     Mexico
    4          4      Texas
    5          5     Canada
    6          6     Mexico
    7          7     Canada
    
    state_mapping <- data.frame(state = c("California", "Oregon", "Texas"),
                                country = c('US', 'US', 'US'),
                                stringsAsFactors = F)
    
    df %>%
        left_join(state_mapping, by = c('location' = 'state')) %>%
        mutate(country = if_else(is.na(.$country),
                                 location,
                                 country))
    
    
      respondent   location country
    1          1 California      US
    2          2     Oregon      US
    3          3     Mexico  Mexico
    4          4      Texas      US
    5          5     Canada  Canada
    6          6     Mexico  Mexico
    7          7     Canada  Canada
    

    一旦你以这种格式获得它,你可以做其他问题建议的 .

相关问题