colour bins of histogram in R

2019-08-22 12:05发布

问题:

I want to plot a barplot like in figure. I want each bin to be coloured based on the sum of the value in the other columns. . I made a reproducible example here.

library(reshape)
library(ggplot2)

values= replicate(4, diff(c(0, sort(runif(92)), 1)))
 colnames(values) = c("A","B","C","D")
 counts = sample(10:100, 93, replace=T)
 df = data.frame(cbind(values,"count"=counts))
 mdf = melt(df,id="count")

 mdf = mdf %>%
  mutate(binCounts = cut(count, breaks = seq(0, 100, by = 5)))



  plot = ggplot(mdf) +
  geom_bar(aes(x=binCounts, fill=variable)) +
  theme(axis.text.x=element_text(angle = 90, hjust=1))

print(plot)

I want the count on the y axis. For each bar I want to plot the proportion of the data from the column A B C and D. However with the above code it tends to plot the count of the variable rather than the sum.

回答1:

mdf %>%
  ungroup() %>% 
  mutate(binCounts = cut(count, breaks = seq(0, 100, by = 5))) %>% 
  group_by(binCounts,variable) %>% 
  summarise(count = sum(count)) %>% 
  ggplot(aes(x=binCounts,y = count, fill=variable)) +
  geom_col() +
  theme(axis.text.x=element_text(angle = 90, hjust=1))