contour无法生成轮廓线

contour无法生成轮廓线

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

问题描述

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

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:

该示例看起来与在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无法生成轮廓线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:44