I have a dataframe that has three factor columns. One column is a 'SurveyDate' column, and the others are attributes about the survey takers; say one is 'Gender' and one is 'HighSchoolGraduate'
I want to create a plot that has date as the x-axis and uses side-by-side bar plots for the number of male and female respondents, and within each of those two bars, stack high school graduate vs. non-high-school-graduate.
testDates <- sample(seq(as.Date('2019/1/1'), as.Date('2019/2/1'), by="day"), 100, replace = TRUE)
gender <- sample(c("F", "M"), 100, replace = TRUE)
graduate <- sample(c("Y", "N"), 100, replace = TRUE)
testdf <- data.frame(testDates, gender, graduate)
I can create a table of frequencies of dates vs. gender and use that to create the side by side plot:
tbl <- with(testdf, table(testDates, gender))
ggplot(as.data.frame(tbl), aes(x=testDates, y=Freq, fill=gender)) +
+ geom_col(position='dodge
So now... how do I divide each of those bars by graduate? (And yes, I should have created more samples for this demo, but the idea still works.)