stat_contour not able to generate contour lines

后端 未结 3 1366
清酒与你
清酒与你 2020-12-01 12:54

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 val

3条回答
  •  被撕碎了的回忆
    2020-12-01 13:18

    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.

提交回复
热议问题