This is a simple question, but I'm having difficulty understanding the format required by ggplot2:
I have the following data.table
in R,
print(dt)
ID category A B C totalABC
1: 10 group1 1 3 0 4
2: 11 group1 1 11 1 13
3: 12 group2 15 20 2 37
4: 13 group2 6 12 2 20
5: 14 group2 17 83 6 106
...
My goal is to create a proportional stacked bar graph as in this example: https://rpubs.com/escott8908/RGC_Ch3_Gar_Graphs
where the percentages of X/totalABC, where X is category_type
either A, B, or C. I would also like to perform this by category, e.g. the x-axis values should be group1
, group2
, etc.
As a concrete example, in the case of group1
, there are 4+13=17 total elements.
The percentages would be percent_A = 11.7%, percent_B = 82.3%, percent_C = 5.9%
The correct ggplot2 solution appears to be:
library(ggplot2)
pp = ggplot(dt, aes(x=category, y=percentage, fill=category_type)) +
geom_bar(position="dodge", stat="identity")
My confusion: how would I create a single percentage
column that corresponds to three categorical values?
If the above is incorrect, how would I format my data.table
to create the stacked barplot?