0

I wrote a ggplot as a function of a column (Especies) so I can do all plots at once. But I would like to include the values of the column (i.e. the species name) as the title for each of the generated plots. Anyone knows how?

A sample of the data with two species:

f.spmonth<-structure(list(Ciudad = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("BARCELONA", "MADRID"), class = "factor"), Mes = c(3L, 4L, 5L, 6L, 7L, 8L, 3L, 4L, 5L, 6L, 7L, 8L, 3L, 4L, 5L, 6L, 7L, 8L, 3L, 4L, 5L, 6L, 7L, 8L), Especie = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Anthocharis cardamines", "Aporia crataegi"), class = "factor"), Número = c(10, 1, 10, 20, 5, 0, 1, 3, 12, 22, 12, 0, 4, 45, 23, 12, 20, 30, 20, 11, 10, 20, 5, 0)), row.names = c(NA, 24L), class = "data.frame")

My code:

hist.fenologias<-function(f.spmonth){
 ggplot(data =f.spmonth, aes(x = Mes,y= Número,fill= Ciudad)) +
 geom_area(stat="identity", show.legend = FALSE)+ theme_bw()+
 labs(y = "Número de individuos observadas", x ="Mes") +
 facet_wrap(~ Ciudad, nrow = 2,scale="free_y")} 

all.fenologias<-dlply(f.spmonth , c("Especie") , function(x) plot(hist.fenologias(x) ) ) 

Many thanks in advance!

YMC
  • 63
  • 6
  • 4
    Please include a sample of the data (using `dput`) in your question. Many users will refrain from downloading files. – iod Sep 27 '18 at 15:27
  • 2
    I don't remember if `dlply()` keeps the grouping variable in the dataset as it splits things, but if "Especie" is still in the dataset you could try adding something like `title = unique(f.spmonth[["Especie"]])` in `labs()` in your plotting function to get a title. – aosmith Sep 27 '18 at 15:45
  • @aosmith Amazing, it worked! So simple and I was breaking my head. Thanks a million for the quick answer. – YMC Sep 27 '18 at 15:58
  • Thanks also to @doviod for helping me to include a data sampled avoiding link! – YMC Sep 27 '18 at 15:58

0 Answers0