I have a dataframe
df <- data.frame(respondent = factor(c(1, 2, 3, 4, 5, 6, 7)),
location = factor(c("US: California", "US: Oregon", "Mexico",
"US: Texas", "Canada", "Mexico", "Canada")))
There are three separate levels related to the US. I don't want to collapse them as the distinction between states is useful for data analysis. I would like to have, however, a basic barplot that stacks the US states on top of one another, so that there are three bars in the barplot--Canada, Mexico, and US--with the last one divided into three states, instead of this:
ggplot(df, aes(location, 1))+
geom_bar(stat = "identity")+
theme(axis.text.x = element_text(angle = 45, hjust = 1),
text = element_text(size=10))
which gives me five bars, with three for the US.
There is an old similar question on Stackoverflow (Grouping/stacking factor levels in ggplot bar chart), but the solution given is pretty convoluted. I'm hoping that there is an easier way to achieve this. Any ideas how it can be done?