Initialize the graph with ggplot() and provide data
Map yr_mnth to x and avg_births to y
Add geom_line()
Code
labs_line <-labs(title ="Average US births", subtitle ="1994-2014",y ="Average number of US births",x ="Year/Month")ggp2_line <-ggplot(data = avg_birth_ym,mapping =aes(x = yr_mnth, y = avg_births)) +geom_line()ggp2_line + labs_line
GRAPH:
Source Code
---title: "Line graphs"format: html: toc: true toc-location: right toc-title: Contents code-fold: true out-height: '100%' out-width: '100%' df-print: pagedexecute: warning: false message: false---```{r}#| label: setup#| message: false#| warning: false#| include: falselibrary(tidyverse)library(lubridate)library(scales)library(knitr)library(kableExtra)library(colorspace)library(munsell)library(colorblindr)library(downlit)library(patchwork)# options ----options(repos ="https://cloud.r-project.org",dplyr.print_min =6, dplyr.print_max =6, scipen =9999)# fonts ----library(extrafont)library(sysfonts)# import fontextrafont::font_import(paths ="assets/Ubuntu/",prompt =FALSE)# add fontsysfonts::font_add(family ="Ubuntu", regular ="assets/Ubuntu/Ubuntu-Regular.ttf")# use fontshowtext::showtext_auto()# add themesource("R/theme_ggp2g.R")# set themeggplot2::theme_set(theme_ggp2g(base_size =13))# utilssource("R/utils.R")# install data packages ----install.packages("fivethirtyeight")install.packages("palmerpenguins")```:::: {.callout-note collapse="false" icon=false}## Graph info::: {style="font-size: 1.25em; color: #02577A;"}**Should I use this graph?**:::<br>```{r}#| label: full_code_display#| eval: true#| echo: false#| warning: false#| message: false#| out-height: '60%'#| out-width: '60%'#| fig-align: rightlibrary(fivethirtyeight)library(ggplot2)library(paletteer)library(ggthemes)# remove extra datesUS_births_2004_2014 <-filter(fivethirtyeight::US_births_2000_2014, year >2003)avg_birth_ym <- US_births_2004_2014 |> dplyr::bind_rows(fivethirtyeight::US_births_1994_2003) |> dplyr::mutate(mon_chr = dplyr::if_else(condition = month <10,true =paste0("0", month),false =as.character(month)),month = lubridate::month(date, label =TRUE),yr_mnth =paste0(year, "-", mon_chr),yr_mnth = lubridate::ym(yr_mnth),yr_qtr =paste0(lubridate::year(date),"/0", quarter(date)),yr_qtr =factor(yr_qtr, ordered =TRUE)) |> dplyr::group_by(yr_mnth) |> dplyr::summarise(avg_births =mean(births, na.rm =TRUE)) |> dplyr::ungroup() labs_line <-labs(title ="Average US births", subtitle ="1994-2014",y ="Average number of US births",x ="Year/Month")ggp2_line <-ggplot(avg_birth_ym,aes(y = avg_births, x = yr_mnth)) +geom_line()ggp2_line + labs_line```::: {style="font-size: 1.10em; color: #02577A;"}**This graph requires:**:::::: {style="font-size: 0.90em; color: #043b67;"}`r emo::ji("check")` a numeric (continuous) variable:::::: {style="font-size: 0.90em; color: #043b67;"}`r emo::ji("check")` a numeric (date) variable:::::::## Description Line graphs display changes of a numeric quantitative variable (on the `y` axis) over time (on the `x` axis). ## Getting set up :::: {.panel-tabset}### Packages::: {style="font-size: 1.15em; color: #1e83c8;"}**PACKAGES:**:::::: {style="font-size: 0.85em;"}Install packages.:::::: {style="font-size: 0.75em;"}```{r}#| label: pkg_code_line_graph#| code-fold: show#| eval: true#| echo: true#| warning: false#| message: false#| results: hideinstall.packages("fivethirtyeight")library(fivethirtyeight)library(ggplot2)```:::### Data::: {style="font-size: 1.15em; color: #1e83c8;"}**DATA:**:::::: {.column-margin}![](../www/538.png){fig-align="right" width="40%" height="40%"}:::<!-- ::: {.column-margin}![Artwork by @allison_horst](../www/lter_penguins.png){fig-align="right" width="30%" height="30%"}![](../www/538.png){fig-align="right" width="15%" height="15%"}![](../www/imdb.png){fig-align="right" width="20%" height="20%"}:::-->::: {style="font-size: 0.85em;"}We'll be using the `US_births_1994_2003` and `US_births_2000_2014` datasets from the `fivethirtyeight` package. - Remove duplicate dates from `US_births_2000_2014` - Binding these together (they have identical columns) - Use `year` and `month` to create `yr_mnth` - Use `year` and `quarter` to create `yr_qtr` - Group by yr_month to create `avg_births`, then un-groupSave as `avg_birth_ym`:::::: {style="font-size: 0.75em;"}```{r}#| label: data_line#| code-fold: show#| eval: true#| echo: trueUS_births_2004_2014 <-filter(fivethirtyeight::US_births_2000_2014, year >2003)avg_birth_ym <- US_births_2004_2014 |> dplyr::bind_rows(fivethirtyeight::US_births_1994_2003) |> dplyr::mutate(mon_chr = dplyr::if_else(condition = month <10,true =paste0("0", month),false =as.character(month) ),month = lubridate::month(date, label =TRUE),yr_mnth =paste0(year, "-", mon_chr),yr_mnth = lubridate::ym(yr_mnth),yr_qtr =paste0(lubridate::year(date),"/0", lubridate::quarter(date)),yr_qtr =factor(yr_qtr, ordered =TRUE) ) |> dplyr::group_by(yr_mnth) |> dplyr::summarise(avg_births =mean(births, na.rm =TRUE)) |> dplyr::ungroup() dplyr::glimpse(avg_birth_ym)```:::::::## The grammar:::: {.panel-tabset}### Code::: {style="font-size: 1.15em; color: #1e83c8;"}**CODE:**:::::: {style="font-size: 0.85em;"}Create labels with `labs()`Initialize the graph with `ggplot()` and provide `data`Map `yr_mnth` to `x` and `avg_births` to `y`Add `geom_line()`:::::: {style="font-size: 0.75em;"}```{r}#| label: code_graph_line#| code-fold: show#| eval: false#| echo: true #| warning: false#| message: false#| out-height: '100%'#| out-width: '100%'#| column: page-inset-right#| layout-nrow: 1labs_line <-labs(title ="Average US births", subtitle ="1994-2014",y ="Average number of US births",x ="Year/Month")ggp2_line <-ggplot(data = avg_birth_ym,mapping =aes(x = yr_mnth, y = avg_births)) +geom_line()ggp2_line + labs_line```:::### Graph::: {style="font-size: 1.15em; color: #1e83c8;"}**GRAPH:**:::```{r}#| label: create_graph_line#| eval: true#| echo: false#| warning: false#| message: false#| out-height: '100%'#| out-width: '100%'#| column: page-inset-right#| layout-nrow: 1labs_line <-labs(title ="Average US births", subtitle ="1994-2014",y ="Average number of US births",x ="Year/Month")ggp2_line <-ggplot(data = avg_birth_ym,mapping =aes(x = yr_mnth, y = avg_births)) +geom_line()ggp2_line + labs_line```::::