Skip to content

Commit dc0771f

Browse files
committed
completing exercise setup
1 parent 9a4b8ed commit dc0771f

1 file changed

Lines changed: 64 additions & 22 deletions

File tree

inst/tutorials/Lesson1/Lesson1.Rmd

Lines changed: 64 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,49 @@ description: "Learnr tutorial to start off with ggplot2. Created for a short wor
1111

1212
```{r setup, include=FALSE}
1313
library(learnr)
14+
library(tibble)
15+
library(ggplot2)
16+
library(dplyr)
17+
library(tidyr)
18+
library(readr)
19+
library(patchwork)
20+
library(scico)
21+
1422
knitr::opts_chunk$set(echo = TRUE)
23+
24+
hospital_inpatients <- read_csv("data/PasientSomatSykeh2.csv")
25+
hospital_inpatients_grouped <- hospital_inpatients %>%
26+
group_by(aar, gruppe)
27+
28+
# calculate sum of patients per year and group
29+
hospital_inpatients_summary <- hospital_inpatients_grouped %>%
30+
summarise(sum = sum(antall.pasienter))
31+
32+
my_plot1 <- ggplot(hospital_inpatients_summary,
33+
aes(x = as.factor(aar), y = sum)) +
34+
geom_col(aes(fill = gruppe), position = position_dodge())
35+
36+
hospital_inpatients_median <- hospital_inpatients_grouped %>%
37+
summarise(median = median(antall.pasienter),
38+
stderr = sd(antall.pasienter))
39+
40+
my_plot2 <- ggplot(hospital_inpatients_median,
41+
aes(as.factor(aar), median)) +
42+
geom_col(aes(fill = gruppe), position = position_dodge())
43+
44+
final.plot <- ggplot(hospital_inpatients_grouped,
45+
aes(x = as.factor(aar), y = antall.pasienter)) +
46+
geom_col(aes(fill = gruppe), position = position_dodge()) +
47+
facet_grid(rows = vars(kjonn), cols = vars(alder))
48+
49+
my_plot1_nicer <- my_plot1 +
50+
scale_fill_brewer(type = "div", palette = "PRGn",
51+
name = "Type of stay") +
52+
xlab("Year") +
53+
ylab("Total number of patients") +
54+
labs(title = "Number of patients in hospitals per year",
55+
subtitle = "groupped by type of stay",
56+
caption = "data source: aggregated data from SSB")
1557
```
1658

1759

@@ -181,27 +223,27 @@ sub-plots, called _facets_.
181223

182224
```{r, fig.cap="No splitting"}
183225
# plotting totals, grouped, non-stacked
184-
ggplot( hospital_inpatients_summary,
185-
aes( x = as.factor( aar ), y = sum ) ) +
186-
geom_col( aes( fill = gruppe ), position = position_dodge() )
226+
ggplot(hospital_inpatients_summary,
227+
aes(x = as.factor(aar), y = sum)) +
228+
geom_col(aes(fill = gruppe), position = position_dodge())
187229
```
188230

189231

190232
```{r, fig.cap="Splitting once", out.width = "100%", fig.height = 5, fig.width = 8}
191233
# splitting this - checking difference with regard to gender
192-
ggplot( hospital_inpatients_grouped,
193-
aes( x = as.factor( aar ), y = antall.pasienter ) ) +
194-
geom_col( aes( fill = gruppe ), position = position_dodge() ) +
195-
facet_wrap( facets = vars( kjonn ) )
234+
ggplot(hospital_inpatients_grouped,
235+
aes(x = as.factor(aar), y = antall.pasienter)) +
236+
geom_col(aes(fill = gruppe), position = position_dodge()) +
237+
facet_wrap(facets = vars(kjonn))
196238
```
197239

198240

199241
```{r, fig.cap="Splitting twice", out.width = "100%", fig.height = 5, fig.width = 8}
200242
# splitting again - with regard to age
201-
final.plot <- ggplot( hospital_inpatients_grouped,
202-
aes( x = as.factor( aar ), y = antall.pasienter ) ) +
203-
geom_col( aes( fill = gruppe ), position = position_dodge() ) +
204-
facet_grid( rows = vars( kjonn ), cols = vars( alder ) )
243+
final.plot <- ggplot(hospital_inpatients_grouped,
244+
aes(x = as.factor(aar), y = antall.pasienter)) +
245+
geom_col(aes(fill = gruppe), position = position_dodge()) +
246+
facet_grid(rows = vars(kjonn), cols = vars(alder))
205247
final.plot
206248
```
207249

@@ -224,9 +266,9 @@ Of course, we can plot several various types of graphs on one plot.
224266
by saving it to an object and adding onto the object.
225267

226268
```{r}
227-
my_plot1 <- ggplot( hospital_inpatients_summary,
228-
aes( x = as.factor( aar ), y = sum ) ) +
229-
geom_col( aes( fill = gruppe ), position = position_dodge() )
269+
my_plot1 <- ggplot(hospital_inpatients_summary,
270+
aes(x = as.factor(aar), y = sum)) +
271+
geom_col(aes(fill = gruppe), position = position_dodge())
230272
231273
my_plot1 +
232274
geom_line(aes(group = gruppe))
@@ -290,9 +332,9 @@ my_plot2 +
290332
ymin = (median - stderr),
291333
ymax = (median + stderr),
292334
group = gruppe
293-
),
335+
),
294336
position = position_dodge(width = 0.9)
295-
)
337+
)
296338
```
297339

298340
---
@@ -443,14 +485,14 @@ my_plot1_nicer +
443485
plot.title = element_text(
444486
size = 20, # text size in pts
445487
face = "bold"
446-
),
488+
),
447489
axis.title = element_text(
448490
size = 16
449-
),
491+
),
450492
axis.text.x = element_text(
451493
angle = 90
452-
)
453-
)
494+
)
495+
)
454496
```
455497

456498
**2. Change the position of the legend**
@@ -459,7 +501,7 @@ my_plot1_nicer +
459501
my_plot1_nicer +
460502
theme(
461503
legend.position = "bottom"
462-
)
504+
)
463505
```
464506

465507
**3. Change the background color**
@@ -468,7 +510,7 @@ my_plot1_nicer +
468510
my_plot1_nicer +
469511
theme(
470512
panel.background = element_rect(fill = "salmon")
471-
)
513+
)
472514
```
473515

474516

0 commit comments

Comments
 (0)