两个 geom_line() 各有一个 y 轴
我想绘制两个变量的时间序列图 - 但它们两个的尺度非常不同。因此,我想绘制一条遵循左侧 y 轴刻度(例如,以百万为单位)的线,另一条线遵循辅助右侧 y 轴(以较低的百分比比例)。有人知道怎么做吗?我已经实现了按照我的意愿放置两个 y 轴刻度,但我需要其中一条线(绘制的两个变量之一)来跟随右侧的 y 轴。这是一些代码。它还不是非常整洁,但为了我的目的,它应该可以工作。
europe_data %>%
rename(`Inflation Rate` = EA19CPALTT01GYM,
`Money Supply` = MYAGM2EZM196N) %>%
mutate(`Inflation Rate` = (`Inflation Rate` * 100)) %>%
ggplot(aes(x = DATE)) +
geom_line(aes(y = `Inflation Rate`),
linetype = "dashed") +
geom_line(aes(y = `Money Supply`)) +
scale_y_continuous(name = "Inflation Rate",
sec.axis = sec_axis(~ . / 1000000000000,
name = "Money Supply"))
提前致谢。
I want to make a plot of a time series of two variables -- but the two of them have very different scales. So I want to plot one line following the scale of the left y-axis (say, in millions) and the other line following the secondary right y-axis (in a low percentage scale). Does anybody know how to make it? I have already achieved to put the two y-axis scales as I wish, but I need one of the lines (one of the two variables plotted) to follow the y-axis on the right. Here's some piece of code. It's not super tidy yet but for the sake of my purpose it should work.
europe_data %>%
rename(`Inflation Rate` = EA19CPALTT01GYM,
`Money Supply` = MYAGM2EZM196N) %>%
mutate(`Inflation Rate` = (`Inflation Rate` * 100)) %>%
ggplot(aes(x = DATE)) +
geom_line(aes(y = `Inflation Rate`),
linetype = "dashed") +
geom_line(aes(y = `Money Supply`)) +
scale_y_continuous(name = "Inflation Rate",
sec.axis = sec_axis(~ . / 1000000000000,
name = "Money Supply"))
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以给你一个使用
base
的解决方案。您可以使用此代码:首先我创建了一个示例数据集:
之后您可以使用此代码:
输出:
I can give you maybe a solution using
base
. You can use this code:First I created a sample dataset:
After that you can use this code:
Output:
当创建带有第二个轴的绘图时,您需要记住该轴仅用于“装饰”。添加辅助轴并不意味着数据将以相同的比例显示。您还需要修改第二个轴上表示的数据,使其与第一个轴上表示的数据的比例大致相同。在这里,你的通胀数据大约在-1到4的范围内,但你的货币供应量大约是5到12万亿。因此,您需要将货币供应量除以大约 5 万亿 (2 * 10^13),才能得到与通货膨胀相同的规模。然后,无论您对数据做了什么,都会在辅助轴上反转。由于这些数字太大,人们很难轻松阅读,因此我实际上只需乘以 5 而不是 5 万亿,并将轴标记为显示万亿:
由 reprex 创建于 2022 年 3 月 12 日包 (v2.0.1)
数据
When creating a plot with a second axis, you need to remember that the axis is only there "for decoration". Adding a secondary axis doesn't mean the data will both appear on the same scale. You need to modify the data that is represented on the second axis too, so that it is at approximately the same scale as the data represented on the first axis. Here, your inflation data is in the range of about -1 to 4, but your money supply is about 5 to 12 trillion. You therefore need to divide the money supply by about 5 trillion (2 * 10^13) to get it on the same scale as the inflation. Then, whatever you have done to the data, you reverse on the secondary axis. Because these numbers are so large, and difficult for people to read easily, I would actually just multiply by 5 instead of 5 trillion, and label the axis as showing trillions:
Created on 2022-03-12 by the reprex package (v2.0.1)
Data