This is not a duplicate question, but it is based on: Stacked bar chart
I am trying to use the accepted answer by agstudy. I have the following dataframe:
types c1 c2 c3 c4 c5 c6
A 20 2 6 1 16 1
B 15 1 7 1 7 1
C 7 5 3 0 8 3
D 5 7 4 7 6 4
F 6 6 6 2 5 6
E 17 8 2 3 4 9
tbl<-melt(tbl,id.vars="types")
ggplot(tbl,aes(x=types,y=value,fill=variable))+geom_bar(stat='identity')
This is a simple way to create a stacked bar chart. I originally had an issue about stacking cause of the melt()
.
My issue comes with stacking the data to get all the values on top of each in one column per row. It would not stack because I had the wrong names and I did not understand the output of melt. But now that I do understand the melt function, it splits up the data and groups it over the id.vars.
by doing so you can create a graph, such that the fill aspect of the graph will be the variable where it fills in the bar with the values in the value
column from melt
. This is interpreted by R as a stacked bar chart.