A little bit of setup

install.packages("gapminder")

Basics from last time

ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point(aes(color = Species))

Activity 1


Creating a histogram

ggplot(iris, aes(x = Petal.Length)) +
  geom_histogram(aes(fill = Species), color = "black")

Messing with “position”

ggplot(iris, aes(x = Petal.Length)) +
  geom_histogram(aes(fill = Species), color = "black", position = "dodge")
ggplot(iris, aes(x = Petal.Length)) +
  geom_histogram(aes(fill = Species), color = "black", position = "identity")
ggplot(iris, aes(x = Petal.Length)) +
  geom_histogram(aes(fill = Species), 
                 color = "black", 
                 position = "identity", 
                 alpha = 0.7)

Messing with “bins”

ggplot(iris, aes(x = Petal.Length)) +
  geom_histogram(aes(fill = Species), 
                 color = "black", 
                 position = "identity", 
                 alpha = 0.7,
                 bins = 15)
ggplot(iris, aes(x = Petal.Length)) +
  geom_histogram(aes(fill = Species), 
                 color = "black", 
                 position = "identity", 
                 alpha = 0.7,
                 bins = 50)
ggplot(iris, aes(x = Petal.Length)) +
  geom_histogram(aes(fill = Species), 
                 color = "black", 
                 position = "identity", 
                 alpha = 0.7,
                 bins = 25)

Introducing facets

ggplot(iris, aes(x = Petal.Length)) +
  geom_histogram(aes(fill = Species), 
                 color = "black", 
                 position = "identity", 
                 alpha = 0.7,
                 bins = 25) +
  facet_grid(Species~.)

Activity 2


ggplot(diamonds, aes(x = price)) + 
  geom_histogram(aes(fill = cut), color = "black") +
  facet_grid(cut~.)

Faceting by multiple variables

ggplot(diamonds, aes(x = price)) + 
  geom_histogram(aes(fill = cut), color = "black") +
  facet_grid(cut~clarity)

Adjusting scales

ggplot(diamonds, aes(x = price)) + 
  geom_histogram(aes(fill = cut), color = "black") +
  facet_grid(cut~., scales = "free_y")
ggplot(diamonds, aes(x = price)) + 
  geom_histogram(aes(fill = cut), color = "black") +
  facet_grid(cut~., scales = "free_y") +
  scale_x_log10()

Introducing other geometries

frequency polygon

ggplot(diamonds, aes(x = price)) + 
  geom_histogram(aes(fill = cut), color = "black") +
  geom_freqpoly() +
  facet_grid(cut~., scales = "free_y") +
  scale_x_log10()

KDE

Computes and draws kernel density estimate, which is a smoothed version of the histogram. This is a useful alternative to the histogram for continuous data that comes from an underlying smooth distribution.

ggplot(diamonds, aes(x = price)) + 
  geom_density(aes(fill = cut)) +
  facet_grid(cut~.) +
  scale_x_log10()
ggplot(diamonds, aes(x = price)) + 
  geom_density(aes(fill = cut), alpha = 0.5) +
  scale_x_log10()
ggplot(diamonds, aes(x = price)) + 
  geom_density(aes(fill = cut), alpha = 0.5, adjust = 2) +
  scale_x_log10()
ggplot(diamonds, aes(x = price)) + 
  geom_density(aes(fill = cut), alpha = 0.5, adjust = .5) +
  scale_x_log10()

Two-dimensional distributions

ggplot(diamonds, aes(x = carat, y = price)) +
  geom_bin2d()

Improving the color with scales

ggplot(diamonds, aes(x = carat, y = price)) +
  geom_bin2d() +
  scale_fill_viridis_c() 
ggplot(diamonds, aes(x = carat, y = price)) +
  geom_bin2d() +
  scale_fill_viridis_c(trans = "log")
ggplot(diamonds, aes(x = carat, y = price)) +
  geom_bin2d() +
  scale_fill_viridis_c(trans = "log", breaks = c(5, 50, 500, 5000)) 

Activity 3


ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_bin2d() +
  scale_x_log10()
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_bin2d() +
  scale_x_log10() +
  scale_fill_viridis_c()
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_bin2d() +
  scale_x_log10() +
  scale_fill_viridis_c() +
  facet_wrap(~continent)

Showing 2D Density

ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_bin2d() +
  geom_density2d() +
  scale_x_log10() +
  scale_fill_viridis_c()
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_density2d(aes(color = ..level..)) +
  scale_x_log10()
ggplot(gapminder, aes(x = gdpPercap, y = lifeExp)) +
  geom_density2d_filled() +
  scale_x_log10()

Violin Plots

ggplot(gapminder, aes(x = year, y = lifeExp)) +
  geom_violin()
ggplot(gapminder, aes(x = factor(year), y = lifeExp)) +
  geom_violin()
ggplot(gapminder, aes(x = factor(year), y = lifeExp)) +
  geom_boxplot()
ggplot(gapminder, aes(x = factor(year), y = lifeExp)) +
  geom_boxplot() +
  geom_violin(fill = NA)

Activity 4


ggplot(gapminder, aes(x = factor(year), y = lifeExp, color = continent)) +
  geom_boxplot() +
  geom_violin(fill = NA) +
  facet_grid(continent~.)

Labels

ggplot(gapminder, aes(x = factor(year), y = lifeExp, color = continent)) +
  geom_boxplot() +
  geom_violin(fill = NA) +
  facet_grid(continent~.) +
  labs(
    x = "",
    y = "Life Expectancy",
    color = "Continent",
    title = "Distribution of Life Expectancy",
    subtitle = "1952-2007"
  )