library(tidyverse)
<- tidytuesdayR::tt_load(2023, week = 05) tuesdata
<- tuesdata$cats_uk
cats_uk <- tuesdata$cats_uk_reference cats_uk_reference
<- cats_uk%>%
df inner_join(cats_uk_reference,by="tag_id")
%>%
dfslice()%>%
glimpse()
library(survival)
library("survminer")
<- df%>%
surv_df select(timestamp,hunt,animal_sex)%>%#DataExplorer::profile_missing()
filter(!is.na(hunt))%>%#count(hunt)
mutate(animal_sex=ifelse(animal_sex=="m",1,2),
hunt=ifelse(hunt=="FALSE",0,1),
timestamp=as.POSIXct(timestamp, format = "%Y-%m-%d %H:%M:%S"),
day=as.Date(timestamp,"%Y-%m-%d %H:%M:%S",tz="GMT"))
range(surv_df$day)
<- as.Date("2017-06-03")
start_date <- as.Date("2017-11-30")
end_date <- seq(start_date, end_date, by = "day")
date_seq
<- surv_df %>%
cat_hz group_by(day)%>%
mutate(time=as.numeric(day - start_date) + 1)%>%
arrange(time)
<- survfit(Surv(time, hunt) ~ animal_sex, data = cat_hz) fit
<- ggsurvplot(fit,
ggsurv data = cat_hz,
censor.shape = "|",
censor.size = 4,
risk.table = TRUE,
submain = "Hazards Distribution and Sex Differences in Hunting Risk",
caption = "Based on Kaplan-Meier estimates\nDataSource: #TidyTuesday 2023 Week5 Pet Cats UK\nDataViz: Federica Gazzelloni #30DayChartChallenge 2023 Day7 - hazards\n")
$plot %+%
ggsurv::scale_colour_fivethirtyeight(labels=c("Male","Female")) %+%
ggthemeslabs(title="Survival of UK Cats") %+%
theme_survminer(base_family = "Roboto Condensed",
font.main = c(18, "bold"),
font.submain = c(14, "bold.italic"),
font.caption = c(11, "plain"),
font.x = c(12, "bold.italic"),
font.y = c(12, "bold.italic"),
font.tickslab = c(12, "plain")) %+%
theme(plot.background = element_rect(fill="grey90",color="grey90"),
panel.background = element_rect(fill="grey90",color="grey90"),
legend.background = element_blank())
showtext.auto(enable = FALSE)
ggsave("w5_PCUK.png",
width = 7,height = 5)