How to fill histogram with color gradient?

前端 未结 2 823
不思量自难忘°
不思量自难忘° 2020-12-11 07:48

I have a simple problem. How to plot histogram with ggplot2 with fixed binwidth and filled with rainbow colors (or any other palette)?

Let

2条回答
  •  心在旅途
    2020-12-11 08:49

    If you really want the number of bins flexible, here is my little workaround:

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

提交回复
热议问题