【问题标题】:How to obtain information about contours from ggplot2::geom_density_2d_filled?如何从 ggplot2::geom_density_2d_filled 获取有关轮廓的信息?
【发布时间】:2021-10-13 06:39:40
【问题描述】:

我可以用 ggplot2 绘制热图。例如

library(tidyverse)

gg <- ggplot(mtcars) + geom_density_2d_filled(aes(x = mpg, y = disp)) +
   theme(legend.position = "none")

是否可以从ggplot 对象获取有关热图轮廓的信息?我需要x, yz 类似于 MASS::kde2d 输出或其他形式。

【问题讨论】:

    标签: r ggplot2 heatmap


    【解决方案1】:

    这篇博文似乎有了答案——https://www.javaer101.com/en/article/42324657.html

    gdata <- layer_data(gg)    ##  Update - more precise method
    #ggbld <- ggplot_build(gg)
    #gdata <- ggbld$data[[1]]
    head(gdata)
    

    然后你可以逐层绘制来演示:

    gdata %>% ggplot() +geom_point(aes(x, y, color = level))
    

    【讨论】:

      猜你喜欢
      • 2012-05-28
      • 2019-10-16
      • 1970-01-01
      • 2017-03-06
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多