Reputation: 33
I am hoping to make a stacked bar plot to show two factors. The questions and answers I can find on this site that address this problem all work with data that appears to be in a matrix format and use ggplot2. My data is in lists of observations, like this:
mydata = data.frame(V1=c("A","B","B","C","C"), V2=c("X","X","Y","Z","Z"))
I would like to show categories of V1 on the x axis of my plot, but stacked to show the proportions of V2 in each bar.
I can use the "count" function in the plyr library to find the frequency of each observation,
library(plyr)
mydata.count = count(mydata)
but I don't know how to structure my barplot command to group data by the level of V1: barplot(mydata.count$freq)
separates all combinations of V1 and V2 into separate bars.
If possible, I would like to create this plot using the base R barplot functions so that it is visually consistent with other plots in my study.
Upvotes: 1
Views: 1509
Reputation: 32558
Use table
to create a contingency table of the counts for all combinations of V1
and V2
. Then, use barplot
to create the stacked bar plot. When used with matrix, barplot
creates stacked vertical bars where the columns appear on x-axis - so it may be necessary to transpose the data.
m = t(table(mydata))
colors = c("red", "green", "blue")
graphics.off()
windows(8, 6)
barplot(m, col = colors)
legend("topleft", legend = row.names(m), fill = colors)
Upvotes: 1
Reputation: 4314
Here is another possibility with ggplot
:
ggplot(as.data.frame(table(mydata)), aes(x=V1, y=Freq, fill=V2)) + geom_bar(stat="identity")
ggplot(as.data.frame(table(mydata)), aes(x=V2, y=Freq, fill=V1)) + geom_bar(stat="identity")
Upvotes: 0