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

R 使ggplot2::stat_bin2d显示密度而不是计数,r,ggplot2,R,Ggplot2,是否可以使其显示组内密度而不是计数 library(ggplot2);data(diamonds) ggplot(diamonds, aes(carat, depth)) + stat_bin2d(bins=40)+ facet_wrap(~color) 这将使比较各组之间的模式更容易,因为有些组可能自然更容易发生 这个问题有点类似于:这也缺少答案 ggplot(diamonds, aes(carat, depth)) + stat_bin2d(bins=40, aes(fil

是否可以使其显示组内密度而不是计数

library(ggplot2);data(diamonds)
ggplot(diamonds, aes(carat, depth)) +  
  stat_bin2d(bins=40)+ facet_wrap(~color)
这将使比较各组之间的模式更容易,因为有些组可能自然更容易发生

这个问题有点类似于:这也缺少答案

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。