我试图将一些NMDS坐标绘制为x和y,并使用多样性度量(shannon)绘制为轮廓但我不断得到以下错误,我不明白为什么......
Error in if (empty(new)) return(data.frame()) :
missing value where TRUE/FALSE needed
我的代码是:
all_merge <-read.table("test.txt", header=TRUE)
p <- ggplot(all_merge, aes(NMDS1, NMDS2, z = shannon))
p + geom_contour()
我的数据集是:
NMDS1 NMDS2 shannon
-0.287555952 -0.129887595 9.516558582
-0.314104852 -0.048655648 8.985087924
-0.214910534 -0.127167065 8.928241917
-0.341295065 -0.296282805 8.315476782
-0.470025718 0.083835083 8.494348157
-0.429386114 0.044064347 8.669813919
-0.427608469 0.124631936 8.15886319
-0.584412991 0.257278736 8.469688185
-0.436526047 -0.070633108 8.496878956
-0.584707076 0.120411579 8.319057817
0.183493022 0.445239412 5.611249955
0.172968855 0.583787121 5.728358304
-0.404838098 -0.0271276 8.679667562
-0.458718755 -0.05638174 8.714026645
0.458621093 -0.186746574 8.094002558
1.148457698 0.044192391 6.058046032
0.346825668 0.258443444 6.682765975
0.753149083 -0.393018506 7.622032803
1.331546069 -0.515095457 5.784195943
0.236285309 0.2553056 7.210095451
0.346995457 -0.816928807 7.198583726
0.137626646 0.129803823 7.663931393
0.340733689 -0.461201268 5.845269914
-0.675116235 -0.037255181 8.371975231
-0.656523041 -0.025798291 8.438133054
-0.578757804 -0.073169316 8.411583639
-0.602672875 0.015207904 8.137468395
0.413598703 0.320133927 5.91489704
0.891714173 1.032329752 3.612230592
0.378252162 0.054121091 7.903450498
0.401158365 0.009307957 8.164654685
-0.074266368 -0.512745143 8.956733268
答案 0 :(得分:8)
geom_contour
(stat_contour
)不适用于不规则网格(请参阅here)。创建常规网格的一种方法是使用包interp
中的插值函数akima
。
library(akima)
library(reshape2)
# interpolate data to regular grid
d1 <- with(all_merge, interp(x = NMDS1, y = NMDS2, z = shannon))
# melt the z matrix in d1 to long format for ggplot
d2 <- melt(d1$z, na.rm = TRUE)
names(d2) <- c("x", "y", "shannon")
# add NMDS1 and NMDS2 from d1 using the corresponding index in d2
d2$NMDS1 <- d1$x[d2$x]
d2$NMDS2 <- d1$y[d2$y]
# plot
ggplot(data = d2, aes(x = NMDS1, y = NMDS2, fill = shannon, z = shannon)) +
geom_tile() +
stat_contour()