调整比例因子(1200 到 1900)对您有用吗?
library(tidyverse)
deciles <- data.frame(count = 1:10,
coef_maroon = c(0.005, 0.015, 0.015, 0.02, 0.03, 0.07, 0.09, 0.12, 0.12, 0.13),
ci_upper_maroon = c(0.008, 0.02, 0.025, 0.03, 0.04, 0.09, 0.11, 0.14, 0.13, 0.15),
ci_lower_maroon = c(0.001, 0.01, 0.005, 0, 0.01, 0.05, 0.07, 0.11, 0.11, 0.12),
coef_navy= c(0, -200, -400, -600, -800, -700, -900, -900, -1100, -1700),
ci_upper_navy = c(100, -100, -300, -500, -700, -600, -800, -700, -900, -1600),
ci_lower_navy = c(-100, -500, -700, -900, -850, -800, -950, -1000, -1200, -1900))
scl = with(deciles, max(abs(coef_navy))/max(abs(coef_maroon)))
ggplot(deciles) +
geom_point(aes(x = count, y = coef_navy, color = 'navy')) +
geom_point(aes(x = count, y = coef_maroon*scl-1900, color = 'maroon')) +
geom_errorbar(aes(x = count, ymin = ci_lower_navy, ymax = ci_upper_navy, color = 'navy'), width = 0) +
geom_errorbar(aes(x = count, ymin = ci_lower_maroon*scl-1900, ymax = ci_upper_maroon*scl-1900, color = 'maroon', width = 0)) +
labs(x = "Group", y = "") +
scale_color_manual(values = c('maroon', 'navy')) +
scale_y_continuous(sec.axis = sec_axis(~(.+1900)/scl, name = "2nd axis"))
由reprex package (v2.0.1) 于 2021-09-02 创建
此外,根据 x 轴值的相关程度,您可以考虑微调这些值,使它们不会重叠,例如
library(tidyverse)
deciles <- data.frame(count = 1:10,
coef_maroon = c(0.005, 0.015, 0.015, 0.02, 0.03, 0.07, 0.09, 0.12, 0.12, 0.13),
ci_upper_maroon = c(0.008, 0.02, 0.025, 0.03, 0.04, 0.09, 0.11, 0.14, 0.13, 0.15),
ci_lower_maroon = c(0.001, 0.01, 0.005, 0, 0.01, 0.05, 0.07, 0.11, 0.11, 0.12),
coef_navy= c(0, -200, -400, -600, -800, -700, -900, -900, -1100, -1700),
ci_upper_navy = c(100, -100, -300, -500, -700, -600, -800, -700, -900, -1600),
ci_lower_navy = c(-100, -500, -700, -900, -850, -800, -950, -1000, -1200, -1900))
scl = with(deciles, max(abs(coef_navy))/max(abs(coef_maroon)))
ggplot(deciles) +
geom_point(aes(x = count, y = coef_navy, color = 'navy'),
position = position_nudge(x = -0.05)) +
geom_point(aes(x = count, y = coef_maroon*scl-1900, color = 'maroon'),
position = position_nudge(x = 0.05)) +
geom_errorbar(aes(x = count, ymin = ci_lower_navy,
ymax = ci_upper_navy, color = 'navy', width = 0),
position = position_nudge(x = -0.05)) +
geom_errorbar(aes(x = count, ymin = ci_lower_maroon*scl-1900,
ymax = ci_upper_maroon*scl-1900,
color = 'maroon', width = 0),
position = position_nudge(x = 0.05)) +
labs(x = "Group", y = "") +
scale_color_manual(values = c('maroon', 'navy')) +
scale_y_continuous(sec.axis = sec_axis(~(.+1900)/scl, name = "2nd axis"))
由reprex package (v2.0.1) 于 2021-09-02 创建
如果您调整 scale_y_continuous 中的expand() 选项,您可以进一步修剪顶部的“空白空间”:
library(tidyverse)
deciles <- data.frame(count = 1:10,
coef_maroon = c(0.005, 0.015, 0.015, 0.02, 0.03, 0.07, 0.09, 0.12, 0.12, 0.13),
ci_upper_maroon = c(0.008, 0.02, 0.025, 0.03, 0.04, 0.09, 0.11, 0.14, 0.13, 0.15),
ci_lower_maroon = c(0.001, 0.01, 0.005, 0, 0.01, 0.05, 0.07, 0.11, 0.11, 0.12),
coef_navy= c(0, -200, -400, -600, -800, -700, -900, -900, -1100, -1700),
ci_upper_navy = c(100, -100, -300, -500, -700, -600, -800, -700, -900, -1600),
ci_lower_navy = c(-100, -500, -700, -900, -850, -800, -950, -1000, -1200, -1900))
scl = with(deciles, max(abs(coef_navy))/max(abs(coef_maroon)))
ggplot(deciles) +
geom_point(aes(x = count, y = coef_navy, color = 'navy'),
position = position_nudge(x = -0.05)) +
geom_point(aes(x = count, y = coef_maroon*scl-1900, color = 'maroon'),
position = position_nudge(x = 0.05)) +
geom_errorbar(aes(x = count, ymin = ci_lower_navy,
ymax = ci_upper_navy, color = 'navy', width = 0),
position = position_nudge(x = -0.05)) +
geom_errorbar(aes(x = count, ymin = ci_lower_maroon*scl-1900,
ymax = ci_upper_maroon*scl-1900,
color = 'maroon', width = 0),
position = position_nudge(x = 0.05)) +
labs(x = "Group", y = "") +
scale_color_manual(values = c('maroon', 'navy')) +
scale_y_continuous(sec.axis = sec_axis(~(.+1900)/scl, name = "2nd axis"),
expand = c(0.01,0.01))
由reprex package (v2.0.1) 于 2021-09-02 创建