1 + 1[1] 2
Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
1 + 1[1] 2
You can add options to executable code like this
[1] 4
The echo: false option disables the printing of code (only output is displayed).
library(tidyverse)── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.5
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.0
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(gt)
library(wordcloud2)
library(babynames)babynames# A tibble: 1,924,665 × 5
year sex name n prop
<dbl> <chr> <chr> <int> <dbl>
1 1880 F Mary 7065 0.0724
2 1880 F Anna 2604 0.0267
3 1880 F Emma 2003 0.0205
4 1880 F Elizabeth 1939 0.0199
5 1880 F Minnie 1746 0.0179
6 1880 F Margaret 1578 0.0162
7 1880 F Ida 1472 0.0151
8 1880 F Alice 1414 0.0145
9 1880 F Bertha 1320 0.0135
10 1880 F Sarah 1288 0.0132
# ℹ 1,924,655 more rows
glimpse(babynames)Rows: 1,924,665
Columns: 5
$ year <dbl> 1880, 1880, 1880, 1880, 1880, 1880, 1880, 1880, 1880, 1880, 1880,…
$ sex <chr> "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", …
$ name <chr> "Mary", "Anna", "Emma", "Elizabeth", "Minnie", "Margaret", "Ida",…
$ n <int> 7065, 2604, 2003, 1939, 1746, 1578, 1472, 1414, 1320, 1288, 1258,…
$ prop <dbl> 0.07238359, 0.02667896, 0.02052149, 0.01986579, 0.01788843, 0.016…
babynames |>
filter(year == 1987)# A tibble: 21,403 × 5
year sex name n prop
<dbl> <chr> <chr> <int> <dbl>
1 1987 F Jessica 55991 0.0299
2 1987 F Ashley 54851 0.0293
3 1987 F Amanda 41786 0.0223
4 1987 F Jennifer 32700 0.0175
5 1987 F Sarah 27890 0.0149
6 1987 F Stephanie 22398 0.0120
7 1987 F Brittany 22228 0.0119
8 1987 F Nicole 20285 0.0108
9 1987 F Heather 18979 0.0101
10 1987 F Elizabeth 18606 0.00993
# ℹ 21,393 more rows
babynames |>
filter(year == 1987, sex == "F") |>
mutate(rank = row_number()) |>
mutate(percent = round(prop * 100, 1)) |>
filter(name == "Margaret") |>
gt()| year | sex | name | n | prop | rank | percent |
|---|---|---|---|---|---|---|
| 1987 | F | Margaret | 3145 | 0.00167838 | 90 | 0.2 |
The name Margaret was ranked 90th in popularity for girls in the year 1987.
babynames |>
filter(year == 1987) |> # use only one year
filter(sex == "F") |> # use only one sex
slice_max(prop, n=100) |> # use the top 100 names
select(name, n) |> # select the two relevant variables: the name and how often it occurs
wordcloud2(size = .5) + WCtheme(2) Word cloud of female name popularity from the year 1987
babynames |> # start with the data
filter(name == "Margaret", sex == "F") |> # choose the name and sex
mutate(percent = round(prop * 100, 1)) |> # create a new variable called percent
ggplot(aes(x = year, y = percent)) + # put year on the x-axis and prop (proportion) on y
geom_line(color = "pink") # make it a line graph and give the line a colorPopularity of the name Margaret over time.
babynames |>
filter(name == "Margaret", sex == "F") |>
mutate(percent = round(prop * 100, 1)) |>
slice_max(prop, n = 10) |>
select(year, sex, name, percent) |>
gt() |>
tab_header(title = "Years Margaret was most popular")| Years Margaret was most popular | |||
| year | sex | name | percent |
|---|---|---|---|
| 1916 | F | Margaret | 2.3 |
| 1917 | F | Margaret | 2.3 |
| 1918 | F | Margaret | 2.3 |
| 1915 | F | Margaret | 2.3 |
| 1920 | F | Margaret | 2.3 |
| 1919 | F | Margaret | 2.2 |
| 1914 | F | Margaret | 2.2 |
| 1921 | F | Margaret | 2.2 |
| 1913 | F | Margaret | 2.2 |
| 1922 | F | Margaret | 2.2 |
The ten years the name Margaret was most popular.
babynames |>
filter(name == "Margaret" | name == "Kirby", sex == "F") |>
mutate(percent = round(prop * 100, 1)) |>
ggplot(aes(x = year, y = percent, color = name)) +
geom_line()Popularity of the names Kirby and Margaret over time.
babynames |>
filter(name == "Poe" | name == "Finn" | name == "Kylo" , year > 1980) |>
filter(sex == "M") |>
ggplot(aes(x = year, y = n, color = name)) +
geom_line()Popularity of male names from the Star Wars sequel trilogy over time. These names increased in popularity in the time since the first movie was released in 2015.