我测量了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绘制这种数据。
有什么想法吗?
答案 0 :(得分:2)
以下是基础绘图功能的解决方案(您可以反转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)))
答案 1 :(得分:1)
为什么不将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”深度,则必须省略或处理它们。