R语言 如何保持ggplot2中变量的顺序不变?

q7solyqu  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(307)

我尝试用以下代码绘制一个模型的回归系数的森林图:

library(ggplot2)
library(tidyverse)

# Create a data.frame
forestPlot <- tribble(
  ~factor, ~PR, ~Low, ~High,
  #------/------/------/------/
  "Breastfeeding: <17 weeks vs 26-51", 0.75,0.52,1.10,
  "17-25 weeks vs 26-51", 0.81, 0.50, 1.31,
  "≥52 weeks vs 26-51", 1.20, 0.88, 1.63,
  
  "Mother's age", 0.99, 0.96, 1.02,
  
  "Mother's education: High school vs tertiary", 1.64, 1.14, 2.36,
  "Vocational vs tertiary",1.31, 0.92, 1.86,
  
  "Mother's country of birth: UK/Ireland vs AU/NZ", 0.53, 0.17, 1.63,
  "India vs AU/NZ",1.66, 1.05, 2.61,
  "Asia-except India vs AU/NZ",1.86,1.28,2.69,
  "Other vs AU/NZ",0.83, 0.43, 1.58,
  
  "IRSAD: 1 vs 5", 1.48, 0.77, 2.83,
  "2 vs 5", 1.47, 0.79, 2.78,
  "3 vs 5", 1.09, 0.56, 2.14,
  "4 vs 5", 1.36, 0.72, 2.56,
  )
forestPlot

# Factoring variables of the model
forestPlot$factor <- factor(forestPlot$factor, levels = c(
  "Breastfeeding: <17 weeks vs 26-51", "17-25 weeks vs 26-51", "≥52 weeks vs 26-51", 
  "Mother's age", "Mother's education: High school vs tertiary", 
  "Vocational vs tertiary", "Mother's country of birth: UK/Ireland vs AU/NZ", 
  "India vs AU/NZ", "Asia-except India vs AU/NZ", "Other vs AU/NZ", "IRSAD: 1 vs 5", 
  "2 vs 5", "3 vs 5", "4 vs 5"))
forestPlot

# Make a forest plot
library(forcats)
Model1A <- forestPlot %>%
  ggplot(aes(x = fct_inorder(factor), y = PR, ymin = Low, ymax = High)) +
  geom_errorbar(width=.2, size = 1.0, show.legend = F) +
  geom_point(size= 3.5, shape=21, fill="white", show.legend = F)+ 
  geom_hline(yintercept = 1, linetype = 2, col = "red", size = 0.8) +
  coord_flip() +
  labs(title = "A. Model for detal caries prevalance", subtitle = "Breastfeeding as main exposure",
       x = "", y = "PR (95% CI)") +
  theme(
    text = element_text(family = "Georgia"),
    plot.title = element_text(size = 14, color = "grey10", face = "bold"),
    plot.subtitle = element_text(face = "italic", color = "gray20", size = 12),
    plot.caption = element_text(face = "italic", size = 12, color = "gray40"),
    axis.title.x = element_text(face = "bold", size = 12, color = "grey20"),
    axis.title.y = element_blank(),
    panel.grid.major = element_line(color = "gray90"),
    panel.grid.minor.y = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.background = element_rect(fill = "#fcfbfd"))
Model1A

然而,预测器的顺序是从底部显示的,而我希望保持它们与输入变量的顺序相同(从顶部开始,而不是从底部开始)。我从stackoverflow中搜索并使用函数fct_inorder(factor),但没有工作。
另一个问题是,是否有R码的方式,在右手边显示OR(或PR/RR)和95%CI,如附图所示:

因此,如果你能给予我一些关于上述绘图的提示,我非常感激。
感谢您,并期待收到您的帮助。

00jrzges

00jrzges1#

默认情况下,离散比例上的类别从底部或左侧的第一个级别或类别开始显示。
要反转顺序,可以使用forcats::fct_rev反转因子水平的顺序,或通过比例反转顺序,例如:在您的情况下使用scale_x_discrete(limits = rev)

1.颠倒等级顺序
library(tidyverse)

forestPlot$factor <- fct_inorder(forestPlot$factor)

ggplot(forestPlot, aes(x = fct_rev(factor), y = PR, ymin = Low, ymax = High)) +
  geom_errorbar(width = .2, size = 1.0, show.legend = F) +
  geom_point(size = 3.5, shape = 21, fill = "white", show.legend = F) +
  geom_hline(yintercept = 1, linetype = 2, col = "red", size = 0.8) +
  coord_flip() +
  labs(
    title = "A. Model for detal caries prevalance", subtitle = "Breastfeeding as main exposure",
    x = "", y = "PR (95% CI)"
  ) +
  theme(
    text = element_text(family = "Georgia"),
    plot.title = element_text(size = 14, color = "grey10", face = "bold"),
    plot.subtitle = element_text(face = "italic", color = "gray20", size = 12),
    plot.caption = element_text(face = "italic", size = 12, color = "gray40"),
    axis.title.x = element_text(face = "bold", size = 12, color = "grey20"),
    axis.title.y = element_blank(),
    panel.grid.major = element_line(color = "gray90"),
    panel.grid.minor.y = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.background = element_rect(fill = "#fcfbfd")
  ) 
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

2.通过scale_x_discrete(limits = rev)反序
ggplot(forestPlot, aes(x = factor, y = PR, ymin = Low, ymax = High)) +
  geom_errorbar(width = .2, size = 1.0, show.legend = F) +
  geom_point(size = 3.5, shape = 21, fill = "white", show.legend = F) +
  geom_hline(yintercept = 1, linetype = 2, col = "red", size = 0.8) +
  coord_flip() +
  labs(
    title = "A. Model for detal caries prevalance", subtitle = "Breastfeeding as main exposure",
    x = "", y = "PR (95% CI)"
  ) +
  theme(
    text = element_text(family = "Georgia"),
    plot.title = element_text(size = 14, color = "grey10", face = "bold"),
    plot.subtitle = element_text(face = "italic", color = "gray20", size = 12),
    plot.caption = element_text(face = "italic", size = 12, color = "gray40"),
    axis.title.x = element_text(face = "bold", size = 12, color = "grey20"),
    axis.title.y = element_blank(),
    panel.grid.major = element_line(color = "gray90"),
    panel.grid.minor.y = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.background = element_rect(fill = "#fcfbfd")
  ) +
  scale_x_discrete(limits = rev)

相关问题