Within the '''library(ggsurvfit)''' package, how do you create a facet plot using the ggsurvfit()
function?
I was under the impression that library(ggsurvfit)
integrated with ggplot2
and so facet should be as normal. Hence I tried the below but with no luck.
library(survival)
library(ggsurvfit)
dat <- structure(list(Rabbit = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L,
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 27L, 26L, 34L, 35L, 28L, 29L, 32L, 36L, 30L, 31L,
33L, 37L, 38L, 39L, 40L, 41L, 42L), Treatment = structure(c(3L,
3L, 4L, 4L, 2L, 2L, 1L, 1L, 5L, 3L, 3L, 4L, 4L, 2L, 2L, 1L, 1L,
5L, 3L, 3L, 4L, 4L, 2L, 2L, 1L, 1L, 5L, 3L, 3L, 4L, 4L, 2L, 2L,
1L, 1L, 5L, 4L, 4L, 5L, 4L, 4L, 5L), levels = c("Meat bait",
"Soil spray", "Carrot bait", "Oat bait", "Control"), class = "factor"),
Survival.Time.Rabbit = c(68.5, 75, 51, 51, 99, 120, 240,
219, 336, 53, 29, 77, 77, 96, 149, 91.5, 77, 336, 336, 336,
77.67, 92.67, 336, 336, 336, 336, 336, 336, 336, 53.5, 73,
336, 336, 336, 336, 336, 336, 336, 336, 336, 336, 336), Status.Rabbit = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Timepoint = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L), levels = c("1 Day post exposure",
"5 Days post exposure", "10 Days post exposure", "20 Days post exposure",
"40 Days post exposure", "60 Days post exposure"), class = "factor"),
Survival.Time.Bait1 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, NA,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, NA, NA, NA, 10L, 10L, NA,
NA, NA, NA, NA, NA, NA, 20L, 20L, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA), Survival.Time.Bait2 = c(NA, NA, NA,
NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 5L,
10L, 10L, NA, NA, 10L, 10L, 10L, 10L, 10L, 20L, 20L, NA,
NA, 20L, 20L, 20L, 20L, 20L, 40L, 40L, 40L, 60L, 60L, 60L
)), row.names = c(NA, -42L), class = "data.frame")
survfit2(Surv(Survival.Time.Rabbit, Status.Rabbit) ~ Timepoint, data = dat) %>%
ggsurvfit(linewidth = 1.5) +
labs(x = "Time to death (hours)",
y = "Survival probability") +
add_confidence_interval() +
add_pvalue("annotation", caption = "Log-rank {p.value}", size = 4) +
theme(axis.title.x = element_text(face = "bold", size = 12),
axis.title.y = element_text(face = "bold", size = 12),
axis.text.x = element_text(size = 10),
axis.text.y = element_text(size = 10)) +
facet_wrap(~Treatment, nrow = 2)
From the above code I was expecting separate survival plots for each treatment that showed Kaplan-Meier curves for each time point. Instead I get the below error message.
Error in `combine_vars()`:
! At least one layer must contain all faceting variables: `Treatment`
✖ Plot is missing `Treatment`
✖ Layer 1 is missing `Treatment`
✖ Layer 2 is missing `Treatment`
✖ Layer 3 is missing `Treatment`
Run `rlang::last_error()` to see where the error occurred.
I have tried to debug this message by following solutions to several similar problems to no avail (Solution1, Solution2, Solution3)