How Do I Add An Additional Column and Change The X-Axis To Log Scale On A Forest Plot Constructed With GGPlot2 and GridExtra?

huangapple go评论53阅读模式
英文:

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 &lt;- data.frame(
Index = c(1, 2, 3, 4,5),
label = c(&quot;All&quot;, &quot;Her2+&quot;, &quot;LumA&quot;, &quot;LumB&quot;, &quot;TNBC&quot;),
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(&quot;(0.14 - 0.28)&quot;, &quot;(0.08 - 0.68)&quot;, &quot;(0.06 - 0.21)&quot;, &quot;(0.1 - 0.5)&quot;, &quot;(0.11 - 0.35)&quot;), 
P_Value = c(&quot;&lt;0.0001&quot;, &quot;0.0065&quot;, &quot;&lt;0.0001&quot;, &quot;&lt;0.0001&quot;, &quot;0.0001&quot;)
)
plot1 &lt;- ggplot(dat, aes(y = Index, x = HR)) + geom_point() + scale_x_continuous(trans = &#39;log10&#39;)   + 
geom_point(shape = 18, size = 5) +  
geom_errorbarh(aes(xmin = LL, xmax = UL), height = 0.25) +
geom_vline(xintercept = 1, color = &quot;red&quot;, linetype = &quot;dashed&quot;, cex = 1, alpha = 0.5) +
scale_y_continuous(name = &quot;&quot;, breaks=1:5, labels = dat$label, trans = &quot;reverse&quot;) +
xlab(&quot;Hazard Ratio (Log10)&quot;) + 
ylab(&quot; &quot;) + 
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 = &quot;black&quot;),
axis.text.y = element_text(size = 12, colour = &quot;black&quot;),
axis.text.x.bottom = element_text(size = 12, colour = &quot;black&quot;),
axis.title.x = element_text(size = 12, colour = &quot;black&quot;))
plot1
table_base &lt;- ggplot(dat, aes(y=label)) +
ylab(NULL) + xlab(&quot;  &quot;) + 
theme(plot.title = element_text(hjust = 0.5, size=12), 
axis.text.x = element_text(color=&quot;white&quot;, 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 = &quot;none&quot;,
panel.background = element_blank(), 
panel.border = element_blank(), 
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), 
plot.background = element_blank())
tab1 &lt;- table_base + 
labs(title = &quot;space&quot;) +
geom_text(aes(y = rev(Index), x = 1, label = sprintf(&quot;%0.1f&quot;, round(HR, digits = 2))), size = 4) + ## decimal places
ggtitle(&quot;HR&quot;)
tab2 &lt;- table_base +
geom_text(aes(y = rev(Index), x = 1, label = CI), size = 4) + 
ggtitle(&quot;95% CI&quot;)
lay &lt;-  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 &lt;- 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 = &quot;red&quot;, linetype = &quot;dashed&quot;, cex = 1, alpha = 0.5) +
scale_y_continuous(name = &quot;&quot;, breaks=1:5, labels = dat$label, trans = &quot;reverse&quot;, expand = expansion(add = 0.5)) +
scale_x_continuous(trans = &#39;log10&#39;)   + 
xlab(&quot;Hazard Ratio (Log10)&quot;) + 
ylab(&quot; &quot;) + 
theme_classic()
tab &lt;- ggplot(dat, aes(y = Index)) +
geom_text(aes(x = 0, label = sprintf(&quot;%0.1f&quot;, 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 = &#39;reverse&#39;, expand = expansion(add = 0.5)) +
scale_x_continuous(
breaks = 0:2, labels = c(&#39;HR&#39;, &#39;CI&#39;, &#39;P-value&#39;), 
position = &#39;top&#39;, expand = expansion(add = 0.5)) +
theme_void() +
theme(axis.text.x = element_text(face = &#39;bold&#39;))
library(patchwork)
forest + tab + plot_layout(ncol = 2, widths = c(3, 1))

How Do I Add An Additional Column and Change The X-Axis To Log Scale On A Forest Plot Constructed With GGPlot2 and GridExtra?

huangapple
  • 本文由 发表于 2023年6月6日 00:32:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定