簡體   English   中英

在 ggplot 中隱藏特定的小平面軸標簽

[英]Hide specific facet axis labels in ggplot

你會看到下面的代碼,我最終得到了一個很好的分面 plot 看起來我需要它,但我想要的是隱藏所有分面的 y 軸標簽,除了最左邊的那些。 所以隱藏 facet 2、3、4、6 和 7 的標簽。這樣我就只剩下每行最左邊的“White”、“Black”和“Hispanic”(我可以稍后清理 prefix_ ). 有任何想法嗎?

d2 %>% 
ggplot(., aes(x = var_new, y = coef,
              ymin = ci_lower, ymax = ci_upper)) +
  geom_point(color = "red") +
  geom_errorbar(width = 0,
                size = 1,
                color = "red") +
  facet_wrap(~model,
             nrow = 2,
             scales = "free") +
  geom_hline(yintercept = 0, linetype = "dashed", color = "black", size = .3) +
  coord_flip() +
  theme_minimal(base_size = 10) +
  theme(legend.position = "none")
structure(list(model = c(7, 6, 5, 7, 6, 5, 7, 6, 5, 4, 3, 4, 
3, 4, 3, 2, 1, 2, 1, 2, 1), race = c("hispanic", "hispanic", 
"hispanic", "black", "black", "black", "white", "white", "white", 
"hispanic", "hispanic", "black", "black", "white", "white", "hispanic", 
"hispanic", "black", "black", "white", "white"), var_new = c("ela_hispanic", 
"math_hispanic", "sci_hispanic", "ela_black", "math_black", "sci_black", 
"ela_white", "math_white", "sci_white", "after_hispanic", "before_hispanic", 
"after_black", "before_black", "after_white", "before_white", 
"part_hispanic", "full_hispanic", "part_black", "full_black", 
"part_white", "full_white"), coef = c(0.91, 0.2615005, -0.0622102, 
3.1966945, 0.9665615, 0.4419779, -4.1608082, -1.75, -3.4185874, 
-1.72661788, -1.87514649, 0.61605887, 0.58634364, 0.87, 0.4, 
1.52820746, 1.35976557, 1.08885352, 0.8323809019, 0.728991331, 
1.53140561), ci_lower = c(0.3, -1.04316665, -1.68479242, -1.0382233, 
-0.70264707, -1.29579134, -12.008101, -3, -6.4522842, -1.9858909, 
-2.10047863, 0.41173674, 0.37007869, -0.3428254, -0.1, 1.21339829, 
1.07813362, 0.778488586, 0.44183285, 0.30081336, 0.98770764), 
    ci_upper = c(1.2, 1.748, 1.560372, 7.4316126, 2.63577, 2.179747, 
    3.6864845, 0.01, -0.3848905, -1.467344828, -1.64981433, 0.8203809961, 
    0.802608596, 0.4, 0.8, 1.8430166, 1.64139752, 1.39921842, 
    1.22292898, 1.15716932, 2.0751036)), row.names = c(NA, -21L
), class = c("tbl_df", "tbl", "data.frame"))

我不明白為什么人們繼續切換 x 和 y 軸變量,然后使用coord_flip使它們以正確的方式旋轉。 這是令人困惑的、不必要的,並且需要更多代碼。 最好以正確的方式放置變量並保持coord不變。

完成后,最簡單的解決方案是將race放在 y 軸上,並將比例更改為free_x 我在每個面板周圍添加了邊框以使事情更清晰。

library(tidyverse)

ggplot(d2, aes(y = race, x = coef, xmin = ci_lower, xmax = ci_upper)) +
  geom_errorbar(width = 0, linewidth = 1.5, color = "red3", alpha = 0.5) +
  geom_point(shape = 21, fill = "red2", size = 3, color = 'white') +
  facet_wrap(~ model, nrow = 2, scales = 'free_x') +
  geom_vline(xintercept = 0, linetype = "dashed", linewidth = 0.3) +
  theme_minimal(base_size = 14) +
  theme(legend.position = "none",
        panel.grid.major.y = element_blank(), 
        panel.border = element_rect(color = 'gray75', fill = NA))

在此處輸入圖像描述

如果你想在 facet 標題中包含前綴(因為它們與model有 1:1 的對應關系),你可以使用tidyr::separate

d2 %>% 
  separate(var_new, into = c('model_name', 'race')) %>%
  mutate(model = paste(model, model_name, sep = ' - ')) %>%
  ggplot(aes(y = race, x = coef, xmin = ci_lower, xmax = ci_upper)) +
  geom_errorbar(width = 0, linewidth = 1.5, color = "red3", alpha = 0.5) +
  geom_point(shape = 21, fill = "red2", size = 3, color = 'white') +
  facet_wrap(~model, nrow = 2, scales = 'free_x') +
  geom_vline(xintercept = 0, linetype = "dashed", linewidth = 0.3) +
  theme_minimal(base_size = 14) +
  theme(legend.position = "none",
        panel.grid.major.y = element_blank(), 
        panel.border = element_rect(color = 'gray75', fill = NA))

在此處輸入圖像描述


附錄

要像這樣比較跨組的系數,通常最好將它們全部放在一個線性范圍 plot 中(類似於森林圖)。 我認為這提供了更好的可視化效果,需要讀者更少的認知努力。 這也顯示了coord_flip的一個很好的用例,即當您想要在組之間進行垂直閃避時。

d2 %>% 
  separate(var_new, into = c('model_name', 'race')) %>%
  mutate(model = paste0('Model ', model, ' : ', model_name)) %>%
  ggplot(aes(x = model, y = coef, ymin = ci_lower, ymax = ci_upper,
             color = race)) +
  annotate("segment", y = rep(-Inf, 3), yend = rep(Inf, 3),
           x = c('Model 2 : part', 'Model 4 : after', 'Model 6 : math'), 
           xend =  c('Model 2 : part', 'Model 4 : after', 'Model 6 : math'), 
           linewidth = 22, alpha = 0.05) +
  coord_flip() +
  geom_errorbar(width = 0, linewidth = 1, alpha = 0.5, 
                position = position_dodge(width = 0.5)) +
  geom_point(size = 1.5, position = position_dodge(width = 0.5)) +
  geom_hline(yintercept = 0, linetype = "dashed", color = "black",
             linewidth = 0.3) +
  scale_color_brewer(palette = 'Set1') +
  theme_minimal(base_size = 14) +
  guides(color = guide_legend(reverse = TRUE)) +
  theme(panel.grid.major.y = element_blank(), 
        panel.border = element_rect(color = 'gray75', fill = NA),
        axis.text.y = element_text(hjust = 0))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM