我有使用此代碼制作的圖表:
df3 <- data.frame(outcome=c("Any complication", "Cardiac complication",
"Vascular Complication","Vascular complication",
"Respiratory complication", "Infectious complication",
"Neurological complication"),
index=1:7,
effect=c(-.4, -.25, -.1, .1, .15, .2, .3),
lower=c(-.43, -.29, -.17, -.02, .04, .17, .27),
upper=c(-.37, -.21, -.03, .22, .24, .23, .33))
plot2 <- ggplot(data=df3, aes(y=index, x=effect, xmin=lower, xmax=upper))
geom_point(shape="diamond", colour="royalblue4", size=5)
geom_errorbarh(height=.2, colour="royalblue4")
scale_y_continuous(breaks=1:nrow(df3), labels=df3$outcome)
labs(title="Adjusted Relative Risk for Complications", x="Relative Risk", y = "Type of complication")
geom_vline(xintercept=0, color='gray', linetype='dashed', alpha=.5)
theme_minimal()
plots_theme

但我想在那里添加 RR、95% 置信區間和其他一些東西,如下所示:

我真的可以使用一些幫助!R在這里很新:(
uj5u.com熱心網友回復:
為了更容易,我會借用幾個可以制作森林圖的 R 包之一。作為快速入門,我forestplot在這里使用包作為示例:
library(tidyverse)
library(forestplot)
df3 %>%
mutate(ci = paste0(" (", lower, ", ", upper, ")")) %>%
arrange(-index) %>%
forestplot::forestplot(
labeltext = c(outcome, effect, ci),
mean = effect,
low = lower,
upper = upper,
size = 0.1,
graph.pos = 2,
vertices = TRUE,
xlab = "Relative Risk",
title="Adjusted Relative Risk for Complications") %>%
fp_add_header(
outcome = c("Outcome"),
effect = c("Estimate"),
ci = "95% (CI)") %>%
fp_set_style(box = "royalblue4", line = "royalblue4")

編輯
與您的問題不同,Estimate圖中顯示的值可能不是相對風險,而是回歸系數。如果您想提出相對風險,您可以利用meta套餐:
library(meta)
df3 <- df3 %>% arrange(-index)
forest_df <-metagen(lower = lower,
upper = upper,
TE = effect,
data = df3,
sm = "RR")
forest(forest_df,
studlab = paste0(outcome),
layout = "JAMA",
common = FALSE,
hetstat = FALSE,
overall = FALSE)
使用更少的代碼和日志格式完成作業。

uj5u.com熱心網友回復:
這里有 geom_rect、geom_text 和 annotate 的一種方式。
library(ggplot2)
df3 <- data.frame(outcome=c("Any complication", "Cardiac complication",
"Vascular Complication","Vascular complication",
"Respiratory complication", "Infectious complication",
"Neurological complication"),
index=1:7,
effect=c(-.4, -.25, -.1, .1, .15, .2, .3),
lower=c(-.43, -.29, -.17, -.02, .04, .17, .27),
upper=c(-.37, -.21, -.03, .22, .24, .23, .33))
ggplot(data=df3, aes(y=index, x=effect, xmin=lower, xmax=upper))
geom_point(shape="diamond", colour="royalblue4", size=5)
geom_errorbarh(height=.2, colour="royalblue4")
scale_y_continuous(breaks=1:nrow(df3), labels=df3$outcome)
labs(title="Adjusted Relative Risk for Complications", x="Relative Risk", y = "Type of complication")
geom_vline(xintercept=0, color='gray', linetype='dashed', alpha=.5)
annotate("rect", xmin=.5, xmax= 1, ymin=0, ymax= 8, fill = "white")
geom_text(aes(label = paste(effect, "(", lower, ",", upper,")")) ,
x= 0.5, hjust="left")
scale_x_continuous(limits = c(-0.5, 1.0))
annotate("text", label = "Estimate - 95% CI", x = 0.5, hjust="left", y= 8)
theme_minimal() theme()

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/533309.html
標籤:rggplot2图形
上一篇:如何更改R中閃避列的順序?
