【问题标题】:ggplot: scale second axis with error barsggplot:用误差线缩放第二轴
【发布时间】:2021-09-02 03:55:15
【问题描述】:

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

我可以按照说明 here 重新调整坐标轴,但我无法概括说明以包含误差线。

这是我目前的情节

这是我的代码

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"))

我的问题是如何缩放我的坐标轴,以使下降趋势图(左轴)从左上角“开始”,而上升趋势图(右轴)从左下角开始?强>

如您所见,左轴顶部有很多死区,右轴不必低至 -0.05,因为最小的栗色值刚好高于 0。

谢谢

【问题讨论】:

    标签: r ggplot2 scale axes multiple-axes


    【解决方案1】:

    调整比例因子(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 创建

    【讨论】:

    • 这一切都帮了大忙!谢谢你。我绝对认为 1800-2000 范围将是正确的选择。我想我使用栗色线处理的“真实”数据更平坦,所以我希望调整比例而不是移动线,但这绝对满足提供的示例的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多