简体   繁体   English

在 ggplot 中隐藏特定的小平面轴标签

[英]Hide specific facet axis labels in ggplot

you'll see with the code below that I end up with a nicely faceted plot that looks how I need it, but all I want is to hide the y axis labels for all facets except the ones on the far left.你会看到下面的代码,我最终得到了一个很好的分面 plot 看起来我需要它,但我想要的是隐藏所有分面的 y 轴标签,除了最左边的那些。 So hide labels for facet 2, 3, 4, 6, and 7. That way I am just left with "White", "Black", and "Hispanic" on the far left of each row (I can clean up the prefix_ later).所以隐藏 facet 2、3、4、6 和 7 的标签。这样我就只剩下每行最左边的“White”、“Black”和“Hispanic”(我可以稍后清理 prefix_ ). Any ideas?有任何想法吗?

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

I don't understand why folks continue to switch the x and y axis variables then use coord_flip to put them round the right way.我不明白为什么人们继续切换 x 和 y 轴变量,然后使用coord_flip使它们以正确的方式旋转。 This is confusing, unnecessary, and requires more code.这是令人困惑的、不必要的,并且需要更多代码。 It's best to just put the variables round the right way and keep the coord as-is.最好以正确的方式放置变量并保持coord不变。

Once that's done, the simplest solution is to put race on the y axis, and change scales to free_x .完成后,最简单的解决方案是将race放在 y 轴上,并将比例更改为free_x I've added a border around each panel to make things a bit clearer.我在每个面板周围添加了边框以使事情更清晰。

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

在此处输入图像描述

If you want to include the prefixes in the facet titles (since they have a 1:1 correspondence with model ), you could use tidyr::separate :如果你想在 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))

在此处输入图像描述


Addendum附录

To compare coefficients across groups like this, it is normally better to put them all in a single linerange plot (similar to a forest plot).要像这样比较跨组的系数,通常最好将它们全部放在一个线性范围 plot 中(类似于森林图)。 I think this provides a much better visualization that requires less cognitive effort from the reader.我认为这提供了更好的可视化效果,需要读者更少的认知努力。 This also shows a good use-case for coord_flip , namely when you want a vertical dodge between groups.这也显示了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