0

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

maplesyrup123
  • 303
  • 1
  • 6
  • 2
    You can't use the `deparse(substitite())` trick with `lapply` because `lapply` changes the names of variables before passing them to your function. Ultimately it's only good for passing along values, not variable names. If you need to set a title, you should explicitly set that as a separate parameter that you can change manually as needed. See this question for what that might look like: https://stackoverflow.com/questions/24309910/how-to-get-name-of-variable-in-r-substitute – MrFlick Aug 20 '22 at 17:26
  • Thanks for the comment. I thought of doing that but I have many plots do make and it would mean specifying each time a variable. Is there a way to automate the title without having to specify a variable? – maplesyrup123 Aug 20 '22 at 17:31
  • 1
    Somewhere a variable will need to exist. You either create it yourself or write some code to create it for you. I'm not sure exactly what you are trying to avoid. If you used a named list rather than just a list you'd already have an easy place to store the title info. And you could use `Map` or `mapply` rather than `lapply` to iterate over multiple values. – MrFlick Aug 20 '22 at 17:34
  • Thanks a lot for the info. I'm quite new to R and only recently learned functions, I'll try anyway with your suggestions, hopefully I get to make it work using `Map`... – maplesyrup123 Aug 20 '22 at 17:40
  • what about something like this `lapply(seq_along(list_sections), function(ii) {y <- list_sections[[ii]]; n <- names(list_sections)[ii]; plot_likert_subsample(Rank_themes_sections,section=y,subsample="dummy1") + ggtitle(n)})` – rawr Aug 20 '22 at 18:02

0 Answers0