-
Notifications
You must be signed in to change notification settings - Fork 5
/
ggplot_efficient.qmd
79 lines (65 loc) · 1.92 KB
/
ggplot_efficient.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
---
title: "HealthyR demo: efficient ggplotting"
format: html
editor: visual
execute:
echo: true
warning: false
editor_options:
chunk_output_type: console
---
# Dataset
We are using the same dataset as our last few demos - licorice gargle RCT.
```{r}
#| echo: false
library(tidyverse)
# Load dataset
licodata_raw = medicaldata::licorice_gargle
# reading in 0,1, recoding
licodata = licodata_raw %>%
mutate(preOp_gender.factor = preOp_gender %>%
factor() %>%
fct_recode("Male" = "0",
"Female" = "1"),
preOp_asa.factor = preOp_asa %>%
factor() %>%
fct_recode("a normal healthy patient" = "1",
"a patient with mild systemic disease" = "2",
"a patient with severe systemic disease" = "3"),
treat.factor = treat %>%
factor() %>%
fct_recode("Sugar 5g" = "0",
"Licorice 0.5g" = "1"),
preOp_smoking.factor = preOp_smoking %>%
factor() %>%
fct_recode("Current" = "1",
"Past" = "2",
"Never" = "3")
)
```
# Plot of smokers
```{r}
barplot_count = function(df, var, xlab = ""){
df %>%
ggplot(aes(x = {{var}} %>%
fct_infreq() %>%
fct_rev())) +
geom_bar(fill = "seagreen3",
colour = "seagreen4") +
coord_flip() +
theme_classic() +
geom_label(aes(label = after_stat(count / sum(count)) %>% scales::percent(1)),
stat = "count",
hjust = 1,
size = 5) +
ylab("N Patients") +
xlab(xlab) +
scale_y_continuous(expand = c(0, 0))
}
```
```{r}
barplot_count(licodata, preOp_smoking.factor, "Smoking")
barplot_count(licodata, treat.factor, "Treatment")
barplot_count(licodata, preOp_asa.factor, "ASA score")
barplot_count(licodata, preOp_gender.factor, "Gender")
```