Datasets

# Load package(s)
library(ggplot2)
library(tidyverse)
library(ggthemes)
library(dplyr)
library(wesanderson)

# Read in the cdc dataset
cdc <- read_delim(file = "data/cdc.txt", delim = "|") %>%
  mutate(genhlth = factor(genhlth,
    levels = c("excellent", "very good", "good", "fair", "poor"),
    labels = c("Excellent", "Very Good", "Good", "Fair", "Poor")
  ))

# Set seed
set.seed(8221984)

# Selecting a random subset of size 100
cdc_small <- cdc %>% sample_n(100)

Exercise 1

# Building plot
plot_01 <-ggplot(data = cdc_small, aes(x = height, y = weight)) +
  geom_point(size = 3, aes(shape = genhlth, color = genhlth)) +
  scale_y_continuous(
    name = "Weight in Pounds",
    limits = c(100, 300),
    breaks = c(seq(100, 350, 25)),
    trans = "log10",
    labels = scales::unit_format(unit = "lbs")
  ) +
  scale_x_continuous(
    name = "Height in Inches",
    limits = c(55, 80),
    breaks = seq(55, 80, 5),
    labels = scales::unit_format(unit = "in")
  ) +
  scale_shape_manual(
    name = "General\nHealth?",
    labels = c("Excellent", "Very Good", "Good", "Fair", "Poor"),
    values = c(17, 19, 15, 9, 4)
  ) +
  scale_color_brewer(
    name = "General\nHealth?",
    labels = c("Excellent", "Very Good", "Good", "Fair", "Poor"),
    palette = "Set1"
  ) +
  theme(
    legend.position = c(1, 0),
    legend.justification = c(1, 0)
  ) +
  labs(title = "CDC BRFSS: Weight by Height")
plot_01

plot_01 +
  theme_void() +
  theme(plot.title = element_text(face = "italic", colour = "#4E2A84"),
    plot.background = element_rect(colour = "orange", size = 6),
    plot.margin = margin(l = 10, b = 15, r = 10, t = 10),
    panel.background = element_rect(fill = "lawngreen"),
    panel.grid.major = element_line(color = "darkcyan", size = 1)
    )

plot_01 +
  theme_excel() +
  theme(plot.title = element_text(hjust = 1, colour = "#4E2A84"),
        plot.background =  element_rect(fill = "red4"),
        axis.title.x = element_text(colour = "#4E2A84"),
        axis.title.y = element_text(colour = "#4E2A84"),
        axis.ticks = element_line(colour = "#4E2A84"),
        legend.background = element_rect(fill = "yellow")
        ) 

plot_01 +
  theme_dark() +
  theme(aspect.ratio = 3/1,
        panel.background = element_rect(fill = "blue"),
        plot.title = element_text(hjust = 0.5),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank()) 

plot_01 +
  theme_tufte()

plot_01 +
  theme_solarized()

plot_01 +
  theme_economist()

plot_01 +
  theme_stata() +
  theme(axis.text.y = element_text(angle = 0))

Among, the plots above, I particularly like theme_stata, which is displayed on plot 8, because I feel like it portrays the data in a clear, organized manner.

Exercise 2

plot_01 +
  theme_void() +
   theme(plot.title = element_text(face = "bold", colour = "turquoise4"),
    plot.background = element_rect(colour = "mediumblue", size = 4,
                                   fill = "khaki4"),
    plot.margin = margin(l = 10, b = 15, r = 10, t = 10),
    panel.background = element_rect(fill = "wheat1"),
    panel.grid.major = element_line(color = "deeppink2", size = 2)
    )

Exercise 3

ggplot(cdc_small,aes(height, weight)) +
  geom_point(colour = "#4E2A84")

ggplot(cdc_small, aes(weight)) +
  geom_bar(colour = "#B6ACD1")