ggplot与两个Y轴在R? [英] ggplot with two y-axis in R?

查看:63
本文介绍了ggplot与两个Y轴在R?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前已经问过这个问题,但是有人关闭了它,表明它已经回答了.我很困惑如何在两个 Y轴上获得两个变量 plotted .我想在 y轴上 plot Level 并在 y轴上 Flow (即辅助轴).这是我的数据,希望得到您的答复.

I have asked this question earlier but someone close it indicating that it has answer. I am pretty confuse how i can get the two variables plotted on two Y-axis. I want to plot Level on the left y-axis and Flow on the right y-axis (ie., secondary axis). Here is my data and i would appreciate a response.

library(tidyverse)
library(lubridate)

set.seed(1500)

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))
ggplot(data = FakeData, aes(x = Date))+
  geom(aes(y = Level))

这是我想看到的情节的示例输出

Here is an example output of the plot i would like to see

推荐答案

这是一个开始:

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

这篇关于ggplot与两个Y轴在R?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆