【发布时间】:2020-10-29 07:58:46
【问题描述】:
我想创建一个函数来绘制随时间变化的地块内植物物种的生长情况(或数据集中描述的覆盖率)。换句话说,我想随着时间的推移在一个地块中为一个物种绘制覆盖日期。
以下是数据集的示例:
Site Date Plot Species Cover
mbs 2020-04-05 A1 Cest 2
mbs 2020-05-05 A1 Cest 10
mbs 2020-06-05 A1 Cest 15
mbs 2020-07-05 A1 Cest 20
mbs 2020-08-05 A1 Cest 16
mbs 2020-04-05 A3 Frve 1
mbs 2020-05-05 A3 Frve 5
mbs 2020-06-05 A3 Frve 10
mbs 2020-07-05 A3 Frve 12
mbs 2020-08-05 A3 Frve 10
这也是我尝试让我的实际数据集可重现的尝试:
dput(PlantComp[3000:3040, c(1, 2, 4, 5, 6)])
structure(list(Site = c("umbs", "umbs", "umbs", "umbs", "umbs",
"umbs", "umbs", "umbs", "umbs", "umbs", "umbs", "umbs", "umbs",
"umbs", "umbs", "umbs", "umbs", "umbs", "umbs", "umbs", "umbs",
"umbs", "umbs", "umbs", "umbs", "umbs", "umbs", "umbs", "umbs",
"umbs", "umbs", "umbs", "umbs", "umbs", "umbs", "umbs", "umbs",
"umbs", "umbs", "umbs", "umbs"), Date = structure(c(18417, 18417,
18417, 18417, 18417, 18417, 18417, 18417, 18417, 18417, 18417,
18417, 18417, 18417, 18417, 18417, 18417, 18417, 18417, 18417,
18417, 18417, 18417, 18417, 18417, 18417, 18417, 18417, 18417,
18417, 18417, 18417, 18417, 18417, 18417, 18417, 18417, 18417,
18417, 18417, 18417), class = "Date"), Plot = c("B4", "B4", "B4",
"B4", "B4", "B4", "B4", "B4", "B4", "B5", "B5", "B5", "B5", "B5",
"B5", "B5", "B6", "B6", "B6", "B6", "B6", "B6", "B6", "B6", "B6",
"C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1", "C1",
"C2", "C2", "C2", "C2", "C2"), Species = c("Cest", "Poco", "Popr",
"Ruac", "Litter", "Umsp", "Ulsp", "Vert_Litter", "Ptaq", "Cest",
"Poco", "Popr", "Litter", "Bare_Ground", "Umsp", "Vert_Litter",
"Cape", "Cest", "Dasp", "Hype", "Poco", "Litter", "Vert_Litter",
"Ulsp", "Ptaq", "Cest", "Dasp", "Hype", "Popr", "Ruac", "Litter",
"Umsp", "Vert_Litter", "Ulsp", "Bare_Ground", "Vear", "Cape",
"Cest", "Dasp", "Hype", "Popr"), Cover = c(6L, 1L, 3L, 3L, 78L,
5L, 1L, 1L, 1L, 16L, 3L, 7L, 76L, 2L, 7L, 1L, 1L, 13L, 8L, 1L,
4L, 79L, 1L, 3L, 1L, 15L, 1L, 1L, 5L, 6L, 68L, 3L, 2L, 3L, 3L,
1L, 1L, 19L, 2L, 1L, 3L)), row.names = 3000:3040, class = "data.frame")
我对日期列也这样做了:
PlantComp$Date <- as.Date(PlantComp$Date,
format = "%m/%d/%y")
这是我尝试在 R 中编写的用于创建所需函数的代码:
perc_cover_plot <- function(Species, Plot) {
PlantCompSub <- filter(PlantComp, Species == Species & Plot == Plot)
return(plot(Cover ~ Date, data = PlantCompSub))
}
当我调用函数perc_cover_plot <- (Cest, A1)
我得到这个plot。无论我将哪个物种或情节放入函数中,我似乎都得到了相同的情节。
我也尝试过管道以获得所需的结果:
perc_cover_plot2 <- function (Species, Plot) {
PlantComp %>%
filter(Species == Species & Plot == Plot) %>%
plot(Cover ~ Date)
}
我希望创建一个基本的折线图 - 类似于 this。
【问题讨论】:
-
您没有以正确的方式调用您的函数。你说你这样调用你的函数:
perc_cover_plot <- (Cest, A1);你应该这样称呼它:plotOut <- perc_cover_plot(Species = "Cest", Plot = "A1") -
我怀疑您可能需要为“封面”添加一些数据?目前,该数据提供了 8 个物种的四个日期。您希望如何打印绘图?一次一个地作为单独的地块,还是可以将地块分面,即在单独地块的网格中?
-
@Peter 对此感到抱歉。现在添加了“封面”。如果地块位于单个地块的网格中,则可以。
-
谢谢,虽然你有
Cover,但只有一个日期,所以情节很枯燥。也许要做的事情是从 4 或 5 个不同日期中提取所有必要变量的前 10 行?