Steps with Tidyverse & ggplot2()

Visualizations in R with ggplot2()

Welcome to Tidyverse! ๐ŸŽ‰

What Youโ€™ll Learn Today

  • ๐Ÿ“ฆ Installing & Loading โ€” Getting started with tidyverse and ggplot2
  • ๐Ÿ“Š Built-in Datasets โ€” Working with mpg and iris data
  • ๐Ÿ‘€ Viewing Data โ€” Exploring your data effectively
  • ๐Ÿ“ˆ Visualization Basics โ€” Creating beautiful plots with ggplot2
  • ๐Ÿ“‰ Advanced Plotting โ€” Lines, smooths, trends, and customization
  • ๐ŸŽฎ Interactive Demos โ€” Hands-on practice with real data

Letโ€™s dive in and start exploring data! ๐Ÿš€

Part 1: Installation & Setup

Getting Your Environment Ready ๐Ÿ› ๏ธ

Installing and loading the essential packages. Note: We can skip this part since we have already been using tidyverse in our environment!

Installing Tidyverse

One Package, Many Superpowers! โœจ

The tidyverse is a collection of R packages designed to work together seamlessly.

# Install tidyverse (only need to do this once!)
# This installs: dplyr, ggplot2, tidyr, readr, purrr, tibble, 
#                stringr, forcats, and more!
install.packages("tidyverse")

# Check if installation was successful
library(tidyverse)

# You should see a message like:
# โ”€โ”€ 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.5.1     โœ” tibble    3.2.1
# โœ” lubridate 1.9.3     โœ” tidyr     1.3.1
# โœ” purrr     1.0.2

Do It Yourself!

Tip

Pro Tip: If you only need specific packages, you can install them individually:

install.packages("ggplot2")  # Just for plotting
install.packages("dplyr")    # Just for data manipulation

Loading the Libraries

Load Packages in Every Session ๐Ÿ”„

You need to load packages each time you start R

Method 1: Load Entire Tidyverse

# Load all tidyverse packages at once
library(tidyverse)

# This gives you access to:
# - ggplot2 for visualization
# - dplyr for data manipulation
# - tidyr for data tidying
# - readr for data import
# - purrr for functional programming
# - tibble for modern data frames
# - stringr for strings
# - forcats for factors

Method 2: Load Individual Packages

# Load only what you need
library(ggplot2)  # For plotting
library(dplyr)    # For data wrangling

# Advantages:
# - Faster loading time
# - Clearer dependencies
# - Fewer potential conflicts

# Disadvantages:
# - More lines of code
# - Need to remember which 
#   package has which function

Note

For this work: Weโ€™ll use library(tidyverse) to load everything at once!

Verify Your Installation

Quick Check: Make sure everything installs correctly! โœ…

# Load tidyverse
library(tidyverse)

# Check versions
packageVersion("ggplot2")
# [1] '3.5.1'

packageVersion("dplyr")
# [1] '1.1.4'

# List all loaded tidyverse packages
tidyverse_packages()
# [1] "broom"      "conflicted" "cli"        "dbplyr"     "dplyr"     
# [6] "dtplyr"     "forcats"    "ggplot2"    "googledrive"...

# Check for conflicts (functions with same names)
tidyverse_conflicts()
# โ”€โ”€ Conflicts โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ tidyverse_conflicts() โ”€โ”€
# โœ– dplyr::filter() masks stats::filter()
# โœ– dplyr::lag()    masks stats::lag()

Template Code for Your Own Projects?

Wait!! You can use the below code to start each project with RStudio with a clean environment.

# Function to clear environment, plots, and console
remove_existing_objects <- function() {
  rm(list = ls()) # Remove all objects from environment
  graphics.off() # Clear all plots
  cat("\014") # Clear console
}

# Load packages (install if not already installed)
required_packages <- c('tidyverse', 'dplyr')

# Function to install and load packages
install_and_load <- function(packages) {
  for(pkg in packages) {
    if(!require(pkg, character.only = TRUE, quietly = TRUE)) {
      install.packages(pkg)
      library(pkg, character.only = TRUE)
    }
  }
}

# Call the functions to set up the environment
install_and_load(required_packages)
remove_existing_objects()

######################

# Add your code below this line

Part 2: Exploring Built-in Datasets

Practice with Real Data ๐Ÿ“Š

R comes with great datasets for learning. Some of these we have already seen in previous slides and work.

The MPG Dataset

Fuel Economy Data โ›ฝ

EPA fuel economy data for 38 popular car models (1999-2008)

Loading and First Look

# Load tidyverse
library(tidyverse)

# The mpg dataset comes with ggplot2
data(mpg)

# Quick overview
mpg
# A tibble: 234 ร— 11
#   manufacturer model    displ  year   cyl trans 
#   <chr>        <chr>    <dbl> <int> <int> <chr> 
# 1 audi         a4         1.8  1999     4 autoโ€ฆ
# 2 audi         a4         1.8  1999     4 manuaโ€ฆ
# 3 audi         a4         2    2008     4 manuaโ€ฆ
# 4 audi         a4         2    2008     4 autoโ€ฆ
# ...

# Dimensions
dim(mpg)
# [1] 234  11

# 234 rows (cars), 11 columns (variables)

Variable Descriptions

  • manufacturer - Car maker
  • model - Model name
  • displ - Engine displacement (liters)
  • year - Manufacturing year
  • cyl - Number of cylinders
  • trans - Transmission type
  • drv - Drive type (f/r/4)
  • cty - City miles per gallon
  • hwy - Highway miles per gallon
  • fl - Fuel type
  • class - Vehicle class

Viewing the MPG Data

Multiple Ways to Explore ๐Ÿ‘€

Different functions show different aspects of your data

# View first 6 rows
head(mpg)

# View last 6 rows
tail(mpg)

# View first n rows
head(mpg, 10)

# Structure of the dataset
str(mpg)
# tibble [234 ร— 11] (S3: tbl_df/tbl/data.frame)
#  $ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ...
#  $ model       : chr [1:234] "a4" "a4" "a4" "a4" ...
#  $ displ       : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...

# Summary statistics for all columns
summary(mpg)
#  manufacturer          model               displ            year     
#  Length:234         Length:234         Min.   :1.600   Min.   :1999  
#  Class :character   Class :character   1st Qu.:2.400   1st Qu.:1999  
#  Mode  :character   Mode  :character   Median :3.300   Median :2004  
#                                        Mean   :3.472   Mean   :2004  

# Get column names
names(mpg)
# [1] "manufacturer" "model"        "displ"        "year"         "cyl"         
# [6] "trans"        "drv"          "cty"          "hwy"          "fl"          
# [11] "class"

# View in RStudio viewer (interactive table)
View(mpg)

The Iris Dataset

Fisherโ€™s Classic Dataset ๐ŸŒธ

Measurements of iris flowers - a classic for classification

Loading and Exploring

# Load the iris dataset
data(iris)

# Convert to tibble (tidyverse format)
iris <- as_tibble(iris)

# View the data
iris
# A tibble: 150 ร— 5
#    Sepal.Length Sepal.Width Petal.Length
#           <dbl>       <dbl>        <dbl>
#  1          5.1         3.5          1.4
#  2          4.9         3            1.4
#  3          4.7         3.2          1.3
# ...

# Dimensions
dim(iris)
# [1] 150   5

# Column names
names(iris)
# [1] "Sepal.Length" "Sepal.Width"  "Petal.Length"
# [4] "Petal.Width"  "Species"

Variable Descriptions

  • Sepal.Length - Sepal length (cm)
  • Sepal.Width - Sepal width (cm)
  • Petal.Length - Petal length (cm)
  • Petal.Width - Petal width (cm)
  • Species - Flower species
    • setosa
    • versicolor
    • virginica

Perfect for: - Scatter plots - Classification - Comparing groups - Visualizing relationships

Comparing Datasets

Understanding Your Data ๐Ÿ“‹

Quick comparison of our two datasets: Each data set has its own structure, variables, and types, which we will explore in detail. We can use these structures to practice our data manipulation skills! Neat-O!

MPG Dataset

glimpse(mpg)
# Rows: 234
# Columns: 11
# $ manufacturer <chr> "audi", "audi"...
# $ model        <chr> "a4", "a4"...
# $ displ        <dbl> 1.8, 1.8, 2.0...
# $ year         <int> 1999, 1999...
# $ cyl          <int> 4, 4, 4, 4...
# $ trans        <chr> "auto(l5)"...
# $ drv          <chr> "f", "f", "f"...
# $ cty          <int> 18, 21, 20...
# $ hwy          <int> 29, 29, 31...
# $ fl           <chr> "p", "p", "p"...
# $ class        <chr> "compact"...

# 234 observations
# Mixed data types
# Categorical + Numeric

Iris Dataset

glimpse(iris)
# Rows: 150
# Columns: 5
# $ Sepal.Length <dbl> 5.1, 4.9, 4.7...
# $ Sepal.Width  <dbl> 3.5, 3.0, 3.2...
# $ Petal.Length <dbl> 1.4, 1.4, 1.3...
# $ Petal.Width  <dbl> 0.2, 0.2, 0.2...
# $ Species      <fct> setosa, setosa...

# 150 observations
# Mostly numeric
# One categorical variable
# Three balanced groups

Part 3: Visualization with ggplot2

Creating Beautiful Plots ๐Ÿ“ˆ

The grammar of graphics

Every ggplot() Function

The Three Essentials

Note

  1. Data: What to visualize
  2. Aesthetics (aes()): Map variables to visual properties
  3. Geometries (geom_*()): How to represent the data
ggplot(data = mpg, 
       aes(x = displ, y = hwy)) +
  geom_point()

Build in Layers

ggplot(mpg) +                    # 1. Data
  aes(x = displ, y = hwy) +      # 2. Aesthetics
  geom_point() +                 # 3. Geometry
  geom_smooth() +                # Add another layer
  labs(title = "Engine Size vs MPG")  # Labels

Aesthetic Mappings

Note

Common aesthetics you can map:

  • x - X-axis position
  • y - Y-axis position
  • color - Point/line color
  • fill - Fill color (bars, areas)
  • size - Point/line size
  • shape - Point shape
  • alpha - Transparency
  • linetype - Line pattern

Aesthetic Mappings, For Example

# Mapping vs Setting
ggplot(mpg, aes(x = displ, 
                y = hwy,
                color = class)) +  # Mapped
  geom_point(size = 3)  # Set (all points)

# Color maps from data
# Size is fixed at 3

Basic Scatter Plots with MPG

Exploring the relationship between engine size and fuel efficiency

# Simple scatter plot
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point()

# Add color by class
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point()

# Customize point size and transparency
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point(size = 3, alpha = 0.6)

# Add labels and title
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point(size = 3, alpha = 0.6) +
  labs(
    title = "Fuel Efficiency vs Engine Size",
    subtitle = "Larger engines generally less efficient",
    x = "Engine Displacement (liters)",
    y = "Highway MPG",
    color = "Vehicle Class",
    caption = "Data: EPA fuel economy dataset"
  )

# Apply a theme
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point(size = 3, alpha = 0.6) +
  labs(title = "Fuel Efficiency vs Engine Size",
       x = "Engine Displacement (liters)",
       y = "Highway MPG") +
  theme_minimal()

Basic Scatter Plots with MPG, Output

The code for each plot is available on the previous slides.

Linear Regression Lines Part 1

# Add linear regression

library(tidyverse)
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point(alpha = 0.5) +
  geom_smooth(method = "lm")

Linear Regression Lines Part 2

# Add linear regression
library(tidyverse)

# Linear model per group
ggplot(mpg, aes(x = displ, y = hwy,
                color = drv)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE)

Adding Linear Regression Lines with Formula and Rยฒ

library(tidyverse)
if (!require('ggpubr')) {
  install.packages('ggpubr')
  library('ggpubr')
}

# Show formula and Rยฒ
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point(alpha = 0.5) +
  geom_smooth(method = "lm", 
              formula = y ~ x) +
  stat_regline_equation(
    label.y = 45, 
    aes(label = ..eq.label..)
  )

Comprehensive Line Types Guide

Everything you need to know about lines

# 1. Straight line through data points (ordered by x)
ggplot(mpg %>% filter(manufacturer == "audi"), 
       aes(x = year, y = hwy)) +
  geom_line(aes(group = model)) +
  geom_point()

# 2. Linear regression line (lm)
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  geom_smooth(method = "lm",      # Linear model
              se = TRUE,           # Show confidence interval
              color = "blue",
              fill = "lightblue")

# 3. LOESS smooth (local regression - default)
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  geom_smooth(method = "loess",   # Local regression
              span = 0.75,         # Smoothness (0.1-1)
              se = TRUE)

# 4. GAM (Generalized Additive Model)
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  geom_smooth(method = "gam",     # More flexible curves
              formula = y ~ s(x, bs = "cs"))

# 5. Polynomial regression
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  geom_smooth(method = "lm",
              formula = y ~ poly(x, 2))  # Quadratic

# 6. Reference lines (horizontal, vertical, diagonal)
ggplot(mpg, aes(x = cty, y = hwy)) +
  geom_point() +
  geom_abline(intercept = 0, slope = 1, # Diagonal: y = x
              linetype = "dashed", 
              color = "red") +
  geom_hline(yintercept = mean(mpg$hwy),  # Horizontal
             linetype = "dotted", 
             color = "blue") +
  geom_vline(xintercept = mean(mpg$cty),  # Vertical
             linetype = "dotdash", 
             color = "green")

Comprehensive Line Types Guide, Output

Everything you need to know about lines

Customizing Line Appearance

# Available linetypes
df <- data.frame(
  x = 1:10,
  y = rep(1:6, length.out = 10)
)

ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point(alpha = 0.3) +
  geom_smooth(
    method = "lm", 
    linetype = "solid"
  )

# Options for linetype:
# "solid" (or 1)
# "dashed" (or 2)
# "dotted" (or 3)
# "dotdash" (or 4)
# "longdash" (or 5)
# "twodash" (or 6)

# Example with multiple lines
ggplot(mpg, aes(x = displ, y = hwy, 
                color = drv)) +
  geom_point() +
  geom_smooth(method = "lm", 
              se = FALSE,
              aes(linetype = drv))

Line Customization

# Control line appearance
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point(alpha = 0.4) +
  geom_smooth(
    method = "lm",
    se = TRUE,
    color = "darkred",      # Line color
    fill = "pink",          # CI fill color
    linewidth = 1.5,        # Line width
    linetype = "dashed",    # Line type
    alpha = 0.3             # CI transparency
  )

# Multiple smooths with different methods
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point(alpha = 0.4) +
  geom_smooth(
    method = "lm", 
    se = FALSE,
    color = "blue",
    aes(fill = "Linear")
  ) +
  geom_smooth(
    method = "loess",
    se = FALSE,
    color = "red",
    aes(fill = "LOESS")
  )

Faceting: Multiple Plots

Split Your Plot by Groups ๐Ÿ“Š

Create separate panels for different categories

# Facet by one variable
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  facet_wrap(~ class)

# Facet by two variables (grid)
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  facet_grid(drv ~ cyl)

# Free scales (each panel has own scale)
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  facet_wrap(~ class, scales = "free")

# Facet with different rows/columns
ggplot(mpg, aes(x = displ, y = hwy, color = drv)) +
  geom_point() +
  geom_smooth(method = "lm", se = FALSE) +
  facet_wrap(~ class, ncol = 3) +
  theme_minimal() +
  labs(
    title = "Fuel Efficiency by Vehicle Class",
    x = "Engine Displacement (L)",
    y = "Highway MPG",
    color = "Drive Type"
  )

Visualizing the Iris Dataset

Multiple visualization approaches

# Scatter Plot with Species
ggplot(iris, 
       aes(x = Petal.Length, 
           y = Petal.Width,
           color = Species)) +
  geom_point(size = 3, alpha = 0.7)

# Add smooth lines per species
ggplot(iris, 
       aes(x = Petal.Length, 
           y = Petal.Width,
           color = Species)) +
  geom_point(size = 2, alpha = 0.6) +
  geom_smooth(method = "lm", 
              se = TRUE) +
  theme_minimal() +
  labs(
    title = "Iris Petal Dimensions",
    x = "Petal Length (cm)",
    y = "Petal Width (cm)"
  )

Multiple Dimensions

# Sepal vs Petal lengths
ggplot(iris, 
       aes(x = Sepal.Length, 
           y = Petal.Length,
           color = Species,
           shape = Species)) +
  geom_point(size = 3, alpha = 0.7) +
  geom_smooth(method = "lm", 
              se = FALSE,
              linewidth = 1) +
  theme_bw()


# All pairwise comparisons
library(GGally) # install.packages('GGally') if needed
iris %>%
  select(everything()) %>%
  ggpairs(aes(color = Species))

More Plot Types

Beyond Scatter Plots ๐Ÿ“Š

Common visualization types for different data

# Box plots - Distribution by group
ggplot(mpg, aes(x = class, y = hwy, fill = class)) +
  geom_boxplot() +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "Highway MPG by Vehicle Class")

# Violin plots - Similar to boxplot but shows distribution shape
ggplot(mpg, aes(x = class, y = hwy, fill = class)) +
  geom_violin() +
  geom_jitter(width = 0.2, alpha = 0.3) +
  theme_minimal()

# Histogram - Distribution of a single variable
ggplot(mpg, aes(x = hwy)) +
  geom_histogram(bins = 30, fill = "steelblue", color = "black") +
  theme_minimal() +
  labs(title = "Distribution of Highway MPG")

# Density plot - Smooth version of histogram
ggplot(mpg, aes(x = hwy, fill = drv)) +
  geom_density(alpha = 0.5) +
  theme_minimal() +
  labs(title = "Highway MPG Distribution by Drive Type")

# Bar chart - Counts or values
ggplot(mpg, aes(x = class, fill = drv)) +
  geom_bar(position = "dodge") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Part 4: Interactive Visualizations

Code Generator Tool

Build Your Own Plot! ๐Ÿ› ๏ธ

Interactive tool to generate ggplot2 code

ggplot2 Code Generator

Click "Generate" to create ggplot2 code!

๐Ÿ’ช Quick Challenge: Complete Workflow

Your Turn! (5 minutes) โฑ๏ธ

Combine everything youโ€™ve learned into one analysis:

Task: Analyze the mpg dataset to answer: โ€œWhich manufacturer makes the most fuel-efficient cars?โ€

  1. Load tidyverse: library(tidyverse)
  2. Calculate average highway MPG by manufacturer using group_by() and summarize()
  3. Arrange results to find the top 5
  4. Create a bar chart showing the top 5 manufacturers
  5. Add appropriate labels and theme
  6. Bonus: Create a scatter plot showing the relationship between engine size and fuel efficiency, colored by your top 5 manufacturers

Hint: Youโ€™ll need: filter(), group_by(), summarize(), arrange(), ggplot(), geom_col() or geom_point()

Best Practices & Tips

Professional Workflow ๐ŸŒŸ

Tips for effective data visualization

ggplot2 Best Practices

Important

Make Your Plots Publication-Ready! ๐Ÿ“Š

Professional visualization guidelines

Doโ€™s: Added details to plot

# Clear, descriptive labels
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point() +
  labs(
    title = "Fuel Efficiency vs Engine Size",
    subtitle = "EPA data 1999-2008",
    x = "Engine Displacement (liters)",
    y = "Highway MPG",
    caption = "Source: EPA"
  )

# Appropriate color scales
ggplot(iris, aes(x = Petal.Length,
                 y = Petal.Width,
                 color = Species)) +
  geom_point() +
  scale_color_brewer(palette = "Set1")

# Readable text
+ theme(
    text = element_text(size = 12),
    axis.text.x = element_text(angle = 45, hjust = 1)
  )

# Show data clearly
+ geom_point(alpha = 0.6, size = 2)

Donโ€™ts: Common mistakes to avoid in plotting

# Avoid: No labels
ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point()  # What does this show?

# Avoid: Too many colors
ggplot(mpg, aes(x = displ, y = hwy,
                color = model)) +
  geom_point()  # 38 colors = chaos

# Avoid: Unreadable text
+ theme(axis.text.x = element_text(
    angle = 90, 
    size = 6))  # Too small!

# Avoid: Overplotting
+ geom_point(size = 10, alpha = 1)
  # Can't see individual points

# Avoid: 3D pie charts!
# Never use 3D unless necessary
# Pie charts are hard to read

Common ggplot2 Errors

Warning

Troubleshooting Tips ๐Ÿ”ง

Common mistakes and how to fix them

# ERROR 1: Using + instead of %>%
mpg %>%
  filter(hwy > 30) +  # โŒ Wrong!
  ggplot(aes(x = displ, y = hwy)) +
  geom_point()

# FIX: Pipe to data, then + for ggplot layers
mpg %>%
  filter(hwy > 30) %>%  # โœ… Correct!
  ggplot(aes(x = displ, y = hwy)) +
  geom_point()

# ERROR 2: Aesthetics in wrong place
ggplot(mpg) +
  geom_point(aes(x = displ, y = hwy), color = class)  # โŒ Won't map

# FIX: Put mapped aesthetics inside aes()
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +  # โœ…
  geom_point()

# ERROR 3: Overwriting ggplot object
plot <- ggplot(mpg, aes(x = displ, y = hwy))
plot + geom_point()  # Shows
plot  # Still empty!

# FIX: Reassign or build completely
plot <- ggplot(mpg, aes(x = displ, y = hwy)) +
        geom_point()  # โœ…

# ERROR 4: Forgetting to load libraries
ggplot(mpg, aes(x = displ, y = hwy)) +  # Error: could not find function "ggplot"

# FIX: Load tidyverse/ggplot2 first
library(tidyverse)  # โœ…

Saving Your Plots

Export for Reports & Presentations ๐Ÿ’พ

Save plots in various formats

# Create your plot
my_plot <- ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point(size = 3, alpha = 0.7) +
  geom_smooth(method = "lm", se = FALSE) +
  theme_minimal() +
  labs(
    title = "Fuel Efficiency by Engine Size",
    x = "Engine Displacement (L)",
    y = "Highway MPG"
  )

# Save as PNG (for presentations, web)
ggsave("fuel_efficiency.png", 
       plot = my_plot,
       width = 10, 
       height = 6, 
       dpi = 300)

# Save as PDF (for publications)
ggsave("fuel_efficiency.pdf", 
       plot = my_plot,
       width = 10, 
       height = 6)

# Save as SVG (scalable, for editing)
ggsave("fuel_efficiency.svg", 
       plot = my_plot,
       width = 10, 
       height = 6)

# Quick save the last plot
ggsave("last_plot.png")  # Automatically uses last plot

# Adjust resolution for print
ggsave("high_res.png", 
       width = 10, 
       height = 6, 
       dpi = 600,  # High quality for print
       bg = "white")  # Ensure white background

Summary & Resources

What Youโ€™ve Learned ๐ŸŽ‰

Your journey with tidyverse and ggplot2

Key Takeaways Part 1

Important

Youโ€™re Now Ready to Explore Data! ๐Ÿš€

Core concepts mastered today

Note

โœ… tidyverse Fundamentals:

  • Installing and loading packages
  • The pipe operator %>% for readable code
  • dplyr verbs: filter(), select(), mutate(), summarize(), group_by(), arrange()
  • Working with real datasets (mpg, iris)

Key Takeaways Part 2

Note

โœ… ggplot2 Visualization:

  • Grammar of graphics: data + aesthetics + geometries
  • Creating scatter plots, line plots, and more
  • Adding trend lines: linear regression (method = "lm") and smooths (method = "loess")
  • Customizing with themes, colors, and labels
  • Faceting for multiple plots
  • Line types and styles (solid, dashed, dotted, smooth)

โœ… Best Practices:

  • Clear labels and titles
  • Appropriate color schemes
  • Readable text and formatting
  • Saving plots for different uses

Quick Reference

Note

Cheat Sheet ๐Ÿ“‹

Quick commands for reference

# Load packages
library(tidyverse)

# View data
head(mpg)
glimpse(iris)
summary(data)

# dplyr operations
data %>%
  filter(condition) %>%        # Keep rows
  select(col1, col2) %>%       # Choose columns
  mutate(new_col = calc) %>%   # Create columns
  group_by(category) %>%       # Group data
  summarize(mean = mean(x)) %>%  # Aggregate
  arrange(desc(col))           # Sort

# Basic ggplot
ggplot(data, aes(x = var1, y = var2)) +
  geom_point() +               # Scatter plot
  geom_smooth(method = "lm") + # Add trend line
  labs(title = "Title") +      # Labels
  theme_minimal()              # Theme

# Save plot
ggsave("plot.png", width = 10, height = 6, dpi = 300)