R - ggplot - stat_contour not able to generate contour lines

Deadly 提交于 2019-11-26 17:45:20

问题


I am trying to add contour 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 to others posted on stackoverflow or in the official manual/tutorial to me, and it seeminlgy 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.

Thank you for your ideas and advice!


回答1:


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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/19065290/r-ggplot-stat-contour-not-able-to-generate-contour-lines

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!