Data Science with R (AECN 896-05)
  • Syllabus
  • Lecture Notes
  • Assignments
  • Exercises

On this page

  • 1 Exercise 1: Modify Plot Background and Grid
    • 1.0.1 Work here
    • 1.0.2 Answer
  • 2 Exercise 2: Change Title and Axis Text Styles
    • 2.0.1 Work here
    • 2.0.2 Answer
  • 3 Exercise 3: Alter Legend Properties
    • 3.0.1 Work here
    • 3.0.2 Answer
  • 4 Exercise 4: Adjust Axis Line Appearance and Title Position
    • 4.0.1 Work here
    • 4.0.2 Answer
  • 5 Exercise 5: Adjusting Strip Text in Facets
    • 5.0.1 Work here
    • 5.0.2 Answer
  • 6 Exercise 6: Modify Legend Key Shapes
    • 6.0.1 Work here
    • 6.0.2 Answer
  • 7 Exercise 7: Custom Axis Ticks and Labels
    • 7.0.1 Work here
    • 7.0.2 Answer
  • 8 Exercise 8: Modify Plot Margins
    • 8.0.1 Work here
    • 8.0.2 Answer
  • 9 Exercise 9: Remove Plot Labels

Ex-2-3: Modify themes of ggplot2 figures

Abstract
Data Visualization

Load Required Packages:

library(ggplot2)
library(dplyr)

1 Exercise 1: Modify Plot Background and Grid

Dataset: mtcars (available within R).

Task: Create a scatter plot of mpg against hp. Adjust theme to: (1) set plot background to lightblue, (2) change major grid to white, (3) remove minor grid and axis ticks.

1.0.1 Work here

Here is the output of successfully created plot:

1.0.2 Answer

Code
(
  ggplot(mtcars, aes(x = hp, y = mpg)) +
    geom_point() +
    theme(
      plot.background = element_rect(fill = "lightblue"),
      panel.grid.major = element_line(color = "white"),
      panel.grid.minor = element_blank(),
      axis.ticks = element_blank()
    )
)

2 Exercise 2: Change Title and Axis Text Styles

Dataset: diamonds (ggplot2).

2.0.1 Work here

Here is the output of successfully created plot:

2.0.2 Answer

Code
(
  ggplot(diamonds, aes(x = carat, y = price)) +
    geom_point(alpha = 0.5) +
    ggtitle("Price vs Carat") +
    theme(
      plot.title = element_text(face = "bold", color = "red"),
      axis.text = element_text(face = "italic")
    )
)

3 Exercise 3: Alter Legend Properties

Dataset: iris (available within R).

3.0.1 Work here

Here is the output of successfully created plot:

3.0.2 Answer

Code
(
  ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
    geom_point() +
    theme(
      legend.position = "bottom",
      legend.background = element_rect(fill = "grey90"),
      legend.title = element_text(face = "bold", color = "blue")
    )
)

4 Exercise 4: Adjust Axis Line Appearance and Title Position

Dataset: Fictitious dataset of monthly sales (sales_data created in setup).

4.0.1 Work here

Here is the output of successfully created plot:

4.0.2 Answer

Code
(
  ggplot(sales_data, aes(x = month, y = sales)) +
    geom_line(group = 1) +
    geom_point() +
    ggtitle("Monthly Sales") +
    theme(
      axis.line = element_line(size = 2),
      plot.title.position = "plot"
    )
)

5 Exercise 5: Adjusting Strip Text in Facets

Dataset: mpg (ggplot2).

5.0.1 Work here

Here is the output of successfully created plot:

5.0.2 Answer

Code
(
  ggplot(mpg, aes(x = displ, y = hwy)) +
    geom_point() +
    facet_wrap(~class) +
    theme(
      strip.text = element_text(color = "white"),
      strip.background = element_rect(fill = "darkblue")
    )
)

6 Exercise 6: Modify Legend Key Shapes

Dataset: iris (available within R).

6.0.1 Work here

Here is the output of successfully created plot:

6.0.2 Answer

Code
(
  ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species)) +
    geom_point() +
    theme(
      legend.key = element_rect()
    ) +
    guides(
      color = guide_legend(override.aes = list(shape = 15))
    )
)

7 Exercise 7: Custom Axis Ticks and Labels

Dataset: diamonds (ggplot2).

7.0.1 Work here

Here is the output of successfully created plot:

7.0.2 Answer

Code
(
  ggplot(diamonds, aes(x = carat, y = price)) +
    geom_point(alpha = 0.5) +
    theme(
      axis.ticks.x = element_line(size = 2),
      axis.text.y = element_text(color = "red")
    )
)

8 Exercise 8: Modify Plot Margins

Dataset: airquality (available within R).

8.0.1 Work here

Here is the output of successfully created plot:

8.0.2 Answer

Code
(
  ggplot(airquality, aes(x = Wind, y = Temp)) +
    geom_point() +
    theme(
      plot.margin = margin(2, 2, 2, 2, "cm")
    )
)

9 Exercise 9: Remove Plot Labels

Dataset: Fictitious dataset of weekly sales.

Task: Customize the theme to remove axis title and plot title.

Create Dataset:

  • Work here
  • Answer

Here is the output of successfully created plot:

Code
(
  ggplot(sales_weekly, aes(x = week, y = sales)) +
    geom_line() +
    ggtitle("Weekly Sales") +
    theme(
      plot.title = element_blank(),
      axis.title.x = element_blank(),
      axis.title.y = element_blank()
    )
)
 

Made with Quarto