R中的累积死亡率图

时间:2017-07-20 07:16:57

标签: r graph cumulative-line-chart

我需要得到一个图表,其中包含4种不同盐度的两个牡蛎种群的平均累积死亡率。

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)

我需要平均两个值(例如1A和1B),然后绘制日期的值。我想只计算累积死亡率计算的日期在x轴上显示。

我在Excel中制作了以下图表,但不是我需要的。

Salinity Graph

1 个答案:

答案 0 :(得分:1)

以下是使用ggplot2dplyr

的解决方案

您可以在绘图之前使用其他包来汇总数据。在这里,我做了两组(1 = 1A和1B,2 =其他坦克)。

出于好奇,为什么不使用Kapplan-Meier图来获取这些数据呢?

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()