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

时间:2016-03-22 14:40:47

标签: 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()

1 个答案:

答案 0 :(得分:6)

您可以使用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
...