【问题标题】:How to add ggplot2 gridlines or color to show multiple plot points by variables (y-axis)?如何添加 ggplot2 网格线或颜色以按变量(y 轴)显示多个绘图点?
【发布时间】:2021-06-02 21:36:16
【问题描述】:

我正在创建一个图表,显示每个变量的 4 个结果 beta + CI 线。

我想更清楚地表明 4 个绘制点(结果)和线 (CI) 与每个变量相关。

因此,我很好奇如何 1) 制作更清晰的面板或网格更改以显示哪 4 个结果与哪个变量相关或 2) 在 y 轴上围绕变量创建括号?

代码和下图。

library(tidyverse)
library(viridis)
#> Loading required package: viridisLite

df <- tribble(
  ~var, ~gender, ~outcome, ~est, ~l95, ~u95,
  "A", "Men", "X", 0.2, 0.1, 0.3,
  "A", "Men", "Y", 0.3, 0.25, 0.35,
  "B", "Men", "X", -0.4, -0.5, -0.3,
  "B", "Men", "Y", -0.45, -0.5, -0.4,
  "A", "Women", "X", 0.4, 0.3, 0.5,
  "A", "Women", "Y", 0.6, 0.55, 0.65,
  "B", "Women", "X", -0.1, -0.2, 0,
  "B", "Women", "Y", -0.3, -0.4, -0.2,
  "A", "Men", "Z", 0.2, 0.1, 0.3,
  "A", "Men", "AA", 0.3, 0.25, 0.35,
  "B", "Men", "Z", -0.4, -0.5, -0.3,
  "B", "Men", "AA", -0.45, -0.5, -0.4,
  "A", "Women", "Z", 0.4, 0.3, 0.5,
  "A", "Women", "AA", 0.6, 0.55, 0.65,
  "B", "Women", "Z", -0.1, -0.2, 0,
  "B", "Women", "AA", -0.3, -0.4, -0.2
)

df %>%
  ggplot(aes(x=est, y=var, color=outcome)) +
  geom_point(position = position_dodge2(width = 1.0)) +
  scale_y_discrete(limits=rev, expand=c(0, 0)) +
  scale_color_viridis_d() +
  geom_linerange(aes(xmin=l95, xmax=u95),
    position = position_dodge2(width=1.0)) +
  geom_vline(xintercept = 0, linetype = "dashed") +
  facet_grid(~ gender) +
  theme_minimal() +
  theme(plot.title.position = "plot",
    plot.title = element_text(face="bold"),
    legend.position = "top",
    strip.text = element_text(face="bold")) +
  labs(y = NULL,
    x = "Standardized Beta",
    title = "My cool plot shows this thing",
    subtitle = "More important details, 95% confidence intervals",
    caption = "Even more important details")

reprex package (v2.0.0) 于 2021 年 6 月 3 日创建

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    我想我会在gender var 上使用outcome 在实际的y 轴上。在我看来,每个面板周围的小灰线也提高了清晰度:

    df %>%
      ggplot(aes(x=est, y=outcome, color=outcome)) +
      geom_point(position = position_dodge2(width = 1.0)) +
      scale_y_discrete(limits=rev, expand=c(0, 0)) +
      scale_color_viridis_d() +
      geom_linerange(aes(xmin=l95, xmax=u95),
        position = position_dodge2(width=1.0)) +
      geom_vline(xintercept = 0, linetype = "dashed") +
      facet_grid(var ~ gender, switch = "y") +
      theme_minimal() +
      theme(plot.title.position = "plot",
        plot.title = element_text(face="bold"),
        legend.position = "top",
        strip.placement = "outside",
        strip.text = element_text(face="bold"),
        panel.background = element_rect(color = "gray50")) +
      labs(y = NULL,
        x = "Standardized Beta",
        title = "My cool plot shows this thing",
        subtitle = "More important details, 95% confidence intervals",
        caption = "Even more important details")
    

    reprex package (v0.3.0) 于 2021-06-02 创建

    【讨论】:

    • 谢谢艾伦!这很有帮助。我会试试这个,我想试着让线条更接近一点,以便通过变量比较 beta 强度。
    【解决方案2】:

    您可以管理的主题属性的其他信息包括

    • panel.spacing - facet 中网格之间的空间
    • panel.border - facet 中网格的边框
    • strip.background - 构面标题周围的边框
    df %>%
      # change y axis to outcome
      ggplot(aes(x=est, y = outcome, color = outcome)) +
      geom_point(position = position_dodge2(width = 1.0)) +
      scale_y_discrete(limits=rev, expand=c(0, 0)) +
      scale_color_viridis_d() +
      geom_linerange(aes(xmin=l95, xmax=u95),
        position = position_dodge2(width=1.0)) +
      geom_vline(xintercept = 0, linetype = "dashed") +
      # facet grid by var & gender
      facet_grid(var ~ gender) +
      theme_minimal() +
      theme(plot.title.position = "plot",
        plot.title = element_text(face="bold"),
        legend.position = "top",
        strip.text = element_text(face="bold")) +
      labs(y = NULL,
        x = "Standardized Beta",
        title = "My cool plot shows this thing",
        subtitle = "More important details, 95% confidence intervals",
        caption = "Even more important details") +
      # add the panel border around each facet
      theme(panel.spacing = unit(5, "mm"),
        panel.border = element_rect(color = "black", fill = NA, size = 1), 
        strip.background = element_rect(color = "black", size = 1))
    

    reprex package (v2.0.0) 于 2021 年 6 月 3 日创建

    另一种不使用 facet 的方法,尽管它可能需要一些额外的技巧来减少 var 之间的空间

    df %>%
      ggplot(aes(x=est, y=var, color=outcome)) +
      geom_point(position = position_dodge2(width = 1.0)) +
      # add a middle discrete value for y-axis "C" then draw a horizontal line
      # on x
      scale_y_discrete(limits=c("B", "C", "A"), expand=c(0, 0),
        breaks = c("B", "A")) +
      geom_hline(yintercept = "C", linetype = "44") +
      scale_color_viridis_d() +
      geom_linerange(aes(xmin=l95, xmax=u95),
        position = position_dodge2(width=1.0)) +
      geom_vline(xintercept = 0, linetype = "dashed") +
      facet_grid(~ gender) +
      theme_minimal() +
      theme(plot.title.position = "plot",
        plot.title = element_text(face="bold"),
        legend.position = "top",
        strip.text = element_text(face="bold")) +
      labs(y = NULL,
        x = "Standardized Beta",
        title = "My cool plot shows this thing",
        subtitle = "More important details, 95% confidence intervals",
        caption = "Even more important details") +
      theme(panel.spacing = unit(0, "mm"),
        panel.border = element_rect(color = "black", fill = NA, size = 1))
    

    reprex package (v2.0.0) 于 2021-06-04 创建

    【讨论】:

    • 谢谢!我想尝试一种方法来做到这一点,而不需要考虑变量,否则我会得到太多面板:)
    • 刚刚为var添加了一种不使用构面的方法
    猜你喜欢
    • 2019-01-02
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多