Adding Lists Names as Plot Titles in Lapply Call in R

Adding lists names as plot titles in lapply call in R

Use lapply on the names of the list items instead:

lapply(names(afn), function(x) plot(afn[[x]], main=x))

Sample Image

To see why you aren't getting your names, run lapply(afn, function(x) names(x)) gives you the names of each list item or something else. Try the same with lapply(names(afn), function(x) x) and compare the difference. Then, remember that we can extract a single list item by name by using [[.

Use lapply to plot data in a list and use names of list elements as plot titles

One possibility would be to loop over the names of the list:

lapply(names(comp.surv), function(x) plot(comp.surv[[x]], main = x))

Or slightly more verbose, loop over the list indices:

lapply(seq_along(comp.surv), function(x) plot(comp.surv[[x]], main = names(comp.surv)[x]))

Add labels and title to a plot derived from an lapply function

I've just added title() function;

df.split <- split(df, df$Commonname, drop = TRUE)
par(mfrow=c(2,2))
lapply(seq_along(df.split), function(x) ccf(df.split[[x]]$CPUE,df.split[[x]]$Discharge, lag.max = 5,
ylab = "", xlab = "", main= names(df.split)[x]))
title("This is a main title", line = -1, outer = TRUE,xlab = 'This is x axis',ylab = 'This is y axis')

Sample Image

How to use list elements as plot title in r?

You could use mapply to loop over both d and names(d) to pass the name of the list element to your function:

d<- list(d1 = list(`1979` = 3.8234619080332, `1980` = 3.94835997755299, 
`1981` = 4.40780893307071), d2 = list(`1979` = 3.78682062013644,
`1980` = 3.89720895853959, `1981` = 4.35137469930167))

fun1<-function(x, y) {
plot(ecdf(unlist(x)), main=y)
}

mapply(fun1, d, names(d))

Plot titles using lapply

I would try:

stacked <- lapply(names(meltmean), 
function (x) ggplot(meltmean[[x]], aes(x=month, y= value, fill=Type))+geom_area()+ggtitle(x))

Change title of plots in list

This works as intended:

mynames <- sapply(names(tbls), function(x) {
paste("How do they rank? -",gsub("\\.",": ",x))
})

myfilenames <- names(tbls)

plot_likert <- function(x, myname, myfilename){
p <- plot(likert(x),
type ="bar",center=3,
group.order=names(x))+
labs(x = "Theme", subtitle=paste("Number of observations:",nrow(x)))+
guides(fill=guide_legend("Rank"))+
ggtitle(myname)
p
}

list_plots <- lapply(1:length(tbls),function(i) {
plot_likert(tbls[[i]], mynames[i], myfilenames[i])
})

Sample Image

When in doubt, keep things stupid and simple. Non-standard evaluation like deparse(substitute( will throw you right into Burns' R inferno.

outputting element names of list of lists within lapply

Similar to thelatemail anwser:

xx=unlist(list_of_lists, recursive=FALSE)
mapply(plot, x = xx, main = names(xx), SIMPLIFY = FALSE)

Adding titles to ggplots created by lapply

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)
})


Related Topics



Leave a reply



Submit