【问题标题】:make ggplot2:: stat_bin2d show density instead of counts使 ggplot2:: stat_bin2d 显示密度而不是计数
【发布时间】:2016-06-16 07:11:00
【问题描述】:

是否可以让它显示组内密度而不是计数?

library(ggplot2);data(diamonds)
ggplot(diamonds, aes(carat, depth)) +  
  stat_bin2d(bins=40)+ facet_wrap(~color)

这将使比较各组之间的模式变得更加容易,因为某些组可能自然而然地出现得更多。

这个问题有点类似于:How to scale (normalise) values of ggplot2 stat_bin2d within each column (by X axis),它也没有答案。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:
    ggplot(diamonds, aes(carat, depth)) +  
      stat_bin2d(bins=40, aes(fill = ..density..))+ facet_wrap(~color)
    

    或者您会对核密度估计感到满意吗?

    ggplot(diamonds, aes(carat, depth)) +  
      stat_density2d(aes(fill = ..density..), geom = "tile", contour = FALSE, n = 25) + 
      facet_wrap(~color) +
      scale_fill_gradient(low = "light blue", high = "dark red")
    

    或使用默认网格:

    ggplot(diamonds, aes(carat, depth)) +  
      stat_density2d(aes(fill = ..density..), geom = "tile", contour = FALSE) + 
      facet_wrap(~color) +
      scale_fill_gradient(low = "light blue", high = "dark red")
    

    【讨论】:

    • 你能评论一下为什么你在 stat_bin2d 和 stat_density2d 之间得到如此不同的密度尺度吗?看起来好像 _bin2d 是准确的,但 density2d 太高了,假设一切都应该总和为 1。
    猜你喜欢
    • 2019-03-12
    • 2012-01-06
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-04
    • 2016-08-15
    • 2021-02-15
    相关资源
    最近更新 更多