Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stat_contour not able to generate contour lines

Tags:

r

ggplot2

contour

I need to add lines via stat_contour() to my ggplot/ggplot2-plot. Unfortunately, I can not give you the real data from which point values should be evaluated. However, another easily repreducably example behaves the same:

testPts <- data.frame(x=rep(seq(7.08, 7.14, by=0.005), 200))
testPts$y <- runif(length(testPts$x), 50.93, 50.96)
testPts$z <- sin(testPts$y * 500)

ggplot(data=testPts, aes(x=x, y=y, z=z)) + geom_point(aes(colour=z))
       + stat_contour()

This results in the following error message:

Error in if (nrow(layer_data) == 0) return() : argument is of length zero In addition: Warning message: Not possible to generate contour data

The example looks not different from others posted on stackoverflow or in the official manual/tutorial to me, and it seemingly doesn't matter if I provide more specifications to stat_contour. It seems the function does not pass the data(-layer) as pointed ou tint the error message.

like image 247
Florian R. Klein Avatar asked Sep 28 '13 09:09

Florian R. Klein


People also ask

How do you do a contour plot in ggplot2?

The geom_density_2d function A 2D density contour plot can be created in ggplot2 with geom_density_2d . You just need to pass your data frame and indicate the x and y variable inside aes . It is possible to increase or decrease the number of levels with bins .


3 Answers

Use stat_density2d instead of stat_contour with irregularly spaced data.

library(ggplot2)

testPts <- data.frame(x=rep(seq(7.08, 7.14, by=0.005), 200))
testPts$y <- runif(length(testPts$x), 50.93, 50.96)
testPts$z <- sin(testPts$y * 500)

(ggplot(data=testPts, aes(x=x, y=y, z=z))
+ geom_point(aes(colour=z))
+ stat_density2d()
)

enter image description here

like image 50
Andy W Avatar answered Oct 14 '22 05:10

Andy W


One solution to this problem is the generation of a regular grid and the interpolation of point values in respect to that grid. Here is how I did it for just one of multiple data fields:

pts.grid <- interp(as.data.frame(pts)$coords.x1, as.data.frame(pts)$coords.x2, as.data.frame(pts)$GWLEVEL_TI)
pts.grid2 <- expand.grid(x=pts.grid$x, y=pts.grid$y)
pts.grid2$z <- as.vector(pts.grid$z)

This results in a data frame which can be used in a ggplot in stat_contour() when defined in the data-parameter of that function:

(ggplot(as.data.frame(pts), aes(x=coords.x1, y=coords.x2, z=GWLEVEL_TI))
#+ geom_tile(data=na.omit(pts.grid2), aes(x=x, y=y, z=z, fill=z))
+ stat_contour(data=na.omit(pts.grid2), binwidth=2, colour="red", aes(x=x, y=y, z=z))
+ geom_point()
)

This solution most likely includes unneccessary transformations because I don't know better yet. Furthermore I must make the same grid generation for every data field individually before combining them in a single data frame again - not as efficient as I would like it to be for bigger data sets.

like image 20
Florian R. Klein Avatar answered Oct 14 '22 05:10

Florian R. Klein


You should generate a z for each combination of x and y using expand.grid or outer. For example:

library(ggplot2)
testPts <- transform(expand.grid(x=1:10,y=1:5),z=sin(x*y))
(ggplot(data=testPts, aes(x=x, y=y, z=z))
 + stat_contour()
 + geom_point(aes(colour=z))
)

enter image description here

like image 2
agstudy Avatar answered Oct 14 '22 05:10

agstudy