#Download Packages and Dependencies

# List of packages and dependencies
my_packages <- c("devtools", "Rtools", "rlang", "tidyverse", "broom", "coefplot", "cowplot", "gapminder", "GGally", "ggrepel", "ggridges", "gridExtra","here", "interplot", "margins", "maps", "mapproj", "mapdata", "MASS", "quantreg", "rlang", "scales", "survey", "srvyr", "viridis", "viridisLite")

# Install the packages directly from the CRAN
install.packages(my_packages, repos = "http://cran.rstudio.com")
## Installing packages into '/cloud/lib/x86_64-pc-linux-gnu-library/4.2'
## (as 'lib' is unspecified)
## Warning: packages 'Rtools', 'margins', 'MASS' are not available for this version of R
## 'MASS' version 7.3-60.2 is in the repositories but depends on R (>= 4.4.0)
## 'MASS' version 7.3-60.2 is in the repositories but depends on R (>= 4.5)
## 
## Versions of these packages for your version of R might be available elsewhere,
## see the ideas at
## https://cran.r-project.org/doc/manuals/r-patched/R-admin.html#Installing-packages
## Warning: dependency 'Matrix' is not available
## also installing the dependencies 'curl', 'vctrs', 'credentials', 'httr2', 'htmltools', 'sass', 'labelled', 'nloptr', 'RcppEigen', 'gert', 'gh', 'whisker', 'bslib', 'downlit', 'htmlwidgets', 'xopen', 'brew', 'brio', 'digest', 'evaluate', 'jsonlite', 'praise', 'processx', 'ps', 'waldo', 'assertthat', 'zoo', 'xts', 'lazyeval', 'crosstalk', 'broom.helpers', 'patchwork', 'lme4', 'coda', 'usethis', 'desc', 'miniUI', 'pkgbuild', 'pkgdown', 'pkgload', 'profvis', 'rcmdcheck', 'remotes', 'roxygen2', 'rversions', 'sessioninfo', 'testthat', 'urlchecker', 'plyr', 'reshape2', 'useful', 'dygraphs', 'plotly', 'ggplot2', 'ggstats', 'rprojroot', 'abind', 'arm', 'interactionTest', 'SparseM', 'MatrixModels', 'minqa', 'numDeriv', 'mitools', 'RcppArmadillo'
## Warning in install.packages(my_packages, repos = "http://cran.rstudio.com"):
## installation of package 'RcppEigen' had non-zero exit status
## Warning in install.packages(my_packages, repos = "http://cran.rstudio.com"):
## installation of package 'MatrixModels' had non-zero exit status
## Warning in install.packages(my_packages, repos = "http://cran.rstudio.com"):
## installation of package 'quantreg' had non-zero exit status
## Warning in install.packages(my_packages, repos = "http://cran.rstudio.com"):
## installation of package 'survey' had non-zero exit status
## Warning in install.packages(my_packages, repos = "http://cran.rstudio.com"):
## installation of package 'srvyr' had non-zero exit status
## Warning in install.packages(my_packages, repos = "http://cran.rstudio.com"):
## installation of package 'lme4' had non-zero exit status
## Warning in install.packages(my_packages, repos = "http://cran.rstudio.com"):
## installation of package 'arm' had non-zero exit status
## Warning in install.packages(my_packages, repos = "http://cran.rstudio.com"):
## installation of package 'interplot' had non-zero exit status

Load the gapminder dataset

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.1     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── 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(gapminder)
gapminder
## # A tibble: 1,704 × 6
##    country     continent  year lifeExp      pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Afghanistan Asia       1952    28.8  8425333      779.
##  2 Afghanistan Asia       1957    30.3  9240934      821.
##  3 Afghanistan Asia       1962    32.0 10267083      853.
##  4 Afghanistan Asia       1967    34.0 11537966      836.
##  5 Afghanistan Asia       1972    36.1 13079460      740.
##  6 Afghanistan Asia       1977    38.4 14880372      786.
##  7 Afghanistan Asia       1982    39.9 12881816      978.
##  8 Afghanistan Asia       1987    40.8 13867957      852.
##  9 Afghanistan Asia       1992    41.7 16317921      649.
## 10 Afghanistan Asia       1997    41.8 22227415      635.
## # ℹ 1,694 more rows
# Define the data
p <- ggplot(data = gapminder)
p

# Specify x and y aesthetics mapping
p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,
                                            y = lifeExp))
p

geom_point() - to add the points from the mapped data, add a geom_point() to the ggplot()

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,
                                            y = lifeExp)) +
  geom_point()
p

Layer 1: Smoothing

p <- ggplot(data = gapminder, mapping = aes(x = gdpPercap,
                                            y = lifeExp)) +
  geom_smooth()
p
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

Layer 2: Add points - Add the geom_point() with geom_smooth to place points in the smoothed line

# Add the geom_point first, then geom_smooth
p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp)) +
  geom_point() +
  geom_smooth()
p
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

Smoothing: Alternative methods - geom_smooth() using the GAM (Generalized Additive Model) by default for smoothing. You can specify the method by using the method parameter in geom_smooth()

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp)) +
  geom_point() +
  geom_smooth(method="lm")
p
## `geom_smooth()` using formula = 'y ~ x'

Set the smoothing method to “loess”

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp)) +
  geom_point() +
  geom_smooth(method="loess")
p
## `geom_smooth()` using formula = 'y ~ x'

Scaling data - in previous graph, GDP per capitais really bunched up. Let’s trasnform the data by scaling it with log base 10

# Scale the GDP using log base 10 and the gam method
p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp)) +
  geom_point() +
  geom_smooth(method="gam") +
  scale_x_log10()
p
## `geom_smooth()` using formula = 'y ~ s(x, bs = "cs")'

Scaling: adding labels - to add labels to the x-axis, we can specify them using the “labels” attribute in the scale (scale_x_log10() function. Some helpful functions are in the “scales” package. You can import “scales” package as library(scales) or refereence it explicitly in the code, without the import)

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp)) +
  geom_point() +
  geom_smooth(method="gam") +
  scale_x_log10(labels = scales::dollar)
p
## `geom_smooth()` using formula = 'y ~ s(x, bs = "cs")'

Setting Aesthetics - to set the size and shape of the data, define the aesthetics in the geom_ function.

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp)) +
  geom_point(color="red", size = 0.5) +
  geom_smooth(method="gam") +
  scale_x_log10(labels = scales::dollar)
p
## `geom_smooth()` using formula = 'y ~ s(x, bs = "cs")'

You can set as many aesthetic properties as you would like to each geom_ in the plot.

# For the smooth line, set it to green, use the lm smoothing
# no standard error and a line size of 5
# For the points, let's make them slightly transparent
p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp)) +
  geom_point(color="#f7797d", size = 0.3) +
  geom_smooth(color = "darkblue", method="lm", se = FALSE, size = 0.5) +
  scale_x_log10(labels = scales::dollar)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
p
## `geom_smooth()` using formula = 'y ~ x'

Titles, labels, and captions - to provide the audience with critical information, we should add label, title, and captions to our graphs. To do so, we use the “labs” function and add it to our ggplot

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp)) +
  geom_point(color="#f7797d", size = 0.3) +
  geom_smooth(color = "darkblue", method="lm", se = FALSE, size = 0.5) +
  scale_x_log10(labels = scales::dollar) +
  labs(x = "GDP per capita",
       y = "Life Expectancy in Years",
       title = "Economics Growth and Life Expectancy",
       subtitle = "Data points are country-years",
       caption = "Source: Gapminder, scaling log 10")
p
## `geom_smooth()` using formula = 'y ~ x'

Mapping data to aesthetics - to make aesthetics dependent of data, we set the attribute in the aesthetics mapping of aes:

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp,
                          color = continent)) +
  geom_point(size = 0.5) +
  geom_smooth(method="loess", size = 0.5) +
  scale_x_log10(labels = scales::dollar) +
  labs(x = "GDP per capita",
       y = "Life Expectancy in Years",
       title = "Economics Growth and Life Expectancy",
       subtitle = "Data points are country-years",
       caption = "Source: Gapminder, scaling log 10")
p
## `geom_smooth()` using formula = 'y ~ x'

Mapping Aesthetics per Geom - instead of mapping the entier plot’s aesthetics, you can map the aesthetics for individual geoms by specifying the mapping = aes() for that geom.

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp)) +
  geom_point(mapping = aes(color = continent),size = 0.5) +
  geom_smooth(method="loess", size = 0.5) +
  scale_x_log10(labels = scales::dollar) +
  labs(x = "GDP per capita",
       y = "Life Expectancy in Years",
       title = "Economics Growth and Life Expectancy",
       subtitle = "Data points are country-years",
       caption = "Source: Gapminder, scaling log 10")
p
## `geom_smooth()` using formula = 'y ~ x'