Plot histograms over factor variables

后端 未结 1 1422
野的像风
野的像风 2021-01-31 10:59

I\'m trying to replicate the graph similar to the following (originally found HERE)
\"stacked

It\'

相关标签:
1条回答
  • 2021-01-31 11:37

    Something like this?

    set.seed(1234)
    df <- data.frame(weekday=rep(sort(unique(weekdays(.leap.seconds))), each=24), 
                           hour=rep(1:24, 7), count=sample(2:600, 24*7, replace=T))
    
    df$weekday <- factor(df$weekday, levels=c("Monday", "Tuesday", "Wednesday", 
                          "Thursday", "Friday", "Saturday", "Sunday"), ordered=T)
    df$hour <- factor(df$hour)
    
    require(ggplot2)    
    p <- ggplot(data = df, aes(x=hour)) 
    p <- p + geom_histogram(aes(weights=count, fill=weekday))
    p <- p + scale_fill_brewer(palette="Set3")
    p <- p + facet_wrap( ~ weekday, ncol=1)
    p
    

    ggplot2_facet_histogram

    0 讨论(0)
提交回复
热议问题