【问题标题】:Why isn't my function to plot growth over time working?为什么我绘制随时间增长的函数不起作用?
【发布时间】: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 &lt;- (Cest, A1) 我得到这个plot。无论我将哪个物种或情节放入函数中,我似乎都得到了相同的情节。

我也尝试过管道以获得所需的结果:

perc_cover_plot2 <- function (Species, Plot) {
        PlantComp %>% 
        filter(Species == Species & Plot == Plot) %>% 
        plot(Cover ~ Date)
}

我希望创建一个基本的折线图 - 类似于 this。

【问题讨论】:

  • 您没有以正确的方式调用您的函数。你说你这样调用你的函数:perc_cover_plot &lt;- (Cest, A1);你应该这样称呼它:plotOut &lt;- perc_cover_plot(Species = "Cest", Plot = "A1")
  • 我怀疑您可能需要为“封面”添加一些数据?目前,该数据提供了 8 个物种的四个日期。您希望如何打印绘图?一次一个地作为单独的地块,还是可以将地块分面,即在单独地块的网格中?
  • @Peter 对此感到抱歉。现在添加了“封面”。如果地块位于单个地块的网格中,则可以。
  • 谢谢,虽然你有Cover,但只有一个日期,所以情节很枯燥。也许要做的事情是从 4 或 5 个不同日期中提取所有必要变量的前 10 行?

标签: r function dataset


【解决方案1】:

这是在不同环境中使用相同名称的另一个问题。绕过它的最简单方法是在应用过滤器之前在内部重命名变量。 示例:

PlantComp <- read.table(text="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 2
mbs 2020-05-05 A3 Frve 10
mbs 2020-06-05 A3 Frve 15
mbs 2020-07-05 A3 Frve 20
mbs 2020-08-05 A3 Frve 33", header=TRUE, sep=" ") 

PlantComp$Date <- as.Date(PlantComp$Date, '%Y-%d-%m')

perc_cover_plot <- function(Species, Plot) { 
    sp <- Species
    pl <- Plot
    PlantCompSub <- subset(PlantComp, Species == sp & Plot == pl)
    return(plot(Cover ~ Date, data = PlantCompSub))
} 

perc_cover_plot("Cest", "A1")

perc_cover_plot("Frve", "A3")

由reprex package (v0.3.0) 于 2020 年 7 月 8 日创建

【讨论】:

    【解决方案2】:

    你可以试试这个:

    PlantComp$Date <- as.Date(PlantComp$Date,'%Y-%d-%m')
    #Function
    perc_cover_plot <- function(x,Species, Plot) { 
      PlantCompSub <- dplyr::filter(x, Species == Species & Plot == Plot) 
      return(plot(Cover ~ Date, data = PlantCompSub))
    } 
    
    perc_cover_plot(PlantComp,Species = 'Cest',Plot = 'A1')
    perc_cover_plot(PlantComp,Species = 'Frve',Plot = 'A3')
    

    我实际上注意到您得到了相同的图,因为您在Cover 中具有相同的值,当然使用您的演示数据。您的真实数据框必须具有不同的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-11
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 2021-03-21
      • 2021-12-18
      • 2011-06-23
      相关资源
      最近更新 更多