Modifying Aesthetics

Introduction to Data Visualization with ggplot2

Rick Scavetta

Founder, Scavetta Academy

Positions

Adjustment for overlapping

  • identity
  • dodge
  • stack
  • fill
  • jitter
  • jitterdodge
  • nudge
Introduction to Data Visualization with ggplot2

position = "identity" (default)

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

Introduction to Data Visualization with ggplot2

position = "identity" (default)

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "identity")

Introduction to Data Visualization with ggplot2

position = "jitter"

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter")

Introduction to Data Visualization with ggplot2

position_jitter()

posn_j <- position_jitter(0.1)

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 col = Species)) +
  geom_point(position = posn_j)

Introduction to Data Visualization with ggplot2

position_jitter()

posn_j <- position_jitter(0.1, 
                          seed = 136)

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = posn_j)
  • Set arguments for the position
  • Consistency across plots & layers

Introduction to Data Visualization with ggplot2

Scale functions

  • scale_x_*()
  • scale_y_*()
  • scale_color_*()
    • Also scale_colour_*()
  • scale_fill_*()
  • scale_shape_*()
  • scale_linetype_*()
  • scale_size_*()
Introduction to Data Visualization with ggplot2

Scale functions

  • scale_x_continuous()
  • scale_y_*()
  • scale_color_discrete()
    • Alternatively, scale_colour_*()
  • scale_fill_*()
  • scale_shape_*()
  • scale_linetype_*()
  • scale_size_*()
Introduction to Data Visualization with ggplot2

scale_*_*()

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length") +
  scale_color_discrete("Species")

Introduction to Data Visualization with ggplot2

The limits argument

ggplot(iris, aes(x = Sepal.Length, 
                  y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length", 
                     limits = c(2,8)) +
  scale_color_discrete("Species")

Introduction to Data Visualization with ggplot2

The breaks argument

ggplot(iris, aes(x = Sepal.Length, 
                  y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length", 
                     limits = c(2, 8), 
                     breaks = seq(2, 8, 3)) +
  scale_color_discrete("Species")

Introduction to Data Visualization with ggplot2

The expand argument

ggplot(iris, aes(x = Sepal.Length, 
                  y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length", 
                     limits = c(2, 8), 
                     breaks = seq(2, 8, 3),
                     expand = c(0, 0)) +
  scale_color_discrete("Species")

Introduction to Data Visualization with ggplot2

The labels argument

ggplot(iris, aes(x = Sepal.Length, 
                  y = Sepal.Width, 
                 color = Species)) + 
  geom_point(position = "jitter") +
  scale_x_continuous("Sepal Length", 
                     limits = c(2, 8), 
                     breaks = seq(2, 8, 3),
                     expand = c(0, 0), 
                     labels = c("Setosa", 
                               "Versicolor", 
                               "Virginica")) +
  scale_color_discrete("Species")

Introduction to Data Visualization with ggplot2

labs()

ggplot(iris, aes(x = Sepal.Length, 
                 y = Sepal.Width, 
                 color = Species)) +
  geom_point(position = "jitter") +
  labs(x = "Sepal Length", 
       y = "Sepal Width", 
       color = "Species")

Introduction to Data Visualization with ggplot2

Let's try it out!

Introduction to Data Visualization with ggplot2

Preparing Video For Download...