使用两个y轴绘图:反转第一个y轴但不反转第二个y轴

huangapple go评论66阅读模式
英文:

Plot with 2 y-axes : reversing the first one but not the second

问题

Sure, here's the translation of the relevant parts:

首先,我绘制了一个深度与年龄关系的折线图。然后,我添加了第二个y轴:沉积速率。

问题是,我的第一个y轴(深度)应该是反向的,从0(顶部)到310(底部)。

但第二个y轴必须是正常的,从0(底部)到34(顶部)。

这是我的代码:

library(ggplot2)

df <- data.frame(
  Depth_cm = c(5, 10, ...),  # Truncated for brevity
  Year = c(1859, 1712, ...),  # Truncated for brevity
  Incertainity = c(66, 66, ...),  # Truncated for brevity
  Rate = c(34, 34, ...)  # Truncated for brevity
)

ggplot(df, aes(x = Year, y = Depth_cm)) +
  geom_ribbon(aes(xmin = Year - Incertainity, xmax = Year + Incertainity), alpha = 0.6, fill = "green3", size = 1) +
  geom_line(aes(linetype = ifelse((Year >= 2006 & Year <= 537) | (Year >= -13716 & Year <= -17097), "solid", "dotted"))) +
  geom_point(data = df[df$Year %in% c(537, -2102, -4417, -13716),], shape = "triangle", color = "red3", size = 2) +
  geom_line(aes(y = (Rate*10)))+
  scale_y_continuous(name = "Depth", sec.axis = sec_axis(~ . /10, name = "Sedimentation rate (cm/kyr)", breaks = seq(0, 34, by = 2))) +
  scale_x_continuous(name = "Year (AD/BC)", limits = c(-17500, 2100), breaks = seq(-17500, 2100, by = 1500)) +
  labs(x = "Year (AD/BC)", y = "Depth (cm)") +
  guides(linetype = FALSE) +
  theme_classic()

深度轴需要反转,但沉积速率轴不需要。

另外,我不知道是否可能:我想使第二个y轴变小,就像图片上的那样。是否有相关的函数?

英文:

First, I plotted a line chart of Depth in function of Age. Then, I added a second y-axis : Sedimentation Rate.

The problem is that my first y-axis (Depth) should be reversed, going from 0 (top) to 310 (bottom).

But the second y-axis have to be normal from 0 (bottom) to 34 (top).
there is my code :

library(ggplot2)

df &lt;- data.frame(Depth_cm = c(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 295, 300, 305, 310
),
Year = c(1859, 1712, 1565, 1418, 1272, 1125, 978, 831, 684, 537, 244, -49, -343, -636, -929, -1222, -1516, -1809, -2102, -2280, -2458, -2636, -2814, -2992, -3170, -3348, -3527, -3705, -3883, -4061, -4239, -4417, -4840, -5262, -5685, -6108, -6530, -6953, -7376, -7798, -8221, -8644, -9066, -9489, -9912, -10334, -10757, -11180, -11603, -12025, -12448, -12871, -13293, -13716, -14139, -14561, -14984, -15407, -15829, -16252, -16675, -17097),
Incertainity = c(66, 66, 66, 66, 66, 66, 66, 66, 66, 115, 115, 115, 115, 115, 115, 115, 115, 115, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133),
Rate = c(34, 34, 34, 34, 34, 34, 34, 34, 34, 17, 17, 17, 17, 17, 17, 17, 17, 17, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12))


ggplot(df, aes(x = Year, y = Depth_cm)) +
  geom_ribbon(aes(xmin = Year - Incertainity, xmax = Year + Incertainity), alpha = 0.6, fill = &quot;green3&quot;, size = 1) +
  geom_line(aes(linetype = ifelse((Year &gt;= 2006 &amp; Year &lt;= 537) | (Year &gt;= -13716 &amp; Year &lt;= -17097), &quot;solid&quot;, &quot;dotted&quot;))) +
  geom_point(data = df[df$Year %in% c(537, -2102, -4417, -13716),], shape = &quot;triangle&quot;, color = &quot;red3&quot;, size = 2) +
  geom_line(aes(y = (Rate*10)))+
  scale_y_continuous(name = &quot;Depth&quot;, sec.axis = sec_axis(~ . /10, name = &quot;Sedimentation rate (cm/kyr)&quot;, breaks = seq(0, 34, by = 2))) +
  scale_x_continuous(name = &quot;Year (AD/BC)&quot;, limits = c(-17500, 2100), breaks = seq(-17500, 2100, by = 1500)) +
  labs(x = &quot;Year (AD/BC)&quot;, y = &quot;Depth (cm)&quot;) +
  guides(linetype = FALSE) +
  theme_classic()

Depth axis needs to be reverse, but not the sedimentation rate axis

I tried the function scale_y_reverse, and also tried to use trans="reverse". But it change both of the axes.

I need to reverse only the Depth axis.

Also, I don't know if it is possible : I would like to make the second y-axis smaller like on the picture. Is there a function for that ? plot model that I would like to repeoduce

答案1

得分: 3

你可以在 scale_y_continuous 中提取偏移量来更改 labels,如果我理解你的意思的话,像这样:

df <- data.frame(Depth_cm = c(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 295, 300, 305, 310 ), Year = c(1859, 1712, 1565, 1418, 1272, 1125, 978, 831, 684, 537, 244, -49, -343, -636, -929, -1222, -1516, -1809, -2102, -2280, -2458, -2636, -2814, -2992, -3170, -3348, -3527, -3705, -3883, -4061, -4239, -4417, -4840, -5262, -5685, -6108, -6530, -6953, -7376, -7798, -8221, -8644, -9066, -9489, -9912, -10334, -10757, -11180, -11603, -12025, -12448, -12871, -13293, -13716, -14139, -14561, -14984, -15407, -15829, -16252, -16675, -17097), Incertainity = c(66, 66, 66, 66, 66, 66, 66, 66, 66, 115, 115, 115, 115, 115, 115, 115, 115, 115, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133), Rate = c(34, 34, 34, 34, 34, 34, 34, 34, 34, 17, 17, 17, 17, 17, 17, 17, 17, 17, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12))

library(ggplot2)
ggplot(df, aes(x = Year, y = Depth_cm)) + 
  geom_ribbon(aes(xmin = Year - Incertainity, xmax = Year + Incertainity), 
              alpha = 0.6, fill = "green3", size = 1) + 
  geom_line(aes(linetype = ifelse((Year >= 2006 & Year <= 537) | (Year >= -13716 & Year <= -17097), "solid", "dotted"))) + 
  geom_point(data = df[df$Year %in% c(537, -2102, -4417, -13716),], shape = "triangle", color = "red3", size = 2) + 
  geom_line(aes(y = (Rate*10)))+ 
  scale_y_continuous(name = "深度 (cm)", labels = function(x) abs(x - 300),
                     sec.axis = sec_axis(~ . /10, name = "沉积速率 (cm/千年)", breaks = seq(0, 34, by = 2))) + 
  scale_x_continuous(name = "年份 (公元/公元前)", limits = c(-17500, 2100), breaks = seq(-17500, 2100, by = 1500)) + labs(x = "年份 (公元/公元前)", y = "深度 (cm)") + 
  guides(linetype = FALSE) + 
  theme_classic()

如果你想要添加更多的 breaks,你可以像这样添加(这里我也改变了 limits 和偏移量):

library(ggplot2)
ggplot(df, aes(x = Year, y = Depth_cm)) + 
  geom_ribbon(aes(xmin = Year - Incertainity, xmax = Year + Incertainity), 
              alpha = 0.6, fill = "green3", size = 1) + 
  geom_line(aes(linetype = ifelse((Year >= 2006 & Year <= 537) | (Year >= -13716 & Year <= -17097), "solid", "dotted"))) + 
  geom_point(data = df[df$Year %in% c(537, -2102, -4417, -13716),], shape = "triangle", color = "red3", size = 2) + 
  geom_line(aes(y = (Rate*10)))+ 
  scale_y_continuous(name = "深度 (cm)", 
                     labels = function(x) abs(x - 310), limits = c(0, 310),
                     breaks = seq(0, 310, by = 10),
                     sec.axis = sec_axis(~ . /10, name = "沉积速率 (cm/千年)", breaks = seq(0, 34, by = 2))) + 
  scale_x_continuous(name = "年份 (公元/公元前)", limits = c(-17500, 2100), breaks = seq(-17500, 2100, by =

<details>
<summary>英文:</summary>

You could change the `labels` by extracting an offset if I understand you correctly in `scale_y_continuous` like this:

``` r
df &lt;- data.frame(Depth_cm = c(5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175, 180, 185, 190, 195, 200, 205, 210, 215, 220, 225, 230, 235, 240, 245, 250, 255, 260, 265, 270, 275, 280, 285, 290, 295, 300, 305, 310 ), Year = c(1859, 1712, 1565, 1418, 1272, 1125, 978, 831, 684, 537, 244, -49, -343, -636, -929, -1222, -1516, -1809, -2102, -2280, -2458, -2636, -2814, -2992, -3170, -3348, -3527, -3705, -3883, -4061, -4239, -4417, -4840, -5262, -5685, -6108, -6530, -6953, -7376, -7798, -8221, -8644, -9066, -9489, -9912, -10334, -10757, -11180, -11603, -12025, -12448, -12871, -13293, -13716, -14139, -14561, -14984, -15407, -15829, -16252, -16675, -17097), Incertainity = c(66, 66, 66, 66, 66, 66, 66, 66, 66, 115, 115, 115, 115, 115, 115, 115, 115, 115, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133, 133), Rate = c(34, 34, 34, 34, 34, 34, 34, 34, 34, 17, 17, 17, 17, 17, 17, 17, 17, 17, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12))

library(ggplot2)
ggplot(df, aes(x = Year, y = Depth_cm)) + 
  geom_ribbon(aes(xmin = Year - Incertainity, xmax = Year + Incertainity), 
              alpha = 0.6, fill = &quot;green3&quot;, size = 1) + 
  geom_line(aes(linetype = ifelse((Year &gt;= 2006 &amp; Year &lt;= 537) | (Year &gt;= -13716 &amp; Year &lt;= -17097), &quot;solid&quot;, &quot;dotted&quot;))) + 
  geom_point(data = df[df$Year %in% c(537, -2102, -4417, -13716),], shape = &quot;triangle&quot;, color = &quot;red3&quot;, size = 2) + 
  geom_line(aes(y = (Rate*10)))+ 
  scale_y_continuous(name = &quot;Depth&quot;, labels = function(x) abs(x - 300),
                     sec.axis = sec_axis(~ . /10, name = &quot;Sedimentation rate (cm/kyr)&quot;, breaks = seq(0, 34, by = 2))) + 
  scale_x_continuous(name = &quot;Year (AD/BC)&quot;, limits = c(-17500, 2100), breaks = seq(-17500, 2100, by = 1500)) + labs(x = &quot;Year (AD/BC)&quot;, y = &quot;Depth (cm)&quot;) + 
  guides(linetype = FALSE) + 
  theme_classic()

使用两个y轴绘图:反转第一个y轴但不反转第二个y轴<!-- -->

If you want to add more breaks you could add them like this (here I also changed the limits and offset):

library(ggplot2)
ggplot(df, aes(x = Year, y = Depth_cm)) + 
  geom_ribbon(aes(xmin = Year - Incertainity, xmax = Year + Incertainity), 
              alpha = 0.6, fill = &quot;green3&quot;, size = 1) + 
  geom_line(aes(linetype = ifelse((Year &gt;= 2006 &amp; Year &lt;= 537) | (Year &gt;= -13716 &amp; Year &lt;= -17097), &quot;solid&quot;, &quot;dotted&quot;))) + 
  geom_point(data = df[df$Year %in% c(537, -2102, -4417, -13716),], shape = &quot;triangle&quot;, color = &quot;red3&quot;, size = 2) + 
  geom_line(aes(y = (Rate*10)))+ 
  scale_y_continuous(name = &quot;Depth&quot;, 
                     labels = function(x) abs(x - 310), limits = c(0, 310),
                     breaks = seq(0, 310, by = 10),
                     sec.axis = sec_axis(~ . /10, name = &quot;Sedimentation rate (cm/kyr)&quot;, breaks = seq(0, 34, by = 2))) + 
  scale_x_continuous(name = &quot;Year (AD/BC)&quot;, limits = c(-17500, 2100), breaks = seq(-17500, 2100, by = 1500)) + labs(x = &quot;Year (AD/BC)&quot;, y = &quot;Depth (cm)&quot;) + 
  guides(linetype = FALSE) + 
  theme_classic()

使用两个y轴绘图:反转第一个y轴但不反转第二个y轴<!-- -->

<sup>Created on 2023-06-29 with reprex v2.0.2</sup>

答案2

得分: 0

感谢两位贡献者,最终代码和结果:
(数据仍然可在初始问题帖子中找到)

ggplot(df, aes(x = Year, y = rev(Depth_cm))) + geom_ribbon(aes(xmin = Year - Incertainity, xmax = Year + Incertainity), alpha = 0.6, fill = "green3", size = 1) + geom_line(aes(linetype = ifelse((Year >= 2006 & Year <= 537) | (Year >= -13716 & Year <= -17097), "solid", "dotted"))) + geom_line(aes(y = (Rate/.35))) + scale_y_continuous(name = "Depth (cm)", labels = function(x) abs(x - 300), sec.axis = sec_axis(~ . *0.35, name = "Sedimentation rate (cm/kyr)", breaks = seq(0, 34, by = 6))) + scale_x_continuous(name = "Year (AD/BC)", limits = c(-17500, 2100), breaks = seq(-17500, 2100, by = 1500)) + labs(x = "Year (AD/BC)", y = "Depth (cm)") + guides(linetype = FALSE) + theme_classic()

使用两个y轴绘图:反转第一个y轴但不反转第二个y轴

英文:

Thanks to both contributors, Final code and result :
(data still availaible in the initial question post)

ggplot(df, aes(x = Year, y = rev(Depth_cm))) +  geom_ribbon(aes(xmin = Year - Incertainity, xmax = Year + Incertainity), alpha = 0.6, fill = &quot;green3&quot;, size = 1) +  geom_line(aes(linetype = ifelse((Year &gt;= 2006 &amp; Year &lt;= 537) | (Year &gt;= -13716 &amp; Year &lt;= -17097), &quot;solid&quot;, &quot;dotted&quot;))) + #geom_point(data = df[df$Year %in% c(-13716,-4417 , -2102,  537),], shape = &quot;triangle&quot;, color = &quot;red3&quot;, size = 2) +  geom_line(aes(y = (Rate/.35)))+   scale_y_continuous(name = &quot;Depth (cm)&quot;, labels = function(x) abs(x - 300), sec.axis = sec_axis(~ . *0.35, name = &quot;Sedimentation rate (cm/kyr)&quot;, breaks = seq(0, 34, by = 6))) + scale_x_continuous(name = &quot;Year (AD/BC)&quot;, limits = c(-17500, 2100), breaks = seq(-17500, 2100, by = 1500)) + labs(x = &quot;Year (AD/BC)&quot;, y = &quot;Depth (cm)&quot;) + guides(linetype = FALSE) +  theme_classic()

使用两个y轴绘图:反转第一个y轴但不反转第二个y轴

huangapple
  • 本文由 发表于 2023年6月29日 19:01:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定