Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
744 views
in Technique[技术] by (71.8m points)

ggplot2 - Trouble displaying contours in R and ggplot with basic dataset

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...