[英]How to plot two graphs with same Y-axis scale but different values
I have been trying to create a plot that shows the difference between two years of the same value.我一直在尝试创建一个 plot 来显示两年相同值之间的差异。 For comparison purposes I want the Y-axis to have the same breaks, however the data differs a lot between years.
出于比较目的,我希望 Y 轴具有相同的中断,但是数据在年份之间差异很大。 GGplot automatically adjusts the axis to this, is there any way to prevent this?
GGplot自动将轴调整到这个,有什么办法可以防止这种情况?
Current issue is visible in the picture.当前问题在图片中可见。 I would like both axis to range up to three hundred as I will eventually compare it to 2022 where the data also ranges higher than 2021.
我希望两个轴的范围都在 300 以内,因为我最终会将其与 2022 年进行比较,其中数据的范围也高于 2021 年。
# Plot for 2020
defplot20 <- ggplot(def20, aes(x=date, y=neerslagtekort)) +
geom_line() + theme_classic() +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week",
date_labels = "%d-%b", expand = c(0, 0)) +
labs(y="Y", x="X") + ggtitle("2020") +
scale_y_continuous(breaks = seq(0, 350, by = 25))
# Plot for 2021
defplot21 <- ggplot(def21, aes(x=date, y=neerslagtekort)) +
geom_line() + theme_classic() +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week",
date_labels = "%d-%b", expand = c(0, 0)) +
labs(y="Y", x="X") + ggtitle("2021") +
scale_y_continuous(breaks = seq(0, 350, by = 25))
ggarrange(defplot20, defplot21)
As @wernor commented, merging the graphs in one graph seems a good option if you want to compare these graphs.正如@wernor 评论的那样,如果您想比较这些图表,将图表合并到一个图表中似乎是一个不错的选择。
If you want to put them next to each other using ggarange
your code is pretty close you just need to add the limits
argument to your scale_y_continuous
to force the axis scale to be the same:如果您想使用
ggarange
将它们彼此相邻放置,您的代码非常接近,您只需将limits
参数添加到您的scale_y_continuous
以强制轴比例相同:
# Plot for 2020
defplot20 <- ggplot(def20, aes(x=date, y=neerslagtekort)) +
geom_line() + theme_classic() +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week",
date_labels = "%d-%b", expand = c(0, 0)) +
labs(y="Y", x="X") + ggtitle("2020") +
scale_y_continuous(limits = c(0, 350), breaks = seq(0, 350, by = 25))
# Plot for 2021
defplot21 <- ggplot(def21, aes(x=date, y=neerslagtekort)) +
geom_line() + theme_classic() +
theme(axis.text.x = element_text(angle = 90)) +
scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 week",
date_labels = "%d-%b", expand = c(0, 0)) +
labs(y="Y", x="X") + ggtitle("2021") +
scale_y_continuous(limits = c(0, 350), breaks = seq(0, 350, by = 25))
ggarrange(defplot20, defplot21)
Be aware!意识到! When using
scale_y_continuous(limits = c(), breaks = seq())
you always need to know your data.使用
scale_y_continuous(limits = c(), breaks = seq())
时,您始终需要了解您的数据。 Otherwise you could make mistakes with zooming to far in or out, losing sight of data and/or datapoints.否则,您可能会在放大或缩小时出错,从而忽略数据和/或数据点。
And without the dput it was not possible to check this code beforehand on your data.如果没有 dput,就无法事先在您的数据上检查此代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.