使用 r 在 ggplot2 中绘制 3 轴图
Drawing a 3 axis graph in ggplot2 with r
我正在尝试在 ggplot2 中重新创建如下图所示的图表。我一直在寻找 3 轴并且似乎正在获得 3d 可视化效果,这显然不是我需要的,所以我的 googlefu 让我失望了。
我的数据很简单:
https://imgur.com/a/eM8tKAG
使用 CTD 列确定不同的图表。
我想我的主要问题是轴的缩放。我的 y 轴是深度。通常将此值取反,因为它将水面置于顶部。
许多人(包括我自己)认为在绘图的同一维度上什至有 2 个轴是次优的。只是有更好的方式来呈现相同的信息。在 ggplot 中可以有第二个轴,但如果你想要第三个轴,你必须自己创建它:
ggplot(df, aes(Salinity, Depth)) +
geom_line(color = "green4") +
geom_line(aes(x = (Temperature - 8.25) * 15 + 29), color = "red3") +
geom_line(aes(x = Silicate * 0.3 + 24.5), col = "deepskyblue3") +
annotate(geom = "text", x = seq(15, 27, 3) * 0.3 + 24.5, y = -0.5,
label = seq(15, 27, 3), col = "deepskyblue3", size = 3.1) +
annotate(geom = "text", color = "deepskyblue3", x=30.87, y = -1.5,
label = "Silicates") +
geom_hline(yintercept = 0, color = "deepskyblue3") +
scale_x_continuous(sec.axis = sec_axis(name = "Temperature",
trans = function(x) {
(x - 28) / 15 + 8.25
})) +
coord_cartesian(ylim = c(-2, 20), clip = "off") +
theme_minimal() +
theme(plot.margin = margin(10, 10, 50, 10),
axis.text.x.top = element_text(colour = "red3"),
axis.title.x.top = element_text(colour = "red3"),
axis.ticks.x.top = element_line(color = "red3"),
axis.line.x.top = element_line(color = "red3"),
axis.text.x.bottom = element_text(colour = "green4"),
axis.title.x.bottom = element_text(colour = "green4"),
axis.ticks.x.bottom = element_line(color = "green4"),
axis.line.x.bottom = element_line(colour = "green4"),
axis.line.y.left = element_line())
数据转录自 OP
df <- data.frame(CTD = c(1, 1, 1, 2, 2),
Depth = c(17.78, 3.89, 1.44, 13.23, 1.34),
Temperature = c(8.28, 8.31, 8.49, 8.25, 8.31),
Salinity = c(31.9, 31.39, 30.45, 32.61, 29.11),
Silicate = c (17.19643, 19.51786, 24.07143, 14.78571, 27.64286))
我正在尝试在 ggplot2 中重新创建如下图所示的图表。我一直在寻找 3 轴并且似乎正在获得 3d 可视化效果,这显然不是我需要的,所以我的 googlefu 让我失望了。 我的数据很简单: https://imgur.com/a/eM8tKAG 使用 CTD 列确定不同的图表。
我想我的主要问题是轴的缩放。我的 y 轴是深度。通常将此值取反,因为它将水面置于顶部。
许多人(包括我自己)认为在绘图的同一维度上什至有 2 个轴是次优的。只是有更好的方式来呈现相同的信息。在 ggplot 中可以有第二个轴,但如果你想要第三个轴,你必须自己创建它:
ggplot(df, aes(Salinity, Depth)) +
geom_line(color = "green4") +
geom_line(aes(x = (Temperature - 8.25) * 15 + 29), color = "red3") +
geom_line(aes(x = Silicate * 0.3 + 24.5), col = "deepskyblue3") +
annotate(geom = "text", x = seq(15, 27, 3) * 0.3 + 24.5, y = -0.5,
label = seq(15, 27, 3), col = "deepskyblue3", size = 3.1) +
annotate(geom = "text", color = "deepskyblue3", x=30.87, y = -1.5,
label = "Silicates") +
geom_hline(yintercept = 0, color = "deepskyblue3") +
scale_x_continuous(sec.axis = sec_axis(name = "Temperature",
trans = function(x) {
(x - 28) / 15 + 8.25
})) +
coord_cartesian(ylim = c(-2, 20), clip = "off") +
theme_minimal() +
theme(plot.margin = margin(10, 10, 50, 10),
axis.text.x.top = element_text(colour = "red3"),
axis.title.x.top = element_text(colour = "red3"),
axis.ticks.x.top = element_line(color = "red3"),
axis.line.x.top = element_line(color = "red3"),
axis.text.x.bottom = element_text(colour = "green4"),
axis.title.x.bottom = element_text(colour = "green4"),
axis.ticks.x.bottom = element_line(color = "green4"),
axis.line.x.bottom = element_line(colour = "green4"),
axis.line.y.left = element_line())
数据转录自 OP
df <- data.frame(CTD = c(1, 1, 1, 2, 2),
Depth = c(17.78, 3.89, 1.44, 13.23, 1.34),
Temperature = c(8.28, 8.31, 8.49, 8.25, 8.31),
Salinity = c(31.9, 31.39, 30.45, 32.61, 29.11),
Silicate = c (17.19643, 19.51786, 24.07143, 14.78571, 27.64286))