将空值 (HR=1) 上的 x 轴值与网格排列对齐
Aligning x-axis values across the null (HR=1) with grid arrange
我想将 ggplot 生成的三个图表沿着风险比 = 1.0 与 grid.arrange 对齐。无论如何要重新缩放此图提供的输出并将其垂直对齐到 HR=1?
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?
一种方法是使用 facet_wrap
。相反,您可能希望为数据集创建一个带有变量的数据框。这将通过只绘制一次来简化过程。通过在 facet_wrap
中使用 scales = "fixed"
,您可以跨图对齐轴。这是一个简化的例子:
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")
为每个绘图在 x 轴上设置相同限制的替代方法。您可以通过使用相同的比例并为每个对 scale_x_continuous.
的调用添加一个 limits 参数来做到这一点
您将需要 select 为您的数据集设置适当的限制。使用提供的数据,这使用 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)
我想将 ggplot 生成的三个图表沿着风险比 = 1.0 与 grid.arrange 对齐。无论如何要重新缩放此图提供的输出并将其垂直对齐到 HR=1?
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?
一种方法是使用 facet_wrap
。相反,您可能希望为数据集创建一个带有变量的数据框。这将通过只绘制一次来简化过程。通过在 facet_wrap
中使用 scales = "fixed"
,您可以跨图对齐轴。这是一个简化的例子:
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")
为每个绘图在 x 轴上设置相同限制的替代方法。您可以通过使用相同的比例并为每个对 scale_x_continuous.
的调用添加一个 limits 参数来做到这一点您将需要 select 为您的数据集设置适当的限制。使用提供的数据,这使用 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)