generated from opensafely/research-template
/
overall_ab_boxplot_brit.R
93 lines (60 loc) · 2.11 KB
/
overall_ab_boxplot_brit.R
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
# # # # # # # # # # # # # # # # # # # # #
# This script:
# Generate a plot to show overall antibiotics prescribing rate by month
# By practice, by month, per 1000 patient
# # # # # # # # # # # # # # # # # # # # #
## Import libraries---
library('tidyverse')
library("ggplot2")
library('dplyr')
library('lubridate')
# impoprt data
df <- read_csv(
here::here("output", "measures", "measure_antibiotics_overall_brit.csv"),
col_types = cols_only(
# Identifier
practice = col_integer(),
# Outcomes
antibacterial_brit = col_double(),
population = col_double(),
value = col_double(),
# Date
date = col_date(format="%Y-%m-%d")
),
na = character()
)
# remove last month data
last.date=max(df$date)
df=df%>% filter(date!=last.date)
first_mon <- (format(min(df$date), "%m-%Y"))
last_mon <- (format(max(df$date), "%m-%Y"))
df$date <- as.Date(df$date)
df$cal_mon <- month(df$date)
df$cal_year <- year(df$date)
df_input <- df %>% filter(practice >0)
num_uniq_prac <- as.numeric(dim(table((df_input$practice))))
#line
df_gprate <- df_input %>% group_by(practice, cal_mon, cal_year) %>%
mutate(ab_rate_1000 = value*1000)
mean_rate <- df_gprate%>% group_by(cal_mon,cal_year)%>%
mutate(mean_rate = mean(ab_rate_1000,na.rm=TRUE),
five=quantile(ab_rate_1000, na.rm=TRUE, c(0.05)),
ninefive= quantile(ab_rate_1000, na.rm=TRUE, c(0.95)))
#boxplot
overallbox_brit <- ggplot(mean_rate, aes(group=date,x=date,y=ab_rate_1000)) +
geom_boxplot(outlier.shape = NA)+
coord_cartesian(ylim = quantile(mean_rate$ab_rate_1000, c(0.025, 0.975),na.rm=TRUE))+
geom_vline(xintercept = as.numeric(as.Date("2019-12-31")))+
geom_vline(xintercept = as.numeric(as.Date("2020-12-31")))+
labs(
title = "Overall antibiotics prescribing rate by practice - Brit",
subtitle = paste(first_mon,"-",last_mon),
caption = paste("Data from approximately", num_uniq_prac,"TPP Practices"),
x = "Time",
y = "Mean Prescribing rate distribution by practice"
)
overallbox_brit
ggsave(
plot= overallbox_brit,
filename="overallbox_brit.jpeg", path=here::here("output"),
)