使用 diff() 函数创建条形图以确定从一个事件到下一个事件的生存变化

Create bar plot using diff() function to determine the change in survival from one event the next

我有一个生存数据集 df_survival,其中包含四列 DateTankFeed_GroupCensus

这是创建数据集的代码:

Date <- c("2020-10-15", "2020-10-15", "2020-10-15", "2020-10-15", "2020-10-15", "2020-10-15","2020-10-15", "2020-10-15","2020-12-05","2020-12-05","2020-12-05","2020-12-05","2020-12-05","2020-12-05","2020-12-05","2020-12-05","2021-01-29","2021-01-29","2021-01-29","2021-01-29","2021-01-29","2021-01-29","2021-01-29","2021-03-13","2021-03-13","2021-03-13","2021-03-13","2021-03-13","2021-03-13","2021-03-13","2021-03-13","2021-04-30","2021-04-30","2021-04-30","2021-04-30","2021-04-30","2021-04-30","2021-04-30","2021-04-30","2021-04-30", "2021-07-06", "2021-07-06", "2021-07-06", "2021-07-06", "2021-07-06", "2021-07-06", "2021-07-06", "2021-07-06")
Tank <- c(1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8)
Feed_Group <- c("Gemma","Gemma","Gemma","Gemma","Mysis","Mysis","Mysis","Mysis","Gemma","Gemma","Gemma","Gemma","Mysis","Mysis","Mysis","Mysis","Gemma","Gemma","Gemma","Gemma","Mysis","Mysis","Mysis","Mysis","Gemma","Gemma","Gemma","Gemma","Mysis","Mysis","Mysis","Mysis","Gemma","Gemma","Gemma","Gemma","Mysis","Mysis","Mysis","Mysis","Gemma","Gemma","Gemma","Gemma","Mysis","Mysis","Mysis","Mysis") 
Census <- c(20,20,20,20,20,20,20,20,15,17,14,10,14,20,16,16,13,16,12,10,14,20,16,16,13,15,11,10,14,20,16,16,13,15,10,9,14,20,15,16,13,14,10,8,14,19,15,15)


df_survival <- data.frame(Date,Tank,Feed_Group,Census)

我创建了这个可视化,它显示了每个组随时间推移的平均人口普查和标准误差。

现在我想创建一个条形图,显示从一个事件到下一个事件有多少动物死亡。因此,从事件 1 到事件 2,Gemma 组的动物减少了 6 只,从事件 2 到事件 3,减少了 1.25 只动物。

我可以像下面那样使用 diff() 函数来创建一个 tibble,为我提供与我想要的完全相同的 Feed_Group 和 Census_Dead 列。

df_survival %>%
  group_by(Feed_Group,Date) %>%
  summarise(Census = mean(Census)) %>%
  summarise(Census_Dead = diff(Census))

但是,我丢失日期列很可能是因为第一个点的差值不是 0,这是有道理的。不幸的是,这让制图变得如此困难。

我知道我可以创建 data.frame 并手动将这些数字与相应的日期放在一起,因为数据集很小,但我很好奇是否可以操纵当前数据以获得类似下列的:

dplyr::summarize 可用于为每个组获取更少的行(不仅仅是聚合到一行)。或者你可以使用 dplyr::group_modify.

df_survival |>
  group_by(Date, Feed_Group) |>
  summarize(Census = mean(Census)) |>
  ungroup() |>
  arrange(Date) |>
  group_by(Feed_Group) |>
  summarize(Census_Dead = -diff(Census),
            Date = Date[-1]) |>
  ggplot(aes(y = Census_Dead, x= Date)) +
  geom_bar(aes(fill = Feed_Group), position = "dodge", stat = "identity")