在R和ggplot中用基本数据集显示轮廓故障 [英] Trouble displaying contours in R and ggplot with basic dataset

查看:128
本文介绍了在R和ggplot中用基本数据集显示轮廓故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一些NMDS坐标绘制为x和y,并使用多样性度量(shannon)将其绘制为轮廓,但我不断收到以下错误,我不明白为什么...

(空(new))返回(data.frame())中的错误:
缺少值,其中TRUE / FALSE需要

我的代码是:

<$ p $ (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.1 64654685
-0.074266368 -0.512745143 8.956733268


解决方案

geom_contour stat_contour )不适用于不规则网格(请参阅 here )。创建常规网格的一种方式是在包 akima 中使用插值函数 interp

  library(akima)
library(reshape2)

#将数据内插到常规网格
d1< ; - with(all_merge,interp(x = NMDS1,y = NMDS2,z = shannon))

#将d1中的z矩阵融合为ggplot的长格式
d2 < - melt (d1 $ z,na.rm = TRUE)
名称(d2)< -c(x,y,shannon)

#将NMDS1和NMDS2从d1使用相应的索引d1
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()


I am trying to plot some NMDS co-ordinates as x and y, and use a diversity measure (shannon)to plot as a contour but I keep getting the following error and I can't see why...

Error in if (empty(new)) return(data.frame()) : 
  missing value where TRUE/FALSE needed

My code is:

all_merge <-read.table("test.txt", header=TRUE)

 p <- ggplot(all_merge, aes(NMDS1, NMDS2, z = shannon))
 p + geom_contour()

and my dataset is:

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

解决方案

geom_contour (stat_contour) does not work on irregular grids (see here). One way to create a regular grid is to use the interpolation function interp in package 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()

这篇关于在R和ggplot中用基本数据集显示轮廓故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆