knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  echo = TRUE
)
library(Stat231)
library(tidyverse)
library(openintro)

Custom Data Visualization

For this lab, we will once again be adjusting the histogram we made in lab 1. Instead of using a prebuilt theme from the ggthemes package, we will be creating our own unique visualizations.

First, lets start with our plot from lab 3 that included labels.

ggplot(data = gpa_study_hours, 
       aes(x = gpa))+
  geom_histogram(color = "White", binwidth = 0.25)+
  labs(
    title = "Distribution of Grade Point Average",
    subtitle = "For 193 Introductory Statistics Students",
    x = "GPA",
    y = "Number of Students",
    caption = "Source: Private US University, 2012"
  )

To customize a ggplot image, we'll add a theme() function to the end and change specific parts of the graph. Here is a full list of what you can change with the theme() function. It is a substantial list but don't worry, we won't change all of them.

First I'm going to adjust my text. I'd prefer to have my title a little larger and in Times New Roman font (which R maps to serif). I'd also like the axis labels to be in italics.

ggplot(data = gpa_study_hours, 
       aes(x = gpa))+
  geom_histogram(color = "White", binwidth = 0.25)+
  labs(
    title = "Distribution of Grade Point Average",
    subtitle = "For 193 Introductory Statistics Students",
    x = "GPA",
    y = "Number of Students",
    caption = "Source: Private US University, 2012"
  )+
  theme(
    title = element_text(size = 16,
                              family = "serif"),
    axis.title = element_text(face = "italic")
  )

Next, I'm going to adjust the panel the plot is on. I'll change the background color and the grid.

ggplot(data = gpa_study_hours, 
       aes(x = gpa))+
  geom_histogram(color = "White", binwidth = 0.25)+
  labs(
    title = "Distribution of Grade Point Average",
    subtitle = "For 193 Introductory Statistics Students",
    x = "GPA",
    y = "Number of Students",
    caption = "Source: Private US University, 2012"
  )+
  theme(
    title = element_text(size = 16,
                         family = "serif"),
    axis.title = element_text(face = "italic"),
    panel.background = element_rect(fill = "lightblue"), 
    panel.grid.major.x = element_blank(), 
    panel.grid.minor.x = element_blank(),
    panel.grid.major.y = element_line(color = "blue"), 
    panel.grid.minor.y = element_blank()
  )

My next step will be to address the plot background (the white space behind the blue). I'd like it to be the same color. I'll also adjust the color and fill of the bars.

ggplot(data = gpa_study_hours, 
       aes(x = gpa))+
  geom_histogram(fill = "aquamarine4",
                 color = "lightblue", 
                 binwidth = 0.25)+
  labs(
    title = "Distribution of Grade Point Average",
    subtitle = "For 193 Introductory Statistics Students",
    x = "GPA",
    y = "Number of Students",
    caption = "Source: Private US University, 2012"
  )+
  theme(
    title = element_text(size = 16,
                         family = "serif"),
    axis.title = element_text(face = "italic"),
    panel.background = element_rect(fill = "lightblue"), 
    panel.grid.major.x = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.grid.major.y = element_line(color = "blue"),
    panel.grid.minor.y = element_blank(),
    plot.background = element_rect(fill = "lightblue")
  )


npaterno/stat231 documentation built on May 1, 2023, 6:07 p.m.