I have a question that it's been asked several times but even though I tried multiple solutions suggested it still does not work in my case.
I have this plotting function:
plot_likert_subsample <- function(x, section, subsample = NULL, separate=NULL){
if(!is.null(separate)){
sec<-deparse(substitute(section))
sub<-deparse(substitute(subsample))
filenames<-paste("Likert_ranking",sec,sub,sep="_",".png")
count<-data.frame(table(Rank_themes_sections[,c(subsample)]))
count<-transform(count,sample_size=paste0(Var1," - ",Freq))
subtitle<-as.character(count$sample_size)
a<-split(x, x[,subsample])
a<-lapply(a, function (x) likert(x[section]))
plotlist<-lapply(seq_along(a), function(i) {
plot(a[[i]],center=3.5) +
ggtitle(paste("Figure: Ranking of",sec," themes - ",names(a)[i],"respondents"))+
labs(x="Theme",subtitle =paste("Sample sizes:", subtitle,collapse = " - "))})
a<-grid.arrange(grobs = plotlist)
print(a)
}
else if(is.null(separate)) {
sec<-deparse(substitute(section))
sub<-deparse(substitute(subsample))
filenames<-paste("Likert_ranking",sec,sub,sep="_")
count<-data.frame(table(Rank_themes_sections[,c(subsample)]))
count<-transform(count,sample_size=paste0(Var1," - ",Freq))
subtitle<-as.character(count$sample_size)
likerts<-likert(x[section],grouping=x[,subsample])
p<-plot(likerts, center=3.5)+
ggtitle(paste("Figure: Ranking of",sec," themes by",sub))+
labs(x="Theme",subtitle =paste("Sample size:", subtitle,collapse = " - "))
print(p)
}
}
I define sec
at the beginning, so that the title of the plot changes according to the section of my data plotted. Everything works fine if I try to plot a single graph, the title and everything else is correct.
E.g. if sec<-deparse(substitute(Social))
the title of the plot is Figure: Ranking of social themes.
However, if I try and lapply
my function to the list of sections like this:
list_sections<-list(Social,Private,Love,Home,Money)
list_plots<-lapply(list_sections,function (y) plot_likert_subsample(Rank_themes_sections,section=y,subsample="dummy1"))
All of my titles become Figure: Ranking of y themes.
From what I understood, this is due to something called lazy evaluation, but I have no idea on how to overcome this! Please help!
Thanks in advance