r - 带有 sec.axis 的 ggplot 的 Y 限制

标签 r ggplot2

我需要使用 sec.axis 创建一个双 y 图,但无法让两个轴都正确缩放。

我一直在遵循此线程中的说明:ggplot with 2 y axes on each side and different scales

但是每次我将 ylim.prim 中的下限更改为 0 以外的任何值时,都会弄乱整个情节。出于可视化的原因,我需要两个轴的非常具体的 y 限制。此外,当我将 geom_col 更改为 geom_line 时,它​​也会弄乱辅助轴的限制。

climate <- tibble(
  Month = 1:12,
  Temp = c(23,23,24,24,24,23,23,23,23,23,23,23),
  Precip = c(101,105,100,101,102, 112, 101, 121, 107, 114, 108, 120)
  )

ylim.prim <- c(0, 125)   # in this example, precipitation
ylim.sec <- c(15, 30)    # in this example, temperature

b <- diff(ylim.prim)/diff(ylim.sec)
a <- b*(ylim.prim[1] - ylim.sec[1])

ggplot(climate, aes(Month, Precip)) +
  geom_col() +
  geom_line(aes(y = a + Temp*b), color = "red") +
  scale_y_continuous("Precipitation", sec.axis = sec_axis(~ (. - a)/b, name = "Temperature"),) +
  scale_x_continuous("Month", breaks = 1:12)  

GGplot1
ylim.prim <- c(0, 125)   # in this example, precipitation
ylim.sec <- c(15, 30)    # in this example, temperature

b <- diff(ylim.prim)/diff(ylim.sec)
a <- b*(ylim.prim[1] - ylim.sec[1])

ggplot(climate, aes(Month, Precip)) +
  geom_line() +
  geom_line(aes(y = a + Temp*b), color = "red") +
  scale_y_continuous("Precipitation", sec.axis = sec_axis(~ (. - a)/b, name = "Temperature"),) +
  scale_x_continuous("Month", breaks = 1:12)  

GGplot2
ylim.prim <- c(95, 125)   # in this example, precipitation
ylim.sec <- c(15, 30)    # in this example, temperature

b <- diff(ylim.prim)/diff(ylim.sec)
a <- b*(ylim.prim[1] - ylim.sec[1])

ggplot(climate, aes(Month, Precip)) +
  geom_line() +
  geom_line(aes(y = a + Temp*b), color = "red") +
  scale_y_continuous("Precipitation", sec.axis = sec_axis(~ (. - a)/b, name = "Temperature"),) +
  scale_x_continuous("Month", breaks = 1:12)  

GGplot3

最佳答案

从我在代码中看到的,您在两个比例之间的转换有点太简单了。
为了得到我认为你想要的结果,有必要对温度数据进行归一化(这样你可以改变传播和均值,并使其适合你的主要 y 尺度),然后计算反向归一化次要 y 轴。
归一化我的意思是:(Temp - mean(TEMP))/sd(TEMP) , 其中 TEMP是一个包含所有值的数组,而 Temp是要绘制的特定值。EDIT:由于 ggplot2 只允许对原始比例进行变换,并且没有为辅助轴设置唯一限制的选项,因此没有简单的选项来设置 ylim次 y 轴。但是,有一种方法可以做到这一点,这很hacky,但我会在这里展示。
通过使用简单的线性模型(或任何其他求解方法)求解两个边界的归一化,可以进行次要 y 轴的变换以匹配唯一的限制。
我使用的线性变换引入了两个变量 as . s是结果乘以的比例因子,它允许改变绘制数据相对于主 y 轴的分布。变量 a沿 y 轴移动生成的变换。
变换是:

y = a + (Temp - mean(TEMP))/sd(TEMP)) * s
计算过程如下:
climate <- tibble(
  Month = 1:12,
  Temp = c(23,23,24,24,24,23,23,23,23,23,23,23),
  Precip = c(101,105,100,101,102, 112, 101, 121, 107, 114, 108, 120)
  )

ylim.prim <- c(95, 125)   # in this example, precipitation
ylim.sec <- c(15, 30)    # in this example, temperature

TEMP <- climate$Temp #needed for coherent normalisation

# This is quite hacky, but it works if you want to set a boundary for the secondary y-axis
fit = lm(b ~ . + 0, 
   tibble::tribble(
     ~a, ~s,  ~b,
      1,  (ylim.sec[1] - mean(TEMP))/sd(TEMP),  ylim.prim[1],
      1,  (ylim.sec[2] - mean(TEMP))/sd(TEMP), ylim.prim[2]))

a <- fit$coefficients['a']
s <- fit$coefficients['s']

要将辅助 y 轴比例调整回您的值,只需反向执行计算中的每一步。
通过这种方式,您可以对两个时间序列进行漂亮且可调整的叠加:
ggplot(climate, aes(Month, Precip)) +
  geom_line() + 
  geom_line(aes(y = (a + ((Temp - mean(TEMP))/sd(TEMP)) * s) ), color = "red") +
  scale_y_continuous("Precipitation", 
                     limits=ylim.prim,
                     sec.axis = sec_axis(~ (. - a) / s * sd(TEMP) + mean(TEMP), name = "Temperature"),) +
  scale_x_continuous("Month", breaks = 1:12) +
  theme(axis.title.y.right = element_text(colour = "red"))
temperature vs precipitation plot with secondary y axis scaled to fit

关于r - 带有 sec.axis 的 ggplot 的 Y 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58774705/

相关文章:

r - 向 ggplot 中堆叠的 geom_col 添加唯一标签

r - 在 ggplot2 中自定义轮廓标签

r - ggplot : aes vs aes_string,或如何以编程方式指定列名称?

r - 在 R 中创建子矩阵后,为什么 str() 显示因子级别的信息不正确?

r - 使用 ggmap 创建正交 map

r - 如何使用美学为 geom_hline() 着色?

r - 如何在曲线下创建水平渐变?

r - 如何在 ggplot 中绘制具有可变 bin 宽度的直方图?

r - 绘制带孔的 "donut"多边形

r - R gplots 中的 heatmap.2 错误