-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
w5_PCUK.qmd
94 lines (74 loc) · 2.32 KB
/
w5_PCUK.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
---
title: "TidyTuesday 2023 w5 Pet Cats UK"
author: "Federica Gazzelloni"
execute:
comments: ""
eval: true
echo: true
warning: false
message: false
---
```{r}
library(tidyverse)
tuesdata <- tidytuesdayR::tt_load(2023, week = 05)
```
```{r}
cats_uk <- tuesdata$cats_uk
cats_uk_reference <- tuesdata$cats_uk_reference
```
```{r}
df <- cats_uk%>%
inner_join(cats_uk_reference,by="tag_id")
```
```{r}
df%>%
slice()%>%
glimpse()
```
```{r}
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)
```
```{r}
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())
```
```{r}
showtext.auto(enable = FALSE)
ggsave("w5_PCUK.png",
width = 7,height = 5)
```