R 提取坐标由 ggplot2::stat_density_2d 绘制

标签 r ggplot2

我正在寻找由 ggplot2::geom_density_2d 生成的轮廓线的坐标.

我在寻找 x/y 坐标(的矩阵)列表。每条轮廓线一个。

这应该类似于 contourLines给,但我无法得到想要的结果。

这是来自 ?stat_density2d 的一些示例代码

 m <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
  geom_point() +
  xlim(0.5, 6) +
  ylim(40, 110)
 m + geom_density_2d()

最佳答案

您可以使用 ggplot_build :

ggplot_build(m + stat_density2d())$data[[2]]

  level        x        y piece  group PANEL  colour size linetype alpha
1 0.002 1.633788 40.00000     1 -1-001     1 #3366FF  0.5        1    NA
2 0.002 1.611111 40.11723     1 -1-001     1 #3366FF  0.5        1    NA
3 0.002 1.555556 40.47552     1 -1-001     1 #3366FF  0.5        1    NA
4 0.002 1.526300 40.70707     1 -1-001     1 #3366FF  0.5        1    NA
5 0.002 1.500000 40.91549     1 -1-001     1 #3366FF  0.5        1    NA
6 0.002 1.448020 41.41414     1 -1-001     1 #3366FF  0.5        1    NA
...

关于R 提取坐标由 ggplot2::stat_density_2d 绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36157437/

相关文章:

r - 如何根据第三列的共享 ID 从一个数据帧中获取数据并将其复制到另一个数据帧中的现有列中

r - 如何对数据集进行子集化以删除任何列中具有特定值的行?

r - 如何使用 ggplot2 绘制矩阵并为相同的值保持相同的颜色

r - 在ggplot2中使用geom_violin将统计信息传递给geoms

从具有多个方面的 ggplot2 中的聚集条形图中删除空因子

r - 如何绘制混合箱形图:另一个箱形图上有抖动点的另一个箱形图?

r - quantmod ... 无法获取当天的 OHLCV 符号数据

r - 处理负值时如何修复R中的累积和

r - 寻求 ggplot 2.2.0 破坏的 gtable_add_grob 代码的解决方法

R ggplot2 : stat_count() must not be used with a y aesthetic error in Bar graph