Art*_*Art 3 r histogram ggplot2 color-palette
我有一个简单的问题.如何ggplot2使用固定binwidth和填充彩虹色(或任何其他调色板)绘制直方图?
可以说我有这样的数据:
myData <- abs(rnorm(1000))
我想绘制直方图,使用例如binwidth=.1.然而,这将导致不同数量的箱子,具体取决于数据:
ggplot() + geom_histogram(aes(x = myData), binwidth=.1) 
如果我知道箱子的数量(例如n=15),我会使用类似的东西:
ggplot() + geom_histogram(aes(x = myData), binwidth=.1, fill=rainbow(n))
但随着垃圾箱数量的变化,我会陷入这个简单的问题.
如果你真的想要灵活的数量,这是我的小解决方法:
library(ggplot2)
gg_b <- ggplot_build(
  ggplot() + geom_histogram(aes(x = myData), binwidth=.1)
)
nu_bins <- dim(gg_b$data[[1]])[1]
ggplot() + geom_histogram(aes(x = myData), binwidth=.1, fill = rainbow(nu_bins))