ggplot:用误差线缩放第二个轴

ggplot: scale second axis with error bars

我正在尝试使用 ggplot2 绘制两个带有相关误差线的散点图。我希望左轴的顶部为 ~0,范围向下到 -2000,右轴的底部为 0,范围最大为 0.15。

我可以按照说明重新缩放点本身的坐标轴 但我无法概括说明以包含误差线。

这是我目前的剧情

这是我的代码

library(tidyverse)
library(ggplot2)

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-1200, 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-1200, ymax = ci_upper_maroon*scl-1200, color = 'maroon', width = 0)) +   
  labs(x = "Group", y = "") +
  scale_color_manual(values = c('maroon', 'navy')) + 
  scale_y_continuous(sec.axis = sec_axis(~(.+1200)/scl, name = "2nd axis"))

我的问题是如何缩放轴,使向下趋势图(左轴)“从”左上角“开始”,向上趋势图(右轴)从底部开始离开了?

如您所见,左轴顶部有很多死space,右轴不需要低至-0.05,因为最小的栗色值刚好在0以上。

谢谢

调整比例因子(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() 选项,您可以进一步 trim 顶部的 'blank space':

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 创建