Plotting many bar charts by year with ggplot

Viewed 33

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!

2 Answers

We could wrap the code in a loop

pdf( "plots.pdf", onefile = TRUE)
for(i in unique(df$year)) {
   df_yr_i <- df %>%
       filter(year==i) 
      


hist <- ggplot(df_yr_i, aes(x=col, y=freq)) +
  geom_col() +
  labs(caption = paste0("N=", sum(df_yr_i$freq)))
  print(hist)
  

}

dev.off()

-output

enter image description here

A simple loop should do the trick:

pdf("my.pdf")
for(i in 1:3) {
  plot_df <- df %>% filter(year ==1)
  
  p <- ggplot(plot_df, aes(col, freq)) +
    geom_col() +
    labs(title = paste("Year", i), caption = paste0("N=", sum(plot_df$freq)))
  
  print(p)
}
dev.off()

Resulting in

my.pdf


Page 1

enter image description here


Page 2 enter image description here


Page 3 enter image description here

Related