Reputation: 121
I want to plot cumulative mortality across development for individuals kept under different conditions. X1 is an ordered categorical variable representing the different life-stages of my study species (E,1,2,3,4,P) and X2 is a categorical variable representing the different conditions under which individuals were raised (five different conditions a-e). My data is as follows:
X2 X1 Cumulative.%.mortality
a E 19.64285714
b E 28.16901408
c E 26.66666667
d E 49.72972973
e E 78.84615385
a 1 26.78571429
b 1 32.3943662
c 1 29.09090909
d 1 51.89189189
e 1 79.80769231
a 2 26.78571429
b 2 40.84507042
c 2 31.51515152
d 2 52.97297297
e 2 79.80769231
a 3 28.57142857
b 3 53.52112676
c 3 33.93939394
d 3 56.21621622
e 3 79.80769231
a 4 30.35714286
b 4 56.33802817
c 4 41.81818182
d 4 58.37837838
e 4 82.21153846
a P 30.35714286
b P 56.33802817
c P 42.42424242
d P 58.37837838
e P 82.21153846
Each data point represents the % of individuals (added to the different conditions at the beginning of the first life stage E) that died at a particular life-stage or before e.g. the value for 1/c represents the % of individuals that died at life-stage 1 (and E) under condition c.
I would like there to be a line connecting each of the data points and for there to be separate lines for each of the different conditions of X2.
I have tried using the plot() function in R but this just brings up separate box plots for each life-stage.
Upvotes: 0
Views: 41
Reputation: 73572
Using matplot
.
> matrix(dat[order(dat$X2), 3], length(l <- unique(dat$X1))) |>
+ matplot(type='s', xlab='stage', ylab='cum. m.', xaxt='n') +
+ axis(1, axTicks(1), labels=l)
Data:
> dput(dat)
structure(list(X2 = c("a", "b", "c", "d", "e", "a", "b", "c",
"d", "e", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a",
"b", "c", "d", "e", "a", "b", "c", "d", "e"), X1 = c("E", "E",
"E", "E", "E", "1", "1", "1", "1", "1", "2", "2", "2", "2", "2",
"3", "3", "3", "3", "3", "4", "4", "4", "4", "4", "P", "P", "P",
"P", "P"), Cumulative...mortality = c(19.64285714, 28.16901408,
26.66666667, 49.72972973, 78.84615385, 26.78571429, 32.3943662,
29.09090909, 51.89189189, 79.80769231, 26.78571429, 40.84507042,
31.51515152, 52.97297297, 79.80769231, 28.57142857, 53.52112676,
33.93939394, 56.21621622, 79.80769231, 30.35714286, 56.33802817,
41.81818182, 58.37837838, 82.21153846, 30.35714286, 56.33802817,
42.42424242, 58.37837838, 82.21153846)), class = "data.frame", row.names = c(NA,
-30L))
Upvotes: 1
Reputation: 174468
Convert X1
to a factor with the correct levels and draw the result using geom_step
in ggplot
:
library(tidyverse)
df %>%
mutate(X1 = factor(X1, unique(X1))) %>%
ggplot(aes(X1,`Cumulative.%.mortality`, group = X2)) +
geom_step(aes(color = X2), linewidth = 1) +
scale_color_brewer(palette = "Set1") +
theme_minimal(base_size = 20) +
scale_y_continuous("Cumulative mortality (%)", limits = c(0, 100),
expand = c(0, 0)) +
labs(x = "Life stage", color = "Conditions")
Data from question in reproducible format
df <- structure(list(X2 = c("a", "b", "c", "d", "e", "a", "b", "c",
"d", "e", "a", "b", "c", "d", "e", "a", "b", "c", "d", "e", "a",
"b", "c", "d", "e", "a", "b", "c", "d", "e"), X1 = c("E", "E",
"E", "E", "E", "1", "1", "1", "1", "1", "2", "2", "2", "2", "2",
"3", "3", "3", "3", "3", "4", "4", "4", "4", "4", "P", "P", "P",
"P", "P"), `Cumulative.%.mortality` = c(19.64285714, 28.16901408,
26.66666667, 49.72972973, 78.84615385, 26.78571429, 32.3943662,
29.09090909, 51.89189189, 79.80769231, 26.78571429, 40.84507042,
31.51515152, 52.97297297, 79.80769231, 28.57142857, 53.52112676,
33.93939394, 56.21621622, 79.80769231, 30.35714286, 56.33802817,
41.81818182, 58.37837838, 82.21153846, 30.35714286, 56.33802817,
42.42424242, 58.37837838, 82.21153846)), class = "data.frame",
row.names = c(NA, -30L))
Upvotes: 0