【问题标题】:How to re-arrange labels in forest plot made with ggplot2如何在使用 ggplot2 制作的森林图中重新排列标签
【发布时间】:2022-01-21 01:04:42
【问题描述】:

在使用 ggplot 2 制作森林图时,我遇到了重新排列标签的问题。希望有人能提供帮助! 问题:

我使用以下代码制作了情节:

df <- data.frame(t(data.frame(
  c("Remifentanil infusionrate, µg/kg/hour",3.1,-0.2,6.2,0.02),
  c("Propofol infusionsrate, mg/kg/hour",0.3,-0.04,0.7,0.13),
  c("BIS",-1.6,-6.5,5.3,0.72),
  c("Time spent in PACU, min",0.2,-0.3,0.6,0.44))))

library(ggplot2)

ggplot(data=df, aes(x=X1,y=as.numeric(X2),
                    ymin=as.numeric(X3),ymax=as.numeric(X4),
                    label=X5)) + 
   geom_point() + 
   geom_linerange() + 
   ylab("Difference (95% Confidence Interval)") + 
   theme_minimal() + 
   coord_flip() + 
   theme(axis.title.y = element_blank()) + 
   geom_text(aes(y=Inf), hjust=1) + 
   ggtitle("Primary and Secondary Outcomes")

它给出了这个输出:

标签按以下顺序排列:

  1. 在 PACU 中花费的时间,分钟
  2. 瑞芬太尼输注速率,µg/kg/小时
  3. 丙泊酚输注速率,mg/kg/小时
  4. BIS

我需要按这个顺序排列标签:

  1. 瑞芬太尼输注速率,µg/kg/小时
  2. 丙泊酚输注速率,mg/kg/小时
  3. BIS
  4. 在 PACU 中花费的时间,分钟

有人知道怎么做吗?

【问题讨论】:

标签: r ggplot2 forestplot


【解决方案1】:

重新组织数据。在 ggplot 中,分类值的顺序是按因子组织的,一旦你希望其余的由 ggplot 处理。

整理数据框中的数据的另一个好处是调用 ggplot 时不那么混乱。

df1 <- data.frame(
  x = c("Remifentanil infusionrate, µg/kg/hour",
           "Propofol infusionsrate, mg/kg/hour",
           "BIS",
           "Time spent in PACU, min"),
  y = c(3.1, 0.3, -1.6, 0.2),
  ymin = c(-0.2, -0.04, -6.5, -0.3),
  ymax = c(6.2, 0.7, 5.3, 0.6),
  label = c(0.02, 0.13, 0.72, 0.44))


df1$x <- factor(df1$x, levels = rev(df1$x))

library(ggplot2)

ggplot(data=df1, aes(x = x, y = y, ymin = ymin, ymax = ymax, label = label)) + 
  geom_point() + 
  geom_linerange() + 
  ylab("Difference (95% Confidence Interval)") + 
  theme_minimal() + 
  coord_flip() + 
  theme(axis.title.y = element_blank()) + 
  geom_text(aes(y=Inf), hjust=1) + 
  ggtitle("Primary and Secondary Outcomes")

reprex package (v2.0.1) 于 2021 年 12 月 18 日创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    相关资源
    最近更新 更多