首页 > 解决方案 > 使用 ggplot 和 dlply 绘图时根据行名设置绘图标题

问题描述

我写了一个 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) ) ) 

提前谢谢了!

标签: rfunctionggplot2ggtitle

解决方案


推荐阅读