R - 绘制垂直剖面

R - plot vertical profile

我测量了 CH4 浓度随深度的变化:

df <- structure(list(Depth = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 15L, 16L, 17L), .Label = c("0", "10", 
"12", "14", "16", "18", "2", "20", "22", "24", "26", "28", "30", 
"32", "4", "6", "8", "AR"), class = "factor"), Conc_CH4 = c(4.30769230769231, 
23.1846153846154, 14.5615384615385, 21.1769230769231, 16.2615384615385, 
132.007692307692, 5.86923076923077, 389.353846153846, 823.023076923077, 
948.684615384615, 1436.56923076923, 1939.88461538462, 26.2769230769231, 
27.5538461538462, 19.6461538461538)), .Names = c("Depth", "Conc_CH4"
), row.names = c(NA, -15L), class = "data.frame")

我需要创建一个这样的情节:

但我遇到了一些问题:我数据中的因素顺序错误,我不知道如何使用 ggplot2 绘制此类数据。

有什么想法吗?

这是一个具有基本绘图功能的解决方案(您反转 ylim 的限制):

df$Depth <- as.numeric(as.character(df$Depth))
df <- df[order(df$Depth),]
plot(Depth~Conc_CH4, df, t="l", ylim=rev(range(df$Depth)))

为什么不将 Depth 转换为数字并绘图?

ggplot(transform(df, Depth=as.numeric(as.character(df$Depth))),
                 aes(x=Conc_CH4, y=Depth)) +
    geom_line() + scale_y_reverse()

as.numeric(as.character(...)) 是因为你的 Depth 是一个因子,调用 as.numeric 直接将因子转换为字符串不同于字符。

scale_y_reverse 反转 y 尺度。

如果您的实际数据中的深度为 "AR",您将不得不忽略它们或以其他方式处理它们。