I would have the follwing example
plot_list2 = split(mtcars, mtcars$cyl) %>%
map(function(q) ggplot(q, aes(x= mpg)) +
geom_histogram(aes(y=..density..),
color="darkblue", fill="lightblue")+
geom_density(alpha=.2, fill="#FF6666") +
theme_classic()+
ggtitle(sprintf('Distribution for', names(q))))
My aim is to use a unique code so that in the ggtitle I can attach each names for the list on top generated with split. What should I change here?
Thanks
I will do like this:
because
cylis unique in each element of the list created withsplityou can use the values ofclyto plot the title. Here I usedlabsinstead ofGGtitlebut should be the same. Furthermore you do not need necessarilypurrr::map. Thebase::lapplyhere works the same. Remember to explicit the library you use in the example code (i.e.library(dyplr)andlibrary(purrr)