英文:
How Do I Add An Additional Column and Change The X-Axis To Log Scale On A Forest Plot Constructed With GGPlot2 and GridExtra?
问题
我使用ggplot2和gridextra包制作了一个森林图,但我想在右侧添加一列以添加P值。
此外,我想将刻度标记更改为log10。
有人可以建议我如何做到这一点吗?我是一个初学者,所以您能提供的任何见解都将不胜感激!
以下是当前代码以及我的数据:
library(gridExtra)
library(ggplot2)
dat <- data.frame(
Index = c(1, 2, 3, 4, 5),
label = c("All", "Her2+", "LumA", "LumB", "TNBC"),
HR = c(0.2, 0.23, 0.11, 0.22, 0.2),
LL = c(0.14, 0.08, 0.06, 0.1, 0.11),
UL = c(0.28, 0.68, 0.21, 0.5, 0.35),
CI = c("(0.14 - 0.28)", "(0.08 - 0.68)", "(0.06 - 0.21)", "(0.1 - 0.5)", "(0.11 - 0.35)"),
P_Value = c("<0.0001", "0.0065", "<0.0001", "<0.0001", "0.0001")
)
plot1 <- ggplot(dat, aes(y = Index, x = HR)) + geom_point() + scale_x_continuous(trans = 'log10') +
geom_point(shape = 18, size = 5) +
geom_errorbarh(aes(xmin = LL, xmax = UL), height = 0.25) +
geom_vline(xintercept = 1, color = "red", linetype = "dashed", cex = 1, alpha = 0.5) +
scale_y_continuous(name = "", breaks = 1:5, labels = dat$label, trans = "reverse") +
xlab("Hazard Ratio (Log10)") +
ylab(" ") +
theme_bw() +
theme(panel.border = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
axis.text.y = element_text(size = 12, colour = "black"),
axis.text.x.bottom = element_text(size = 12, colour = "black"),
axis.title.x = element_text(size = 12, colour = "black"))
plot1
table_base <- ggplot(dat, aes(y = label)) +
ylab(NULL) + xlab(" ") +
theme(plot.title = element_text(hjust = 0.5, size = 12),
axis.text.x = element_text(color = "white", hjust = -3, size = 25),
axis.line = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.y = element_blank(),
legend.position = "none",
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_blank())
tab1 <- table_base +
labs(title = "space") +
geom_text(aes(y = rev(Index), x = 1, label = sprintf("%0.1f", round(HR, digits = 2))), size = 4) +
ggtitle("HR")
tab2 <- table_base +
geom_text(aes(y = rev(Index), x = 1, label = CI), size = 4) +
ggtitle("95% CI")
lay <- matrix(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 3), nrow = 1)
grid.arrange(plot1, tab1, tab2, layout_matrix = lay)
代码来源:https://github.com/mbounthavong/Forest-plots-in-R/blob/main/RMarkdown_code
英文:
I made a forest plot using the ggplot2 and gridextra packages, but I would like to add an extra column on the right side so that I can add the P-values.
Additionally, I would like to change the scale notation to log10.
Can anyone advise me on how to do this? I'm a novice, so any insight you can provide would be greatly appreciated!
Here is what the current code looks like with my data added:
library(gridExtra)
library(ggplot2)
dat <- data.frame(
Index = c(1, 2, 3, 4,5),
label = c("All", "Her2+", "LumA", "LumB", "TNBC"),
HR = c(0.2,0.23, 0.11, 0.22,0.2),
LL = c(0.14, 0.08, 0.06, 0.1,0.11),
UL = c(0.28, 0.68, 0.21, 0.5,0.35),
CI = c("(0.14 - 0.28)", "(0.08 - 0.68)", "(0.06 - 0.21)", "(0.1 - 0.5)", "(0.11 - 0.35)"),
P_Value = c("<0.0001", "0.0065", "<0.0001", "<0.0001", "0.0001")
)
plot1 <- ggplot(dat, aes(y = Index, x = HR)) + geom_point() + scale_x_continuous(trans = 'log10') +
geom_point(shape = 18, size = 5) +
geom_errorbarh(aes(xmin = LL, xmax = UL), height = 0.25) +
geom_vline(xintercept = 1, color = "red", linetype = "dashed", cex = 1, alpha = 0.5) +
scale_y_continuous(name = "", breaks=1:5, labels = dat$label, trans = "reverse") +
xlab("Hazard Ratio (Log10)") +
ylab(" ") +
theme_bw() +
theme(panel.border = element_blank(),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
axis.text.y = element_text(size = 12, colour = "black"),
axis.text.x.bottom = element_text(size = 12, colour = "black"),
axis.title.x = element_text(size = 12, colour = "black"))
plot1
table_base <- ggplot(dat, aes(y=label)) +
ylab(NULL) + xlab(" ") +
theme(plot.title = element_text(hjust = 0.5, size=12),
axis.text.x = element_text(color="white", hjust = -3, size = 25), ## This is used to help with alignment
axis.line = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.y = element_blank(),
legend.position = "none",
panel.background = element_blank(),
panel.border = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.background = element_blank())
tab1 <- table_base +
labs(title = "space") +
geom_text(aes(y = rev(Index), x = 1, label = sprintf("%0.1f", round(HR, digits = 2))), size = 4) + ## decimal places
ggtitle("HR")
tab2 <- table_base +
geom_text(aes(y = rev(Index), x = 1, label = CI), size = 4) +
ggtitle("95% CI")
lay <- matrix(c(1,1,1,1,1,1,1,1,1,1,2,3,3), nrow = 1)
grid.arrange(plot1, tab1, tab2, layout_matrix = lay)
Code source: https://github.com/mbounthavong/Forest-plots-in-R/blob/main/RMarkdown_code
答案1
得分: 1
这是您提供的代码的翻译:
如何尝试这样的方式:
forest <- ggplot(dat, aes(y = Index, x = HR)) +
geom_point(shape = 18, size = 5) +
geom_errorbarh(aes(xmin = LL, xmax = UL), height = 0.25) +
geom_vline(xintercept = 1, color = "red", linetype = "dashed", cex = 1, alpha = 0.5) +
scale_y_continuous(name = "", breaks=1:5, labels = dat$label, trans = "reverse", expand = expansion(add = 0.5)) +
scale_x_continuous(trans = 'log10') +
xlab("Hazard Ratio (Log10)") +
ylab(" ") +
theme_classic()
tab <- ggplot(dat, aes(y = Index)) +
geom_text(aes(x = 0, label = sprintf("%0.1f", round(HR, digits = 2))), size = 4) +
geom_text(aes(x = 1, label = CI), size = 4) +
geom_text(aes(x = 2, label = P_Value), size = 4) +
scale_y_continuous(trans = 'reverse', expand = expansion(add = 0.5)) +
scale_x_continuous(
breaks = 0:2, labels = c('HR', 'CI', 'P-value'),
position = 'top', expand = expansion(add = 0.5)) +
theme_void() +
theme(axis.text.x = element_text(face = 'bold'))
library(patchwork)
forest + tab + plot_layout(ncol = 2, widths = c(3, 1))
希望这对您有所帮助。
英文:
How about something like this:
forest <- ggplot(dat, aes(y = Index, x = HR)) +
geom_point(shape = 18, size = 5) +
geom_errorbarh(aes(xmin = LL, xmax = UL), height = 0.25) +
geom_vline(xintercept = 1, color = "red", linetype = "dashed", cex = 1, alpha = 0.5) +
scale_y_continuous(name = "", breaks=1:5, labels = dat$label, trans = "reverse", expand = expansion(add = 0.5)) +
scale_x_continuous(trans = 'log10') +
xlab("Hazard Ratio (Log10)") +
ylab(" ") +
theme_classic()
tab <- ggplot(dat, aes(y = Index)) +
geom_text(aes(x = 0, label = sprintf("%0.1f", round(HR, digits = 2))), size = 4) +
geom_text(aes(x = 1, label = CI), size = 4) +
geom_text(aes(x = 2, label = P_Value), size = 4) +
scale_y_continuous(trans = 'reverse', expand = expansion(add = 0.5)) +
scale_x_continuous(
breaks = 0:2, labels = c('HR', 'CI', 'P-value'),
position = 'top', expand = expansion(add = 0.5)) +
theme_void() +
theme(axis.text.x = element_text(face = 'bold'))
library(patchwork)
forest + tab + plot_layout(ncol = 2, widths = c(3, 1))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论