I used a for loop to automate the generation of 20 barplots with ggplot. I used the following code:
plist<-list()
for (i in 1:20){
TheTable <- answers[i] %>% count(answers[i])
colnames(TheTable) <- c("q", "n")
rightans <- correct_answers[i]
perc_wrong <- prop.table(table(right_wrong[i]))[1]*100
p<-ggplot(TheTable)+
geom_bar(stat="identity", aes(x=q, y=n, fill = q == rightans)) +
scale_colour_manual(name = 'Right', values = setNames(c('green','red'), c(T, F)))+
theme_classic()+
theme(legend.position='none')+
xlab("")+
ylab("")+
labs(title= paste0("Question ", i, " (", rightans, ") "),
subtitle=paste0(round(perc_wrong, 0), "% was wrong"))
print(p)
plist[[i]]<-p
assign(paste0("Q", i, "_plot"), p)
}
do.call(grid.arrange,plist)
My data can be found here (as .RData-file) for reproducibility
The loop and all works fine. If I flip through my plots in the plot area in RStudio immediately after running the code, they all look as they should.
Yet, there is one element that doesn't work: in using do.call(grid.arrange, plist)
it seems as though the fill = q == rightans
statement does not work. In fact, it colors the bars of all 20 plots in function of the last fill = q == rightans
statement it received in the for loop. And this is only for the fill=
argument in the aes()
--all other arguments work flawlessly.
So when I plot them all together (or when I call one single plot later on from the console) it uses the last function it received, which means that each plot will have "C" colored as red (i.e. the wrong answer).
I don't understand what happens here and I have no clue how to solve this issue. I haven't found a working solution on StackOverflow or elsewhere on the web. Any suggestions?