带有 sec.axis 的 ggplot 的 Y 限制

Ank*_*nke 6 r ggplot2

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

我一直在遵循此线程中找到的说明:ggplot with 2 y axis on every side and differentscale

但每次我将 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)  
Run Code Online (Sandbox Code Playgroud)

GG图1

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)  
Run Code Online (Sandbox Code Playgroud)

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)  
Run Code Online (Sandbox Code Playgroud)

GGplot3

an *_*ost 6

从我在代码中看到的,两个比例之间的转换有点太简单了。

为了获得我认为您想要的结果,有必要对温度数据进行标准化(这样您可以改变分布和平均值,并使其适合您的主要 y 尺度),然后计算反向标准化次要 y 轴。

我所说的标准化是指:(Temp - mean(TEMP))/sd(TEMP),其中TEMP是所有值的数组,Temp是要绘制的特定值。

EDIT:由于 ggplot2 只允许转换原始比例,并且没有为辅助轴设置唯一限制的选项,因此没有简单的选项来设置ylim辅助 y 轴。但是,有一种方法可以做到这一点,这很棘手,但我将在这里展示。

通过使用简单的线性模型(或任何其他求解方法)求解两个边界的归一化,可以使辅助 y 轴的变换匹配唯一的限制。我使用的线性变换引入了两个变量ass是结果乘以的缩放因子,它允许改变绘制数据相对于主 y 轴的分布。该变量a沿 y 轴移动生成的变换。

变换是:

y = a + (Temp - mean(TEMP))/sd(TEMP)) * s
Run Code Online (Sandbox Code Playgroud)

计算过程如下:

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']

Run Code Online (Sandbox Code Playgroud)

要将辅助 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"))
Run Code Online (Sandbox Code Playgroud)

温度与降水量图,其中次要 y 轴缩放以适合


Zhi*_*ang 2

这个怎么样:

  ggplot(climate, aes(Month, Precip)) +
    geom_line() +
    geom_line(aes(y = 4.626*Temp), color = "red") +
    scale_y_continuous("Precipitation", sec.axis = sec_axis(~ ./4.626, name = "Temperature"),) +
    scale_x_continuous("Month", breaks = 1:12)   
Run Code Online (Sandbox Code Playgroud)

如果您需要进一步解释,请告诉我。 在此输入图像描述