K. Lesciotto
K. Lesciotto

Reputation: 23

Adding titles to ggplots created by lapply

How can I add titles based off the a list of variables? The code below works to produce the 10 plots, but there are no titles. I would like the titles to reflect the variables listed inthe dfList.

dfList<-list(s$Basioccipital,s$Basisphenoid,s$Interparietal,s$L_Frontal,s$L_LateralOccipital,s$L_Nasal,s$L_Parietal,s$L_SquamousTemporal,s$Presphenoid,s$SquamousOccipital)


lapply(dfList, function (x){
  ggplot(data=x,aes(x=Genotype2, y=Volume))+
  geom_boxplot(aes(fill=factor(Genotype2))) + ggtitle(dfList[i])
})

Upvotes: 0

Views: 477

Answers (2)

Dan
Dan

Reputation: 12084

I can't test this as you don't include any data, but here's a potential solution...

dfList<-list("Basioccipital", "Basisphenoid", "Interparietal",
             "L_Frontal", "L_LateralOccipital", "L_Nasal", "L_Parietal", 
             "L_SquamousTemporal", "Presphenoid", "SquamousOccipital")


lapply(dfList, function (x){
  ggplot(data=s[[x]],aes(x=Genotype2, y=Volume))+
    geom_boxplot(aes(fill=factor(Genotype2))) + ggtitle(x)
})

Upvotes: 2

Shirin Yavari
Shirin Yavari

Reputation: 692

you can use the following code in place of your current ggtitle:

 ggtitle(names(dfList[i]))

Upvotes: 0

Related Questions