Pet Cats UK

Welcome to TidyTuesday 2023 week 5

Published

January 31, 2023

library(tidyverse)
tuesdata <- tidytuesdayR::tt_load(2023, week = 05)
cats_uk <- tuesdata$cats_uk
cats_uk_reference <- tuesdata$cats_uk_reference
df <- cats_uk%>%
  inner_join(cats_uk_reference,by="tag_id")
df%>%
  slice()%>%
  glimpse()
library(survival)
library("survminer")

surv_df <- 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)
start_date <- as.Date("2017-06-03")
end_date <- as.Date("2017-11-30")
date_seq <- seq(start_date, end_date, by = "day")

cat_hz <- surv_df %>%
  group_by(day)%>%
  mutate(time=as.numeric(day - start_date) + 1)%>%
  arrange(time)


fit <- survfit(Surv(time, hunt) ~ animal_sex, data = cat_hz)
ggsurv <- ggsurvplot(fit,
                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") 

ggsurv$plot %+%
  ggthemes::scale_colour_fivethirtyeight(labels=c("Male","Female")) %+%
  labs(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)