Plotting with ggplot2

library(tidyverse)

Practice with irises

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

Start with just data and coordinates

ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) 

Add in points to show data, along with color and a linear model

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

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

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

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

Activity 1


More about Color

This applies the color to everything - global ggplot call carries to all geom_*

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

If we want to color the points based on the species: This doesn’t work.

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

This does! Using the data to color the data requires the aes() function

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

This changes all of the points to a single color

ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point(color = "blue") +
  geom_smooth(method = lm)

This creates a color based on one value, “blue.” It doesn’t involve the color “blue” at all.

ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point(aes(color = "blue")) +
  geom_smooth(method = lm)

Colors can be continuous

ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point(aes(color = Sepal.Length)) +
  geom_smooth(method = lm)

Other options

ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point(aes(shape = Species)) +
  geom_smooth(method = lm)
ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point(aes(size = Species)) +
  geom_smooth(method = lm)
ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point(aes(size = Sepal.Length)) +
  geom_smooth(method = lm)
ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point(aes(alpha = Sepal.Length)) +
  geom_smooth(method = lm)

https://ggplot2.tidyverse.org/reference/geom_point.html


Activity 2



Activity 3


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

Activity 4


ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point(aes(color = Species)) +
  geom_smooth(method = lm) +
  geom_smooth(method = loess, color = "orange")

Plotting in Layers

ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_smooth(method = lm) +
  geom_smooth(method = loess, color = "orange") +
  geom_point(aes(color = Species))
ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_smooth(method = lm) +
  geom_point(aes(color = Species)) +
  geom_smooth(method = loess, color = "orange")

Using a custom formula

ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point() +
  geom_smooth(method = loess)
ggplot(iris, aes(x = Petal.Length, y = Petal.Width)) +
  geom_point() +
  geom_smooth(method = loess) +
  geom_smooth(method = loess, formula = y ~ cos(x), color = "orange")

Activity 5


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

Setting CI Level

Get rid of SE/CI altogether

ggplot(diamonds, aes(x = carat, y = price)) +
  geom_point() +
  geom_smooth(se = FALSE)

Change CI (.95 by default)

ggplot(diamonds, aes(x = carat, y = price)) +
  geom_point() +
  geom_smooth(level = .99)
ggplot(diamonds, aes(x = carat, y = price)) +
  geom_point() +
  geom_smooth(level = .99, aes(fill = "99%")) +
  geom_smooth(level = .95, aes(fill = "95%")) +
  geom_smooth(level = .80, aes(fill = "80%"))