使用facet_wrap将ggplot中的所有面以0为中心

bogh5gae  于 2023-01-28  发布在  其他
关注(0)|答案(2)|浏览(136)

我有一个很简单的问题。
我正在使用facet_wrap()将3个图堆叠在一起。
我希望所有3个图都以0为中心,这样hline就都在彼此的正上方。理想情况下,同时还允许它们的比例根据需要沿着x-axis自由变化(我已经在scales = "free"上做了)。
有什么想法吗?参见下面的示例代码和数据

d_example %>% 
  ggplot(aes(x = var, y = coef, 
             ymin = ci_lower, ymax = ci_upper,
             color = race)) +
  geom_point(position = position_dodge(width = 0.7),
             size = 3) +
  geom_errorbar(width = 0, 
                size = 1.1, 
                alpha = 0.6, 
                position = position_dodge(width = 0.7)) +
  facet_wrap(~ outcome, 
             nrow = 3,
             scales = "free") +
  coord_flip() +
  geom_hline(yintercept = 0, 
             linetype = "dashed", 
             color = "black",
             size = .3) +
  theme_minimal()
d_example <- structure(list(var = c("score_A", "score_B", "score_C", "score_A", 
"score_B", "score_C", "score_A", "score_B", "score_C", "friends_A", 
"friends_B", "friends_A", "friends_B", "friends_A", "friends_B", 
"poverty_A", "poverty_B", "poverty_A", "poverty_B", "poverty_A", 
"poverty_B"), coef = c(-0.8, -0.7, 0.12, -0.7, -0.9, -0.05, -0.73, 
-1, 0.02, 0.55, -0.09, 0.51, 0.2, 0.65, 0.05, 1.8, 0.34, 1.103, 
0.077, 1.02, 0.06), race = c("white", "white", "white", "black", 
"black", "black", "hispanic", "hispanic", "hispanic", "white", 
"white", "black", "black", "hispanic", "hispanic", "white", "white", 
"black", "black", "hispanic", "hispanic"), outcome = c("score", 
"score", "score", "score", "score", "score", "score", "score", 
"score", "friends", "friends", "friends", "friends", "friends", 
"friends", "ses", "ses", "ses", "ses", "ses", "ses"), ci_lower = c(-1, 
-0.9, -0.08, -0.9, -1.1, -0.25, -0.93, -1.2, -0.18, 0.35, -0.29, 
0.31, 0, 0.45, -0.15, 1.6, 0.14, 0.903, -0.123, 0.82, -0.14), 
    ci_upper = c(-0.6, -0.5, 0.32, -0.5, -0.7, 0.15, -0.53, -0.8, 
    0.22, 0.75, 0.11, 0.71, 0.4, 0.85, 0.25, 2, 0.54, 1.303, 
    0.277, 1.22, 0.26)), class = c("spec_tbl_df", "tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -21L), spec = structure(list(
    cols = list(var = structure(list(), class = c("collector_character", 
    "collector")), coef = structure(list(), class = c("collector_double", 
    "collector")), race = structure(list(), class = c("collector_character", 
    "collector")), outcome = structure(list(), class = c("collector_character", 
    "collector")), ci_lower = structure(list(), class = c("collector_double", 
    "collector")), ci_upper = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1L), class = "col_spec"))
nx7onnlm

nx7onnlm1#

这里有一个简单而又滑稽的答案,它给每个面一个自由范围,同时保持中心为零:
添加一个带有反向y的不可见层,使每个面都围绕0自然对称。

geom_errorbar(aes(y = -coef, ymin = -ci_lower, ymax = -ci_upper),
              alpha = 0,
              position = position_dodge(width = 0.7)) +

或者,更好的方法是使用@r2evans建议的geom_blank,它是专门为您希望绘图为某些数据点腾出空间,而不浪费实际绘制它们的周期或字节的情况而设计的。https://ggplot2.tidyverse.org/reference/geom_blank.html

geom_blank(aes(y = -coef, ymin = -ci_lower, ymax = -ci_upper)) +

lp0sw83n

lp0sw83n2#

我在下面添加了scale_y_continuous(limits=);这允许容易地对准Hline:

d_example %>%
  ggplot(aes(x = var, y = coef,
             ymin = ci_lower, ymax = ci_upper,
             color = race)) +
  geom_point(position = position_dodge(width = 0.7),
             size = 3) +
  geom_errorbar(width = 0,
                size = 1.1,
                alpha = 0.6,
                position = position_dodge(width = 0.7)) +
  facet_wrap(~ outcome,
             nrow = 3,
             scales = "free") +
  coord_flip() +
  geom_hline(yintercept = 0,
             linetype = "dashed",
             color = "black",
             linewidth = .3) +
  scale_y_continuous(limits = range(unlist(d_example[,c("coef","ci_lower","ci_upper")]))) +
  theme_minimal()

如果要真正居中,请使用以下命令:

scale_y_continuous(limits = c(-1,1) * max(abs(range(unlist(d_example[,c("coef","ci_lower","ci_upper")])))))

相关问题