stat_contour 无法生成轮廓线 [英] stat_contour not able to generate contour lines

查看:40
本文介绍了stat_contour 无法生成轮廓线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 stat_contour() 添加行到我的 ggplot/ggplot2-plot.不幸的是,我无法为您提供应评估点值的真实数据.但是,另一个易于重现的示例的行为相同:

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:

if (nrow(layer_data) == 0) return() 错误:参数有长度零 另外:警告信息:无法生成轮廓数据

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

对我来说,该示例看起来与在 stackoverflow 或官方手册/教程中发布的其他示例没有什么不同,如果我为 stat_contour 提供更多规范,这似乎无关紧要.该函数似乎没有按照错误消息指出的那样传递数据(-层).

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.

推荐答案

这个问题的一个解决方案是生成一个规则的网格和相对于该网格的点值的插值.以下是我仅针对多个数据字段之一执行此操作的方法:

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)

这会产生一个数据框,当在该函数的数据参数中定义时,该数据框可以在 stat_contour() 中的 ggplot 中使用:

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.

这篇关于stat_contour 无法生成轮廓线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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