使用 ggplot2 自定义森林图中比值比条形的自上而下顺序

问题描述 投票:0回答:1

我一直在使用 ggplot2 在 R 中绘制森林图,但在我的一生中,我无法以正确的顺序获得比值比。我希望它们从上到下是“未调整”、“根据家庭因素调整”、“根据家庭和自我运作因素调整”、“根据家庭、自我运作和社会人口因素调整”,如数据集。然而,情节是这样的:

有没有办法简单地反转订单?我已经尝试了 chatGPT 提供的所有解决方案,这是我能得到的最接近我想要的顺序的解决方案(所有其他尝试都在顺序上混乱了)。

这是我的代码:

library(devtools)
library(ggplot2)
library(stringr)
library(dplyr)
library(forcats)

# Create a data frame with your odds ratios and confidence intervals
odds_ratios <- c(1.290, 1.217, 1.231, 1.244)
ci_lower <- c(1.107, 1.038, 1.019, 1.030)
ci_upper <- c(1.504, 1.427, 1.486, 1.502)
Model <- c("Unadjusted", "Adjusted for family factors", "Adjusted for family and self-functioning factors", "Adjusted for family, self-functioning, and socio-demographic factors")

# Create a data frame
data <- data.frame(
  Model = Model,
  Odds_Ratio = odds_ratios,
  CI_lower = ci_lower,
  CI_upper = ci_upper
)

# Define the desired order of levels
desired_order <- c("Unadjusted", "Adjusted for family factors", "Adjusted for family and self-functioning factors", "Adjusted for family, self-functioning, and socio-demographic factors")

# Reorder the data frame rows based on the desired order
data <- data %>%
  mutate(Model = factor(Model, levels = desired_order)) %>%
  arrange(Model)

# Add a row number column
data$row_num <- 1:nrow(data)

# Define custom breaks and labels for the x-axis
custom_breaks <- c(0.5, 1, 1.2, 1.5)
custom_labels <- c(" ", "Just as likely", "1.2x as likely", "1.5x as likely")

# Add significance column
data$Significance <- ifelse(data$CI_lower > 1 | data$CI_upper < 1, "Significant", "Not Significant")

# Wrap long labels
data$Model <- str_wrap(data$Model, width = 30)

# Create the forest plot with customized labels
ggplot(data, aes(x = Odds_Ratio, y = reorder(Model, row_num, reverse = TRUE))) +
  geom_vline(xintercept = 1, linetype = "dashed", color = "gray") +
  geom_point(aes(x = Odds_Ratio, color = Significance), size = 3) +
  geom_errorbarh(aes(xmin = CI_lower, xmax = CI_upper), height = 0.3) +
  scale_x_continuous(breaks = custom_breaks, labels = custom_labels) +
  scale_color_manual(values = c("Significant" = "red", "Not Significant" = "black")) +
  theme_minimal() +
  labs(x = "Odds Ratio", y = " ") +
  ggtitle(paste("Odds Ratios for Family Affectedness of Individual Chronic Pain:", "\n", "Women vs. Men")) +
  theme(
    axis.title.y = element_blank(),
    axis.text = element_text(size = 10),
    panel.grid.major.y = element_line(color = "gray", linetype = "dashed"),
    legend.position = "none",
    plot.title = element_text(hjust = 0.5, lineheight = 1.2)
  )

提前非常感谢!

r ggplot2 visualization r-forestplot
1个回答
0
投票

如果您将“模型”作为一个因素,您可以使用

fct_rev()
获得所需的订单,例如

library(tidyverse)

# Create a data frame with your odds ratios and confidence intervals
odds_ratios <- c(1.290, 1.217, 1.231, 1.244)
ci_lower <- c(1.107, 1.038, 1.019, 1.030)
ci_upper <- c(1.504, 1.427, 1.486, 1.502)
Model <- c("Unadjusted", "Adjusted for family factors", "Adjusted for family and self-functioning factors", "Adjusted for family, self-functioning, and socio-demographic factors")

# Create a data frame
data <- data.frame(
  Model = Model,
  Odds_Ratio = odds_ratios,
  CI_lower = ci_lower,
  CI_upper = ci_upper
)

# Define the desired order of levels
desired_order <- c("Unadjusted", "Adjusted for family factors", "Adjusted for family and self-functioning factors", "Adjusted for family, self-functioning, and socio-demographic factors")

# Reorder the data frame rows based on the desired order
data <- data %>%
  mutate(Model = factor(Model, levels = desired_order)) %>%
  arrange(Model)

# Add a row number column
data$row_num <- 1:nrow(data)

# Define custom breaks and labels for the x-axis
custom_breaks <- c(0.5, 1, 1.2, 1.5)
custom_labels <- c(" ", "Just as likely", "1.2x as likely", "1.5x as likely")

# Add significance column
data$Significance <- ifelse(data$CI_lower > 1 | data$CI_upper < 1, "Significant", "Not Significant")

# Wrap long labels
data$Model <- factor(str_wrap(data$Model, width = 30),
                     levels = c("Unadjusted",
                                "Adjusted for family factors",
                                "Adjusted for family and\nself-functioning factors",
                                "Adjusted for family,\nself-functioning, and\nsocio-demographic factors"
                                ))

# Create the forest plot with customized labels
ggplot(data, aes(x = Odds_Ratio, y = fct_rev(Model))) +
  geom_vline(xintercept = 1, linetype = "dashed", color = "gray") +
  geom_point(aes(x = Odds_Ratio, color = Significance), size = 3) +
  geom_errorbarh(aes(xmin = CI_lower, xmax = CI_upper), height = 0.3) +
  scale_x_continuous(breaks = custom_breaks, labels = custom_labels) +
  scale_color_manual(values = c("Significant" = "red", "Not Significant" = "black")) +
  theme_minimal() +
  labs(x = "Odds Ratio", y = " ") +
  ggtitle(paste("Odds Ratios for Family Affectedness of Individual Chronic Pain:", "\n", "Women vs. Men")) +
  theme(
    axis.title.y = element_blank(),
    axis.text = element_text(size = 10),
    panel.grid.major.y = element_line(color = "gray", linetype = "dashed"),
    legend.position = "none",
    plot.title = element_text(hjust = 0.5, lineheight = 1.2)
  )

创建于 2024-04-04,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.