使ggplot2 :: stat_bin2d显示密度而不是计数 [英] make ggplot2:: stat_bin2d show density instead of counts

查看:714
本文介绍了使ggplot2 :: stat_bin2d显示密度而不是计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  library(ggplot2); data(diamonds)

可以使它显示组内密度而不是计数吗? )
ggplot(钻石,aes(克拉,深度))+
stat_bin2d(bins = 40)+ facet_wrap(〜color)

这样可以更容易地比较各组之间的模式,因为有些组可能会更自然地发生。

这个问题有点类似于:



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

  ggplot(钻石,aes(克拉,深度))+ 
stat_density2d(aes(fill = ..density ..),geom =tile,contour = FALSE,n = 25)+
facet_wrap(〜color)+
scale_fill_gradient(低=浅蓝色,高=深红色)



或者使用默认网格:

  ggplot(钻石,aes(克拉,深度))+ 
stat_density2d(aes(fill = ..density ..),geom =tile,contour = FALSE)+
F acet_wrap(〜color)+
scale_fill_gradient(低=浅蓝色,高=深红色)


Is it possible to make it show within-group densities instead of counts?

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

This would make it easier to compare patterns among the groups, as some groups may naturally be more occurring.

The question is slightly similar to: How to scale (normalise) values of ggplot2 stat_bin2d within each column (by X axis) which is also lacking an answer.

解决方案

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

Or would you be happy with a kernel density estimate?

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

Or with default grid:

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

这篇关于使ggplot2 :: stat_bin2d显示密度而不是计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆