隐藏 NAs,ggplot 条形图中的空条

Hide NAs, empty bars from bar-chart in ggplot

我有包含 2 个分类变量的数据框,并通过 ggplot 构建了条形图。唯一的问题是 Category: "Canada" 和 Category: "USA" 有不同的分类变量,这就是为什么在 y 轴上,如您所见,加拿大的 J、G 和美国的 F、B 是空白。

你能告诉我如何避免这个问题吗?另外,我想按正值降序排列加拿大和美国的金条

    library(tidyverse)
df <- data.frame (Origin = c("Canada", "Canada","Canada", "Canada","Canada", "Canada","USA","USA","USA","USA","USA","USA"),
                  Model = c("A","B","C","D","E","F","A","G","C","D","E","J"),
                  poschange = c(60, 45,34,56, 65, 44,40, 55, 35, 24,34,12),
                  negchange = c(-5,-2,-0.5,-2,-1,-0.05,-1,-3,-0.1,-3,-1.5,-0.9))

df2 <- df %>% pivot_longer(., cols=c('poschange','negchange'),
                           names_to = 'value_category') %>% 
  mutate(Groups = paste(Origin, Model),
         value_category = factor(value_category, levels = c("negchange", "poschange")))

my_color = c("A" = '#7fc97f', "B" = '#beaed4', "C" = '#fdc086', 
             "D" = '#ffff99', "E" = '#386cb0', "F" = '#f0027f', "white" = "white","G"="black","J"="blue")

ggplot(df2, aes(value, Model, 
                fill = ifelse(value_category == "negchange", "white", Model), 
                color = Model)) + 
  geom_col(position = "dodge") +
  scale_fill_manual(values = my_color, breaks = df2$Model) +
  scale_color_manual(values = my_color, breaks = df2$Model) +
  labs(fill = "Model") +
  facet_grid(Origin ~ ., switch = "y") +
  theme(axis.text.x = element_text(angle = 90),
        strip.background = element_rect(fill = "white"),
        strip.placement = "outside",
        strip.text.y.left = element_text(angle = 0),
        panel.spacing = unit(0, "lines"))

添加 scales = "free_yreorder_within:

library(tidyverse)
df <- data.frame (Origin = c("Canada", "Canada","Canada", "Canada","Canada", "Canada","USA","USA","USA","USA","USA","USA"),
                  Model = c("A","B","C","D","E","F","A","G","C","D","E","J"),
                  poschange = c(60, 45,34,56, 65, 44,40, 55, 35, 24,34,12),
                  negchange = c(-5,-2,-0.5,-2,-1,-0.05,-1,-3,-0.1,-3,-1.5,-0.9))

df2 <- df %>% pivot_longer(., cols=c('poschange','negchange'),
                           names_to = 'value_category') %>% 
  mutate(Groups = paste(Origin, Model),
         value_category = factor(value_category, levels = c("negchange", "poschange")),
         )

my_color = c("A" = '#7fc97f', "B" = '#beaed4', "C" = '#fdc086', 
             "D" = '#ffff99', "E" = '#386cb0', "F" = '#f0027f', "white" = "white","G"="black","J"="blue")

ggplot(df2, aes(value, tidytext::reorder_within(Model, value, Origin), 
                fill = ifelse(value_category == "negchange", "white", Model), 
                color = Model)) + 
  geom_col(position = "dodge") +
  scale_fill_manual(values = my_color, breaks = df2$Model) +
  scale_color_manual(values = my_color, breaks = df2$Model) +
  tidytext::scale_y_reordered() +
  labs(fill = "Model") +
  facet_grid(Origin ~ ., switch = "y", scales = "free_y") +
  theme(axis.text.x = element_text(angle = 90),
        strip.background = element_rect(fill = "white"),
        strip.placement = "outside",
        strip.text.y.left = element_text(angle = 0),
        panel.spacing = unit(0, "lines"))

reprex package (v2.0.1)

于 2022-05-03 创建