使用ggplot和dlply进行绘图时,按行名称设置绘图标题

时间:2018-09-27 15:25:07

标签: r function ggplot2 ggtitle

我写了一个ggplot作为列(Especies)的函数,所以我可以一次绘制所有图。但是我想将列的值(即物种名称)作为每个生成图的标题。有人知道吗?

具有两种物质的数据样本:

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

我的代码:

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

非常感谢!

0 个答案:

没有答案