Skip to content

Commit 41d54bc

Browse files
authored
Merge pull request #2 from stekhoven/1-create-a-panel-of-figures
Added multi-panel figure to analysis
2 parents acfb20e + 5fefb86 commit 41d54bc

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

analysis.qmd

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ library(babynames)
1010
library(knitr)
1111
library(dplyr)
1212
library(ggplot2)
13+
library(tidyr)
14+
library(pheatmap)
1315
```
1416

17+
# Writing in Quarto part
18+
1519
Let's have a look at the first couple of rows in the data:
1620

1721
```{r}
@@ -67,3 +71,45 @@ get_most_frequent(babynames, select_sex = "M") |>
6771
plot_top()
6872
```
6973

74+
# git and github part
75+
76+
We want to plot multiple panels in a figure - an example for the most liked girl's name can be seen in @fig-mult-girls.
77+
78+
79+
```{r}
80+
#| label: fig-mult-girls
81+
#| layout: [[50,50], [100]]
82+
#| fig-cap: "Most favourite girl names - a closer look!"
83+
#| fig-subcap:
84+
#| - "Top 5 girl's names over the years"
85+
#| - "Top 10 girl's names over the years"
86+
#| - "Heatmap of top 30 girl's names versus years"
87+
# get most frequent girl names from 2010 onwards
88+
from_year <- 2010
89+
most_freq_girls <- get_most_frequent(babynames, select_sex = "F",
90+
from = from_year)
91+
92+
# plot top 5 girl names
93+
most_freq_girls |>
94+
plot_top(top = 5)
95+
96+
# plot top 10 girl names
97+
most_freq_girls |>
98+
plot_top(top = 10)
99+
100+
# get top 30 girl names in a matrix
101+
# with names in rows and years in columns
102+
prop_df <- babynames |>
103+
filter(name %in% most_freq_girls$most_frequent$name[1:30] & sex == "F") |>
104+
filter(year >= from_year) |>
105+
select(year, name, prop) |>
106+
pivot_wider(names_from = year,
107+
values_from = prop)
108+
109+
prop_mat <- as.matrix(prop_df[, 2:ncol(prop_df)])
110+
rownames(prop_mat) <- prop_df$name
111+
112+
# create heatmap
113+
pheatmap(prop_mat, cluster_cols = FALSE, scale = "row")
114+
```
115+

0 commit comments

Comments
 (0)