R stacked barchart with aggregate data

后端 未结 2 1250
我寻月下人不归
我寻月下人不归 2021-01-02 16:56

I\'m having troubles creating a stacked barchart with aggregate data. When dealing with aggregate tables from other people\'s reports I generally use Excel, but I\'d like to

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-02 17:26

    reshape your data:

    library(reshape2)
    df <- melt(D)
    

    And simply plot it :)

    ggplot(df, aes(x = factor(Education), y = value, fill = factor(variable))) +
    geom_bar() + facet_grid(.~Group) +
    ylab('') + xlab('') + opts(title = '') + scale_fill_discrete('') +
    theme_bw() +
    opts(axis.text.x=theme_text(angle = 45, hjust = 1, vjust = 1))
    

    Where the first line creates sets aesthetics, second line adds bar layer and the facet, on the 3rd line we remove unwanted texts from the plot, the 4th line sets the b&w theme and on the last line we rotate the x asis labels.

    enter image description here

提交回复
热议问题