Here is a sample dataset:
#sample data
df <- tibble(year=c(1,1,1,1,2,2,2,2,3,3,3,3),
col=c("a", "b", "c", "d", "a", "b", "c", "d", "a", "b", "c", "d"),
freq=c(2,3,5,1,4,3,8,3,5,7,3,9))
I want to create a bar plot for each year of data independently. I'd also like to print the total sample size for each year as a caption on the respective plot. I'm currently doing it manually like this:
#find total sample size for year 1
df_yr_1 <- df %>%
filter(year==1)
sum(df_yr_1$freq)
#make year 1 histogram
hist <- ggplot(df_yr_1, aes(x=col, y=freq)) +
geom_col() +
labs(caption = "N=11")
hist
So on and so forth for each year.
Is there a way to automate this process? Ideally, all the plots would save to a pdf (1 per page), but if they're saved independently that's fine too. I have a feeling adding the total sample size as a caption might make the process more challenging, so if it's possible to automate the process without the sample size captions that would still be very helpful. Thanks in advance!



