make ggplot2 :: stat_bin2d显示密度而不是计数

时间:2016-06-16 07:11:00

标签: r ggplot2

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

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)略有相似,但也缺乏答案。

1 个答案:

答案 0 :(得分:9)

ggplot(diamonds, aes(carat, depth)) +  
  stat_bin2d(bins=40, aes(fill = ..density..))+ facet_wrap(~color)

resulting plot

或者您对内核密度估算感到满意吗?

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")

resulting plot

或使用默认网格:

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")

resulting plot