如何绘制等高线,显示 95% 的值落在 R 和 ggplot2 中的位置 [英] How to plot a contour line showing where 95% of values fall within, in R and in ggplot2

查看:18
本文介绍了如何绘制等高线,显示 95% 的值落在 R 和 ggplot2 中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有:

x <- norm(1000)y <- 范数(1000)

如何使用 ggplot2 生成包含以下两个几何图形的图:

  1. 两个系列值的二元期望
  2. 显示 95% 的估计值落在何处的等高线?

我知道如何做第一部分:

 df <- data.frame(x=x, y=y)p <- ggplot(df, aes(x=x, y=y))p <- p + xlim(-10, 10) + ylim(-10, 10) # 说p <- p + geom_point(x=mean(x), y=mean(y))

而且我也知道 ggplot2 中的 stat_contour() 和 stat_density2d() 函数.

而且我也知道 stat_contour 中有bins"选项.

但是,我想我需要的是类似于分位数内的 probs 参数,但超过二维而不是一维.

我还在图形包中看到了一个解决方案.但是,我想在 ggplot 中执行此操作.

非常感谢帮助,

乔恩

解决方案

不幸的是,接受的答案目前在 ggplot2 2.1.0 上失败并出现 Error: Unknown parameters:breaks.我根据

提示:ks 包依赖于 rgl 包,手动编译可能很麻烦.即使您在 Linux 上,获得预编译版本也容易得多,例如sudo apt install r-cran-rgl 在 Ubuntu 上,如果你设置了适当的 CRAN 存储库.

Say we have:

x <- rnorm(1000)
y <- rnorm(1000)

How do I use ggplot2 to produce a plot containing the two following geoms:

  1. The bivariate expectation of the two series of values
  2. A contour line showing where 95% of the estimates fall within?

I know how to do the first part:

 df <- data.frame(x=x, y=y)
 p <- ggplot(df, aes(x=x, y=y))
 p <- p + xlim(-10, 10) + ylim(-10, 10) # say
 p <- p + geom_point(x=mean(x), y=mean(y))

And I also know about the stat_contour() and stat_density2d() functions within ggplot2.

And I also know that there are 'bins' options within stat_contour.

However, I guess what I need is something like the probs argument within quantile, but over two dimensions rather than one.

I have also seen a solution within the graphics package. However, I would like to do this within ggplot.

Help much appreciated,

Jon

解决方案

Unfortunately, the accepted answer currently fails with Error: Unknown parameters: breaks on ggplot2 2.1.0. I cobbled together an alternative approach based on the code in this answer, which uses the ks package for computing the kernel density estimate:

library(ggplot2)

set.seed(1001)
d <- data.frame(x=rnorm(1000),y=rnorm(1000))

kd <- ks::kde(d, compute.cont=TRUE)
contour_95 <- with(kd, contourLines(x=eval.points[[1]], y=eval.points[[2]],
                                    z=estimate, levels=cont["5%"])[[1]])
contour_95 <- data.frame(contour_95)

ggplot(data=d, aes(x, y)) +
  geom_point() +
  geom_path(aes(x, y), data=contour_95) +
  theme_bw()

Here's the result:

TIP: The ks package depends on the rgl package, which can be a pain to compile manually. Even if you're on Linux, it's much easier to get a precompiled version, e.g. sudo apt install r-cran-rgl on Ubuntu if you have the appropriate CRAN repositories set up.

这篇关于如何绘制等高线,显示 95% 的值落在 R 和 ggplot2 中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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