ggplot与两个Y轴在R?

时间:2020-06-25 15:52:43

标签: r ggplot2 tidyverse yaxis

我之前已经问过这个问题,但是有人关闭了它,表明它已经回答了。我很困惑如何在两个plotted上获得两个变量Y-axis。我想在plot的y轴上Level left,在Flow的{​​{1}}(即次级轴)上。这是我的数据,希望得到您的答复。

right y-axis

这是我想看到的情节的示例输出 enter image description here

1 个答案:

答案 0 :(得分:2)

这是一个开始:

FakeData <- data.frame(Date = seq(as.Date("2020-01-01"), to = as.Date("2020-01-31"), by = "days"),
                       Level = runif(31, 0, 30), Flow = runif(31, 1,10))

scale_factor <- 4
   ggplot(data = FakeData, aes(x = Date))+
   geom_col(aes(y = Level), fill="darkgreen") +
   geom_line(aes(y = Flow*scale_factor), color="blue") +
   scale_y_continuous(sec.axis = sec_axis(~ .*1, labels = number_format(scale=1/scale_factor), name="Flow"))

enter image description here