简体   繁体   English

如何 plot 两个 Y 轴刻度相同但值不同的图

[英]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.

相关问题 如何使用ggplot将两个不同比例的y轴放在绘图的同一侧? - How to put two y-axis with different scale on the same side of the plot with ggplot? 如何在同一面板的同一X轴上绘制具有两个不同y轴范围的点? - How do I plot points with two different y-axis ranges on the same panel in the same X axis? 如何在具有一个x轴和两个y轴的图中将误差线缩放到右y轴的尺寸 - How to scale the errorbars to the dimensions of the right y-axis in a plot with one x-axis and two y-axis ggplot2-如何使用主要和次要y轴在同一图上对具有不同比例的两个变量进行箱图绘制? - ggplot2 - How to boxplot two variables with different scales on same plot using a primary and secondary y-axis? 在ggplot中重叠两个具有不同y轴的图 - Overlapping two graphs with different y-axis in ggplot 如何将自动绘图中呈现的两个图中的线系列组合成一个 plot,共享相同的 y 轴和两个不同的 x 轴? - How to combine the line series in two plots rendered in autoplot into a single plot, sharing the same y-axis and with two different x-axes? 如何在两个图表ggplot 2上对齐y轴,以使它们具有相同的范围和增量 - How to align y-axis on two graphs ggplot 2 so that they have the same range and increments 如何在 R 中更改散点 plot 中 Y 轴的比例(间隔)? - How to change the scale(interval) of Y-axis in a scatter plot in R? 如何更改预测对象图中的 y 轴比例? - How to change the y-axis scale in plot for a forecast object? 标尺未显示在图的y轴上 - Scale is not shown on y-axis of plot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM