2024

Import ggplot2 Library

# Don't forget to load the ggplot2 library
library(ggplot2)

Adjusting Text and Labels

ggplot(data = mpg, aes(x = displ, y = hwy)) + 
  geom_point(aes(color = class)) + 
  labs(title = "Engine Displacement vs. Highway MPG",
       x = "Displacement (L)", 
       y = "Highway MPG",
       color = "Vehicle Class")

Adjusting Text and Labels

Using Colors Effectively

ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) + 
  geom_point() + 
  scale_color_brewer(palette = "Set1")

Using Colors Effectively

Modifying Legend and Axis Properties

ggplot(data = mpg, aes(x = displ, y = hwy)) + 
  geom_point(aes(color = class)) + 
  theme(legend.position = "top",
        axis.text.x = element_text(angle = 45, hjust = 1))

Modifying Legend and Axis Properties

Introduction to the theme() Function

ggplot(data = mpg, aes(x = displ, y = hwy)) + 
  geom_point() + 
  labs(title = "Engine Displacement vs. Highway MPG",
       x = "Displacement (L)", 
       y = "Highway MPG",
       color = "Vehicle Class") +
  theme(axis.title.x = element_text(face = "bold"),
        plot.title = element_text(hjust = 0.5))

Introduction to the theme() Function

Common argument to theme

  • text elements:
    • text: General text appearance (applies to all text unless overridden).
    • plot.title: The plot title text.
    • plot.subtitle: The plot subtitle text.
    • plot.caption: The caption text.
    • axis.title.x, axis.title.y: X and Y axis titles.
    • axis.text.x, axis.text.y: X and Y axis text.
    • legend.title: The legend title text.
    • legend.text: The legend item texts.
  • legend:
    • legend.position: Position of the legend (“none”, “left”, “right”, “bottom”, “top”, or two-element numeric vector).
    • legend.background: Background of the legend.
    • legend.key: Background of the legend keys.
    • legend.box: Arrangement of multiple legends (“horizontal” or “vertical”).
  • panel:
    • panel.background: Background of the plotting area.
    • panel.grid.major, panel.grid.minor: Major and minor grid lines of the panel.
    • panel.border: Border around the plotting area.
  • axis lines:
    • axis.line.x, axis.line.y: Lines along the X and Y axes.
  • strip (for facets):
    • strip.background: Background of the facet labels.
    • strip.text.x, strip.text.y: Text of the facet labels along the X and Y direction.
  • plot margins:
    • plot.margin: Margins around the entire plot.

Fancy theme example


# Basic scatter plot
p <- ggplot(mpg, aes(x = displ, y = hwy)) +
  geom_point(aes(color = class)) +
  labs(title = "Engine Displacement vs. Highway MPG",
       x = "Displacement (liters)",
       y = "Highway MPG")

# Customize the plot
p + theme(
  # Customize plot title
  plot.title = element_text(face = "bold", size = 14, color = "blue"),

  # Customize panel background
  panel.background = element_rect(fill = "lightblue", colour = "black", linewidth = 1),

  # Customize axis lines
  axis.line = element_line(color = "darkblue", linewidth = 1),

  # Customize axis text
  axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1),

  # Customize legend background
  legend.background = element_rect(fill = "lightgray", linewidth= 0.5, linetype = "solid"),

  # Customize legend title and text
  legend.title = element_text(face = "bold", size = 10, color = "darkred"),
  legend.text = element_text(size = 8)
)

Fancy theme example

Applying Pre-made Themes: Minimal

# Minimal theme
ggplot(data = mpg, aes(x = displ, y = hwy)) + 
  geom_point() + 
  theme_minimal()

Applying Pre-made Themes: Minimal

Applying Pre-made Themes: Light

# Light theme
ggplot(data = mpg, aes(x = displ, y = hwy)) + 
  geom_point() + 
  theme_light()

Applying Pre-made Themes: Light

Applying Pre-made Themes: Dark

# Dark theme
ggplot(data = mpg, aes(x = displ, y = hwy)) + 
  geom_point() + 
  theme_dark()

Applying Pre-made Themes: Dark

Applying Pre-made Themes: Classic

# Classic theme
ggplot(data = mpg, aes(x = displ, y = hwy)) + 
  geom_point() + 
  theme_classic()

Applying Pre-made Themes: Classic

Question

Change the title of the following to “Impact of Engine Displacement on Highway MPG”, make the x-axis title bold, and set the legend position to the bottom of the plot.

ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) + 
  geom_point() + 
  labs(x = "Displacement (L)", 
       y = "Highway MPG",
       color = "Vehicle Class")

Answer

Click here for the answer
ggplot(data = mpg, aes(x = displ, y = hwy, color = class)) + 
  geom_point() + 
  labs(title = "Impact of Engine Displacement on Highway MPG",
       x = "Displacement (L)", 
       y = "Highway MPG",
       color = "Vehicle Class") +
  theme(legend.position = "bottom",
        axis.title.x = element_text(face = "bold"))

Question

Create a scatter plot using the theme_dark() with customized color for the plot background to a slightly lighter shade of grey given by ““#3E3E3E”.

ggplot(data = mpg, aes(x = displ, y = hwy)) + 
  geom_point() + 
  theme_dark()

Answer

Click here for the answer
ggplot(data = mpg, aes(x = displ, y = hwy)) + 
  geom_point() + 
  theme_dark(base_size = 14) +
  theme(plot.background = element_rect(fill = "#3E3E3E"))

Question

Apply the theme_minimal() to the following plot and adjust the axis text to be italic.

ggplot(data = mpg, aes(x = class)) + 
  geom_bar() + 
  theme_minimal()

Answer

Click here for the answer
ggplot(data = mpg, aes(x = class)) + 
  geom_bar(fill = "steelblue") + 
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, face = "italic"),
        axis.text.y = element_text(face = "italic"))