ggplot density2d中不重叠的多边形 [英] non-overlapping polygons in ggplot density2d

查看:122
本文介绍了ggplot density2d中不重叠的多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在ggplot2中重新创建一个填充的轮廓图,但创建的多边形会导致一些丑陋的裁剪:

考虑示例数据:

  require(ggplot2)
require(MASS)
x< - runif(10000)
y< - rnorm( 10000)
dt < - data.frame(x = x,y = y)

使用 filled.contour ,我可以使用

  k <-kde2d(x,y)
filled.contour(k,xlim = c(0,1),ylim = c(-3,3))
然而,使用ggplot时,密度多边形会在与密度估计的某个边界相交处得到剪切。



$ b

pre $ p < - ggplot(dt,aes(x = x,y = y))
dens < - stat_density2d(aes(fill = .. level ..) ,geom =polygon)
p + dens



解决方案



user20650的注释确实解决了这个问题,这似乎是runif在[0,1]上有支持,但核心密度估计有点儿除此之外:

以下是他未来参考的解决方案:

  p <-ggplot(dt,aes(x = x,y = y))
dens < - stat_density2d(aes(fill = .. level ..),geom =polygon)
p + dens + scale_x_continuous(limit = c(-0.1,1.1)))

解决方案

user20650确实解决了这个问题,这似乎是runif在[0,1]上有支持,但是核心密度估计有点超出了这个范围:

这是他的解决方案,我可以解决这个问题。

  p < -  ggplot(dt,aes(x = x,y = y) )
dens < - stat_density2d(aes(fill = .. level ..),geom =polygon)
p + dens + scale_x_continuous(limit = c(-0.1,1.1)))


I am trying to recreate a filled contourplot in ggplot2, but the polygons created result in some ugly clipping:

Consider the sample data:

require(ggplot2)
require(MASS)
x <- runif(10000)
y <- rnorm(10000)
dt <- data.frame(x=x, y=y)

Using filled.contour, I can get a density plot using

k <- kde2d(x,y)
filled.contour(k,xlim=c(0,1), ylim=c(-3,3))

However, using ggplot the density polygons get cut where they intersect some boundary of the density estimate.

p <- ggplot(dt, aes(x=x, y=y)) 
dens <- stat_density2d(aes(fill=..level..), geom="polygon")
p + dens

Solution

The comment by user20650 indeed solved the issue, which seemed to be that runif has support on [0,1], but the kernel density estimates are a bit beyond that:

Here is his solution for future reference:

p <- ggplot(dt, aes(x=x, y=y)) 
dens <- stat_density2d(aes(fill=..level..), geom="polygon")
p + dens + scale_x_continuous(limit=c(-0.1, 1.1)))

解决方案

The comment by user20650 indeed solved the issue, which seemed to be that runif has support on [0,1], but the kernel density estimates are a bit beyond that:

Here is his solution so I can close the issue.

p <- ggplot(dt, aes(x=x, y=y)) 
dens <- stat_density2d(aes(fill=..level..), geom="polygon")
p + dens + scale_x_continuous(limit=c(-0.1, 1.1)))

这篇关于ggplot density2d中不重叠的多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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