Coordinates vs. scales

Intermediate Data Visualization with ggplot2

Rick Scavetta

Founder, Scavetta Academy

Plot the raw data

ggplot(msleep, aes(bodywt, y = 1)) +
  geom_jitter() +
  scale_x_continuous(limits = c(0, 7000), 
                     breaks = seq(0, 7000, 1000))

Skewed data

Intermediate Data Visualization with ggplot2

Transform the raw data

ggplot(msleep, aes(log10(bodywt), y = 1)) +
  geom_jitter() +
  scale_x_continuous(limits = c(-3, 4),
                     breaks = -3:4)

Trans raw values

Intermediate Data Visualization with ggplot2

Add logtick annotation

ggplot(msleep, aes(log10(bodywt), y = 1)) +
  geom_jitter() +
  scale_x_continuous(limits = c(-3, 4),
                     breaks = -3:4) +
  annotation_logticks(sides = "b")

Trans raw values, with tick marks

Intermediate Data Visualization with ggplot2

Use scale_*_log10()

ggplot(msleep, aes(bodywt, y = 1)) +
  geom_jitter() +
  scale_x_log10(limits = c(1e-03, 1e+04))

scale trans

Intermediate Data Visualization with ggplot2

Compare direct transform and scale_*_log10() output

Trans raw values

scale trans

Intermediate Data Visualization with ggplot2

Use coord_trans()

ggplot(msleep, aes(bodywt, y = 1)) +
  geom_jitter() +
  coord_trans(x = "log10") 

coord trans

Intermediate Data Visualization with ggplot2

Compare scale_*_log10() and coord_trans() output

coord trans

scale trans

Intermediate Data Visualization with ggplot2

Adjusting labels

coord relabeled

scale relabeled

Intermediate Data Visualization with ggplot2

Time for exercises

Intermediate Data Visualization with ggplot2

Preparing Video For Download...