如何根据 R 中森林图中的值为点分配不同的颜色?

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

我正在尝试使用

forestplot
包在 R 中创建森林图。我想根据
log.est
值是否高于或低于 0 为点和线分配不同的颜色。具体来说,我想要:

  • 蓝色代表
    log.est > 0
  • 红色代表
    log.est <= 0

这是我的数据:

df <- structure(list(variable = c("N.Acetylputrescine", "Homocitrulline", "Argininic.acid", 
               "SM.C16.1", "Oxalic.acid", "Cer.d18.1.22.0.", "Citrulline", 
               "X3.Hydroxybutyric.acid", "Glycine", "Cer.d18.1.25.0.", 
               "Uridine", "Cer.d18.1.24.1.", "Deoxyguanosine", "Adenine"), 
  log.est = c(18.12, 11.70, 11.61, 9.95, 8.79, 8.72, 7.07, 4.13, 2.63, 
              -5.85, -6.47, -6.81, -10.47, -14.84), 
  p.value = c(9.49e-05, 0.000196, 0.0117, 0.137, 7.44e-05, 0.251, 0.514, 
              0.000162, 0.0376, 0.909, 0.000858, 0.345, 0.000531, 1.4e-05), 
  log.lower = c(17.12, 10.62, 9.44, -8.30, 7.81, -8.23, -8.08, 3.08, 
                -1.49, -10.03, -7.14, -8.43, -11.12, -15.37), 
  log.upper = c(18.71, 12.31, 12.44, 11.16, 9.38, 10.16, 9.08, 4.74, 
                3.59, 9.86, -5.19, 6.91, -9.27, -13.97)
), class = "data.frame", row.names = c(NA, -14L))

这是我用来生成森林图的代码:

library(forestplot)

forestplot(
  labeltext = cbind("Variable" = df$variable,
                    "Coefficient" = round(df$log.est, 2),
                    "P-value" = format.pval(df$p.value)),
  mean = df$log.est,
  lower = df$log.lower,
  upper = df$log.upper,
  xlab = "Effect Size",
  title = "Logistic Regression"
)

这会生成森林图,但所有点和线都是相同的颜色。如何将 blue 分配给带有

log.est > 0
的点,并将 red 分配给带有
log.est <= 0
的点?

我尝试了下面的代码:但它给了我所有数据的蓝色。

# Define colors based on log.est values
df$color <- ifelse(df$log.est > 0, "blue", "red")

# Create the forest plot
forestplot(labeltext = cbind("Variable" = df$variable,
                    "Coefficient" = round(df$log.est, 2),
                    "P-value" = format.pval(df$p.value)),
           mean = df$log.est,
           lower = df$log.lower,
           upper = df$log.upper,
           xlab = "Effect Size",
           title = "Logistic Regression",
           boxsize = 0.3, # Size of the points
           txt_gp = fpTxtGp(label = gpar(cex = 0.8)), # Text size customization
           col = fpColors(box = df$color, line = df$color, zero = "black"))

ouput

任何帮助或建议将不胜感激。谢谢!

r ggplot2 r-forestplot forest-plots
1个回答
1
投票

你可以尝试:

# Order df by log.est
df <- df[order(df$log.est, decreasing = TRUE), ]

# Add a color column for positive and negative values
df$color <- ifelse(df$log.est >= 0, "blue", "red")

# Forest plot
ggplot(df, aes(x = log.est, y = reorder(variable, log.est), xmin = log.lower, xmax = log.upper, color = color)) +
  geom_pointrange() +
  geom_vline(xintercept = 0, linetype = "dashed", color = "black") +
  scale_color_identity() +
  labs(title = "Forest Plot with Color-Coded Estimates",
       x = "Log Estimate",
       y = "Variable") +
  theme_minimal() +
  theme(axis.text.y = element_text(size = 10), axis.title.y = element_blank())

enter image description here

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.