将零轴(HR = 1)上的x轴值与网格排列对齐

时间:2019-07-08 15:43:35

标签: r ggplot2 gridextra

我想将ggplot生成的三个图与网格比率为1.0的grid.arrange对齐。无论如何,是否有必要重新缩放此图提供的输出并使其在HR = 1上垂直对齐?

Please see below for the desired output.

boxLabels = c("Black", "Hispanic", "Asian", "Pacific Islander")

df <- data.frame(
  yAxis = length(boxLabels):1,
  boxOdds = c(6.07,1.35,1.05,4.56),
  boxCILow = c(1.23,0.23,0.26,1.20),
  boxCIHigh = c(29.92,7.83,4.15,17.24)
)

p <- ggplot(df, aes(x = boxOdds, y = yAxis))
p <- p + geom_vline(aes(xintercept = 1), size = .25, linetype = "dashed") +
  geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), size = .5, height = .2, color = c('#e495a5','#abb065','#39beb1','#aca4e2')) +
  geom_point(size = 3.5, color = "gray38") +
  theme_bw() +
  theme(panel.grid.minor = element_blank()) +
  scale_y_continuous(breaks = yAxis, labels = boxLabels) +
  scale_x_continuous(breaks = c(0,1,5,10,15,20,25,30) ) +
  coord_trans(x = "log10") +
  #ylab("Changes in AHR (Referent: Never AHR)") +
  ylab(expression(atop("Race/Ethnicity"))) +
  xlab("Hazard ratio (COPD mortality)") +
  annotate(geom = "text", y =1.0, x = 1.0, label ="", size = 3.5, hjust = 0) + 
  ggtitle("") 
p

df2 <- data.frame(
  yAxis = length(boxLabels):1,
  boxOdds = c(1.09,0.80,1.07,1.19),
  boxCILow = c(0.53,0.38,0.75,0.77),
  boxCIHigh = c(2.24,1.72,1.5,1.83)
)

p2 <- ggplot(df2, aes(x = boxOdds, y = yAxis))
p2 <- p2 + geom_vline(aes(xintercept = 1), size = .25, linetype = "dashed") +
  geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), size = .5, height = .2, color = c('#e495a5','#abb065','#39beb1','#aca4e2')) +
  geom_point(size = 3.5, color = "gray38") +
  theme_bw() +
  theme(panel.grid.minor = element_blank()) +
  scale_y_continuous(breaks = yAxis, labels = boxLabels) +
  scale_x_continuous(breaks = c(0,0.5,1,1.5,2,2.5,3)) +
  coord_trans(x = "log10") +
  ylab(expression(atop("Race/Ethnicity"))) +
  xlab("Hazard ratio (CVD mortality)") +
  annotate(geom = "text", y =1.0, x = 1.0, label ="", size = 3.5, hjust = 0) + 
  ggtitle("") 
p2

df3 <- data.frame(
  yAxis = length(boxLabels):1,
  boxOdds = c(0.47,0.90,0.85,0.92),
  boxCILow = c(0.19,0.44,0.60,0.59),
  boxCIHigh = c(1.14,1.84,1.19,1.42)
)

p3 <- ggplot(df3, aes(x = boxOdds, y = yAxis))
p3 <- p3 + geom_vline(aes(xintercept = 1), size = .25, linetype = "dashed") +
  geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), size = .5, height = .2, color = c('#e495a5','#abb065','#39beb1','#aca4e2')) +
  geom_point(size = 3.5, color = "gray38") +
  theme_bw() +
  theme(panel.grid.minor = element_blank()) +
  scale_y_continuous(breaks = yAxis, labels = boxLabels) +
  scale_x_continuous(breaks = c(0,0.5,1,1.5,2,2.5,3)) +
  coord_trans(x = "log10") +
  ylab(expression(atop("Race/Ethnicity"))) +
  xlab("Hazard ratio (Cancer mortality)") +
  annotate(geom = "text", y =1.0, x = 1.0, label ="", size = 3.5, hjust = 0) + 
  ggtitle("") 
p3

theme_set(theme_pubr())

library("gridExtra")
grid.arrange(p, p2, p3)

我想将ggplot生成的三个图与网格比率为1.0的grid.arrange对齐。无论如何,是否有必要重新缩放此图提供的输出并使其在HR = 1上垂直对齐?

2 个答案:

答案 0 :(得分:2)

为每个图在x轴上设置相同限制的替代方法。您可以使用相同的比例尺,并在每次对scale_x_continuous的调用中添加一个limit参数来实现此目的。

您将需要为数据集选择适当的限制。根据提供的数据,使用的限制为0.1和30

library(ggplot2)
library(gridExtra)

boxLabels = c("Black", "Hispanic", "Asian", "Pacific Islander")

df <- data.frame(
      yAxis = length(boxLabels):1,
      boxOdds = c(6.07,1.35,1.05,4.56),
      boxCILow = c(1.23,0.23,0.26,1.20),
      boxCIHigh = c(29.92,7.83,4.15,17.24)
)
p <- ggplot(df, aes(x = boxOdds, y = yAxis))+
      geom_vline(aes(xintercept = 1), size = .25, linetype = "dashed") +
      geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), 
                     size = .5, height = .2, color = c('#e495a5','#abb065','#39beb1','#aca4e2')) +
      geom_point(size = 3.5, color = "gray38") +
      theme_bw() +
      theme(panel.grid.minor = element_blank()) +
      scale_y_continuous(breaks = df$yAxis, labels = boxLabels) +
      scale_x_continuous(breaks = c(0,1,5,10,15,20,25,30), limits = c(.1,30)) +
      coord_trans(x = "log10") +
      #ylab("Changes in AHR (Referent: Never AHR)") +
      ylab(expression(atop("Race/Ethnicity"))) +
      xlab("Hazard ratio (COPD mortality)") +
      annotate(geom = "text", y =1.0, x = 1.0, label ="", size = 3.5, hjust = 0) + 
      ggtitle("") 
p

df2 <- data.frame(
      yAxis = length(boxLabels):1,
      boxOdds = c(1.09,0.80,1.07,1.19),
      boxCILow = c(0.53,0.38,0.75,0.77),
      boxCIHigh = c(2.24,1.72,1.5,1.83)
)

p2 <- ggplot(df2, aes(x = boxOdds, y = yAxis))+
      geom_vline(aes(xintercept = 1), size = .25, linetype = "dashed") +
      geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), size = .5, height = .2, color = c('#e495a5','#abb065','#39beb1','#aca4e2')) +
      geom_point(size = 3.5, color = "gray38") +
      theme_bw() +
      theme(panel.grid.minor = element_blank()) +
      scale_y_continuous(breaks = df2$yAxis, labels = boxLabels) +
      # scale_x_continuous(breaks = c(0,0.5,1,1.5,2,2.5,3)) +
      scale_x_continuous(breaks = c(0,1,5,10,15,20,25,30), limits = c(.1,30)) +
      coord_trans(x = "log10") +
      ylab(expression(atop("Race/Ethnicity"))) +
      xlab("Hazard ratio (CVD mortality)") +
      annotate(geom = "text", y =1.0, x = 1.0, label ="", size = 3.5, hjust = 0) + 
      ggtitle("") 
p2

df3 <- data.frame(
      yAxis = length(boxLabels):1,
      boxOdds = c(0.47,0.90,0.85,0.92),
      boxCILow = c(0.19,0.44,0.60,0.59),
      boxCIHigh = c(1.14,1.84,1.19,1.42)
)

p3 <- ggplot(df3, aes(x = boxOdds, y = yAxis)) +
      geom_vline(aes(xintercept = 1), size = .25, linetype = "dashed") +
      geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), size = .5, height = .2, color = c('#e495a5','#abb065','#39beb1','#aca4e2')) +
      geom_point(size = 3.5, color = "gray38") +
      theme_bw() +
      theme(panel.grid.minor = element_blank()) +
      scale_y_continuous(breaks = df3$yAxis, labels = boxLabels) +
      # scale_x_continuous(breaks = c(0,0.5,1,1.5,2,2.5,3)) +
      scale_x_continuous(breaks = c(0,1,5,10,15,20,25,30), limits = c(.1,30)) +
      coord_trans(x = "log10") +
      ylab(expression(atop("Race/Ethnicity"))) +
      xlab("Hazard ratio (Cancer mortality)") +
      annotate(geom = "text", y =1.0, x = 1.0, label ="", size = 3.5, hjust = 0) + 
      ggtitle("") 
p3

theme_set(ggpubr::theme_pubr())

grid.arrange(p, p2, p3)

enter image description here

答案 1 :(得分:1)

一种实现方法是使用facet_wrap。取而代之的是,您要创建一个带有数据集变量的数据框。这样只需绘制一次即可简化该过程。通过在scales = "fixed"中使用facet_wrap,您可以跨绘图对齐轴。这是一个简化的示例:

library("dplyr")
library("ggplot2")

boxLabels = c("Black", "Hispanic", "Asian", "Pacific Islander")

df1 <- data.frame(
  yAxis = length(boxLabels):1,
  boxOdds = c(6.07,1.35,1.05,4.56),
  boxCILow = c(1.23,0.23,0.26,1.20),
  boxCIHigh = c(29.92,7.83,4.15,17.24),
  data = 1
)

df2 <- data.frame(
  yAxis = length(boxLabels):1,
  boxOdds = c(1.09,0.80,1.07,1.19),
  boxCILow = c(0.53,0.38,0.75,0.77),
  boxCIHigh = c(2.24,1.72,1.5,1.83),
  data = 2
)

df <- bind_rows(df1, df2)

ggplot(df, aes(x = boxOdds, y = yAxis)) + 
  geom_vline(aes(xintercept = 1), size = .25, linetype = "dashed") +
  geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), size = .5, height = .2) +
  geom_point(size = 3.5, color = "gray38") +
  facet_wrap(~data, nrow = 2, scale = "fixed")

enter image description here