Cumulative Mortality Graph in R

905 Views Asked by At

I need to get a graph that has the average cumulative mortality for two populations of oysters at 4 different salinities.

Date = c("5/29/2017","5/31/2017","6/2/2017", "5/29/2017","5/31/2017","6/2/2017","5/29/2017","5/31/2017","6/2/2017","5/29/2017","5/31/2017","6/2/2017","5/29/2017","5/31/2017 ","6/2/2017","5/29/2017 ","5/31/2017","6/2/2017", "5/29/2017","5/31/2017","6/2/2017","5/29/2017", "5/31/2017","6/2/2017")
Mortality =  c(0, 0.4, 0.5, 0.1, 0.3, 0.4, 0.2, 0.3, 0.5, 0.1, 0.2, 0.6, 0, 0.7, 0.8, 0.1, 0.6, 0.9, 0.1, 0.5, 0.9, 0.1, 0.6, 0.8)
Tank = c("1A", "1A", "1A", "1B", "1B", "1B", "5A", "5A", "5A", "5B", "5B", "5B", "1A", "1A", "1A", "1B", "1B", "1B", "5A", "5A", "5A", "5B", "5B", "5B")
Population = c("SL", "SL", "SL", "SL", "SL", "SL", "SL", "SL", "SL", "SL", "SL", "SL", "LC", "LC", "LC", "LC", "LC", "LC", "LC", "LC", "LC", "LC", "LC", "LC") = c (SL SL SL SL SL SL SL SL SL SL SL SL LC LC LC LC LC LC LC LC LC LC LC LC)

I need to average the two values (for example 1A and 1B) and then plot that value for the date. I would like only the dates where cumulative mortality was calculated to show on the x axis.

I have the following graph I made in Excel, but isn't quite what I need.

Salinity Graph

1

There are 1 best solutions below

0
On

Here is my solution using ggplot2 and dplyr

You could use other packages to summarise your data before plotting. Here I made two groups (1 = 1A and 1B, 2 = other tanks).

Just out of curiousity, why do you not use Kapplan-Meier plots for these data?

Date = c("5/29/2017","5/31/2017","6/2/2017", "5/29/2017","5/31/2017","6/2/2017","5/29/2017","5/31/2017","6/2/2017","5/29/2017","5/31/2017","6/2/2017","5/29/2017","5/31/2017 ","6/2/2017","5/29/2017 ","5/31/2017","6/2/2017", "5/29/2017","5/31/2017","6/2/2017","5/29/2017", "5/31/2017","6/2/2017")
Mortality =  c(0, 0.4, 0.5, 0.1, 0.3, 0.4, 0.2, 0.3, 0.5, 0.1, 0.2, 0.6, 0, 0.7, 0.8, 0.1, 0.6, 0.9, 0.1, 0.5, 0.9, 0.1, 0.6, 0.8)
Tank = c("1A", "1A", "1A", "1B", "1B", "1B", "5A", "5A", "5A", "5B", "5B", "5B", "1A", "1A", "1A", "1B", "1B", "1B", "5A", "5A", "5A", "5B", "5B", "5B")
Population = c("SL", "SL", "SL", "SL", "SL", "SL", "SL", "SL", "SL", "SL", "SL", "SL", "LC", "LC", "LC", "LC", "LC", "LC", "LC", "LC", "LC", "LC", "LC", "LC")


df <- data.frame(Date, Mortality, Tank, Population)

library(dplyr)
library(ggplot2)

dfs <- df %>% mutate(TankGroup =  if_else(Tank %in% c('1A','1B'),1,2)) %>% 
  group_by(Date, TankGroup) %>% 
  summarise(Mortality = mean(Mortality))

ggplot(dfs, aes(x = Date, y = Mortality, group = factor(TankGroup), color = factor(TankGroup))) +
  geom_point() + geom_line() + theme_bw()