Collect garbage and disable graphics, and clear environment :–

gc()
##          used (Mb) gc trigger (Mb) max used (Mb)
## Ncells 456311 24.4     981921 52.5   643648 34.4
## Vcells 822689  6.3    8388608 64.0  1648779 12.6
graphics.off()
rm(list = ls(all.names = TRUE))

Load required libraries

# load Packages
library(tidyverse)   # for data wrangling 
library(flexdashboard)  # framework for outputs
library(shiny)       # for responsive plots
library(plotly)      # for interactive plots 
library(DT)          # for data tables
library(ggplot2)     # for graphing 
library(ggpmisc)     # for helper functions of ggplot2
library(gapminder)   # for source dataset
library(tidymodels)  # for modelling  
library(vip)         # for variable importance
library(kableExtra)  # for html tables
library(gganimate)   # for animating a plot
library(gifski)       # for gif outputs from animated plots
library(av)         # for video output

Plots

Static

This is a basic static ggplot2 plot of lifeExp ~ gdpPercap by pop and continent.

p <- gapminder %>%
  filter(year==1977) %>%
  ggplot( aes(x = gdpPercap, 
              y = lifeExp, 
              size = pop, 
              color = country)) +
  geom_point() +
  geom_jitter() + 
  geom_smooth(method = "lm",
              formula = y ~ log10(x), 
              se = FALSE, 
              size = 0.5, 
              color = "black", 
              linetype="dotted")+
  scale_colour_manual(values = country_colors) +
  labs(title = "Life Expectancy vs GDP Per Capita",
       y = "Life Expectancy",
       x = "GDP Per Capita")+
  scale_x_continuous(labels = dollar) +
  theme(plot.title = element_text(hjust = 0.5),
        legend.position = c(0.88, 0.4),
        legend.title = element_blank(),
        legend.background = element_blank())

ggsave(filename = "Life Expectancy vs GDP Per Capita.png",
       width = 7, height = 7,
       units = "in")

ggplotly(p)

GG Animate

This is a basic gganimate animation using ggplot2, a plot of lifeExp ~ gdpPercap by pop and continent.

p2 <- gapminder %>%
  ggplot(aes(x = gdpPercap, 
             y = lifeExp, 
           size = pop, 
           colour = country)) +
  geom_point(alpha = 0.7, 
             show.legend = FALSE) +
  geom_smooth(method = "lm",
              formula = y ~ log10(x), 
              se = FALSE, 
              size = 0.5, 
              color = "black", 
              linetype="dotted")+
  scale_colour_manual(values = country_colors) +
  scale_size(range = c(2, 12)) +
  scale_x_log10() +
  labs(title = "Life Expectancy vs GDP Per Capita by 'Year: {frame_time}'",
       y = "Life Expectancy",
       x = "Logarithmic GDP Per Capita") +
  theme(plot.title = element_text(hjust = 0.5))+
  transition_time(year) +
  ease_aes('linear')

Save as gif (.gif)

# save as gif
p2_gif <- animate(p2, 
                  fps = 10, 
                  width = 750, 
                  height = 450,
                  renderer = gifski_renderer())

anim_save(p2_gif, filename = "life expectancy.gif")

p2_gif

Save as video (.mp4)

# save as a video
p2_vid <- animate(p2,
                  width = 800,
                  height = 450,
                  renderer = ffmpeg_renderer())

anim_save(p2_vid, filename = "life expectancy.mp4")

p2_vid

Plotly Animate

This is a basic plotly animation using and ggplot2 plot of lifeExp ~ gdpPercap by pop and continent.

p3 <- gapminder %>%
  ggplot(aes(gdpPercap, lifeExp, color = continent)) +
  geom_point(aes(size = pop, frame = year, ids = country)) +
  scale_x_log10() +
   scale_colour_manual(values = continent_colors) +
  labs(title = "Life Expectancy vs GDP Per Capita by Year",
       y = "Life Expectancy",
       x = "Logarithmic GDP Per Capita") +
  theme(plot.title = element_text(hjust = 0.5)) 

ggplotly(p3)%>% 
  animation_opts(1000, 
                 easing = "elastic", 
                 redraw = FALSE) %>% 
  animation_button(x = 1, 
                   xanchor = "right",
                   y = 0,
                   yanchor = "bottom") %>%
  animation_slider(currentvalue = list(prefix = "YEAR ", 
                                       font = list(color="blue")))

set the default engine

lm_model <- linear_reg() %>% 
  set_engine('lm') %>% # adds lm implementation of linear regression
  set_mode('regression')

Get the gapminder dataset

data <- gapminder

Fit model

lm_fit <- lm_model %>% 
  fit(lifeExp ~ gdpPercap + pop + continent, data = data)

View fit properties

tidy(lm_fit) %>%
  filter(p.value < 0.05) %>%
  kbl(caption = "Significant Factors") %>%
  kable_classic(full_width = F, html_font = "Cambria")
Significant Factors
term estimate std.error statistic p.value
(Intercept) 47.8140784 0.3395416 140.819490 0.0000000
gdpPercap 0.0004495 0.0000235 19.157877 0.0000000
pop 0.0000000 0.0000000 3.325735 0.0009007
continentAmericas 13.4759434 0.6000420 22.458335 0.0000000
continentAsia 8.1926319 0.5712355 14.341952 0.0000000
continentEurope 17.4726928 0.6246165 27.973474 0.0000000
continentOceania 18.0833044 1.7822543 10.146310 0.0000000

View \(R^2\)

glance(lm_fit)%>%
  kbl(caption = "R-Squared Value") %>%
  kable_classic(full_width = F, html_font = "Cambria")
R-Squared Value
r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC deviance df.residual nobs
0.5820689 0.5805912 8.365349 393.9129 0 6 -6033.827 12083.65 12127.18 118754.5 1697 1704

variable importance

ggplotly(vip(lm_fit, 
    aesthetics = list(fill = 2:7)), tooltip = "Importance")