I am trying to create a barplot that shows the average hourly wages of union and nonunion workers grouped by single or married grouped by college grad or not college grad. While I've managed to construct a passable barplot with two factor groupings, I cannot figure out how to do so with three factor groupings. The examples I have seen that have three factors look just at frequency counts, so I'm not sure how to incorporate the mean of another variable across all the factors into the plot. What I am looking to create is something that looks like this (created in Stata): Average Hourly Wage by Union Status, Marital Status, and College Graduation My code looks like this:
levelbar = tapply(wage, list(as.factor(union), as.factor(married),
as.factor(collgrad)), mean)
par(mfrow = c(1, 2))
barplot(levelbar, beside = TRUE)
barplot(t(levelbar), beside = TRUE)
When I run this, however, I receive the error:
Error in barplot.default(levelbar, beside = TRUE) :
'height' must be a vector or a matrix
Any help on this would be appreciated. I'm sure ggplot might be useful here, but I do not have a great deal of experience using that package.