I am new to R so sorry if the question is dumb, but I have looked around and can't find an answer:
I have a panel dataset with loan data for 25 banks over a period of 24 months. Here I make a simpler version with 3 banks and 3 periods:
bank<-c("bank1", "bank1", "bank1", "bank2", "bank2", "bank2", "bank3", "bank3", "bank3")
date<-c("jan-2016", "feb-2016", "Mar-2016", "jan-2016", "feb-2016","Mar-2016", "jan-2016", "feb-2016", "Mar-2016")
tot_loans<-c(200000, 100000, 200000, 155000, 233000, 435000, 99000, 111000, 129000)
df<-data.frame(bank, date, tot_loans)
I'd like to create a loop that saves ggplot objects for each bank. My goal is to use these objects later in a markdown file. I try this:
bank_list <- unique(df$bank)
for (i in seq_along(bank_list)) {
paste(i, "total_loans", sep="_") <- df %>%
group_by(date) %>%
filter(bank==[[i]]) %>%
ggplot(aes(x=date, y=loan_size)) +
geom_line() +
ggtitle(paste([[i]], "value of loans", sep=" "))
}
But there are multiple errors here. What I would like to get is a series of ggplot objects called "bank1_total_loans", "bank2_total_loans" etc, and each of them has the bank name as ggplot title. Is there a way to do this?