Advertisement
celestialgod

colorful histogram

Jan 23rd, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.55 KB | None | 0 0
  1. library(data.table)
  2. library(ggplot2)
  3.  
  4. # data generation
  5. colors <- rainbow(20)
  6. DT <- data.table(x = runif(1e4), grp = sample(LETTERS[1:10], 1e4, TRUE))
  7. breaks <- seq(0, 1, by = 0.05)
  8. DT[ , color := colors[findInterval(x, breaks)]]
  9. ggplot(DT, aes(x)) +
  10.   stat_bin(aes(fill = color), breaks = breaks, show.legend = FALSE) +
  11.   facet_wrap(~grp, 5, 2)
  12.  
  13. # percentage
  14. ggplot(DT, aes(x)) +
  15.   stat_bin(aes(fill = color, y = (..count..)/sum(..count..)),
  16.            breaks = breaks, show.legend = FALSE) +
  17.   facet_wrap(~grp, 5, 2) + labs(y = "Percentage")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement