我是R中等高线图的新手,我正在尝试创建一个显示营养物浓度随深度和盐度的变化。
我的数据集目前看起来像这样(link):
> head(DF)
salinity depth silicon
1 32.9 0.00 3.872717
2 32.9 0.00 3.906963
3 32.9 0.00 3.872717
4 33.4 3.56 3.119292
5 33.5 3.56 3.076484
6 33.0 0.00 3.675799
我想要的是y轴上的深度,x轴上的盐度以及基于颜色显示的硅浓度。
根据我的阅读,为了创建轮廓图,我需要将当前拥有的数据转换为矩阵(通过创建函数?)。
这是可以实现的吗?我不确定我是否会以完全错误的方式解决这个问题,但基本上我想要的是这样的事情(图像质量道歉):
但用盐度代替时间和硅浓度而不是温度。
谢谢,
KEZ
可复制的数据:
DF <- structure(list(salinity = c(32.9, 32.9, 32.9, 33.4, 33.5, 33,
33, 33.2, 33.3, 33.1, 33.1, 33.1, 33.7, 33.7, 34, 34, 34, 33.6,
34.3, 34.3, 34.8, 35.8, 34.7, 34.4, 34.3, 34.5, 34.4, 34.9, 34.9,
34.9, 34.8, 35, 35, 36, 34.9, 35, 35.2, 35.1, 30.2, 33.4, 34.5,
34.9, 33.4, 33.4, 35.1, 35.1, 34.6, 35.1, 34.43, 34.67, 34.67,
34.96, 34.76, 35.11, 34.14, 34.97, 25.13, 35.16, 35.11, 35.11,
35.11, 35.15), depth = c(0, 0, 0, 3.56, 3.56, 0, 0, 4.493, 4.493,
0, 0, 0, 4.362, 4.362, 9.9, 9.9, 0, 0, 5.826, 5.826, 11.725,
11.725, 11.725, 0, 0, 2.766, 2.766, 9.355, 9.355, 0, 0, 12.46,
12.46, 12.46, 0, 0, 12.427, 12.427, 1.2, 3.6, 6.2, 11, 1.1, 1.1,
4.2, 12.8, 6.9, 10.4, 1.16, 4.5, 4.5, 15.35, 1.13, 8.25, 17.92,
1.05, 14.25, 20.54, 0.97, 0.97, 7.67, 19.6), silicon = c(3.872716895,
3.90696347, 3.872716895, 3.119292237, 3.076484018, 3.675799087, 3.855593607,
3.547374429, 3.299086758, 4.591894977, 4.566210046, 4.857305936, 2.759703196,
2.5456621, 2.597031963, 2.126141553, 2.417237443, 2.331621005, 1.989155251,
1.835045662, 1.946347032, 1.937785388, 1.526826484, 1.638127854, 1.929223744,
1.698059361, 1.894977169, 1.312785388, 1.698059361, 1.329908676, 1.484018265,
1.621004566, 1.175799087, 1.167237443, 1.218607306, 1.038812785, 1.552511416,
1.141552511, 5.329861111, 1.684027778, 2.612847222, 1.840277778, 1.588541667,
1.553819444, 2.682291667, 1.692708333, 1.111111111, 1.935763889, 0.815972222,
1.197916667, 1.197916667, 1.796875, 1.258680556, 1.059027778, 1.25, 0.512152778,
1.336805556, 1.284722222, 0.998263889, 0.928819444, 0.399305556, 1.814236111
)), .Names = c("salinity", "depth", "silicon"), class = "data.frame", row.names = c(NA,
-62L))
编辑:对于任何有兴趣的人,在Frank的帖子的帮助下,我能用我的完整数据集创建以下内容:
答案 0 :(得分:2)
您可以使用interp
包中的akima
函数进行插值。否则,您必须确定如何处理缺少数据的区域。
library(akima)
s <- interp(DF$salinity, DF$depth, DF$silicon, duplicate="mean",
xo=seq(min(DF$salinity), max(DF$salinity), length=50),
yo=seq(min(DF$depth), max(DF$depth), length=50))
# you can choose values other than length = 50.
# Note that I used duplicate = "mean", but you can pick your own way of handling duplicates
然后,有许多绘图选项,每个选项都有很多自定义空间。以下是一些选择:
filled.contour(s, color = terrain.colors)
image(s, col=rainbow(60))
library(fields); image.plot(s)
library(ggplot2)
ggs <- data.frame(salinity = rep(s$x, each=length(s$x)), depth = s$y, silicon = as.vector(t(s$z)))
p <- ggplot(ggs, aes(salinity, depth, fill=silicon))
p + geom_raster() + scale_fill_continuous(low="green", high="red") + theme_bw()