ggplot2 中具有特定面转换的辅助轴

use*_*702 5 r ggplot2

我有一个具有多个方面的 ggplot2 条形图,并且想在每个方面叠加具有不同 Y 轴的线图。我面临的问题是每个方面的主 Y 轴和次 Y 轴之间的关系都不同。有没有办法使辅助轴缩放到线图数据,以便在每个方面使用图的完整高度?

这是一个最小的示例,理想情况下,左轴在两个方面的范围应为 (0, 30),但右轴的范围应为 (0, 130) 和 (0, 10):

library(ggplot2)

temp <- data.frame(
  region = c("1", "1", "1", "1", "1", "2", "2", "2", "2", "2"),
  year = c(2010, 2011, 2012, 2013, 2014, 2010, 2011, 2012, 2013, 2014),
  temp = c(22, 23, 20, 17, 26, 21, 28, 19, 17, 22)
)

wind <- data.frame(
  region = c("1", "1", "1", "1", "1", "2", "2", "2", "2", "2"),
  year = c(2010, 2011, 2012, 2013, 2014, 2010, 2011, 2012, 2013, 2014),
  wind = c(112, 125, 124, 119, 108, 7, 3, 8, 5, 2)
)

ggplot() +
  geom_bar(data = temp, aes(year, temp, fill = region), stat = "identity") +
  geom_line(data = wind, aes(year, wind)) +
  facet_wrap(~region, nrow = 2, scales = "free") +
  scale_y_continuous(sec.axis = sec_axis(~., name = "wind"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述