typestar

Coordinates in R

coord_flip, zooming without dropping data, and fixed aspect ratios.

library(ggplot2)
library(tibble)

runs <- tibble(x = 1:20, y = (1:20) * 1.5 + c(rep(0, 19), 40))

zoomed <- ggplot(runs, aes(x, y)) +
  geom_point() +
  geom_smooth(method = "lm", formula = y ~ x, se = FALSE) +
  coord_cartesian(ylim = c(0, 40))

clipped <- ggplot(runs, aes(x, y)) +
  geom_point() +
  geom_smooth(method = "lm", formula = y ~ x, se = FALSE) +
  ylim(0, 40)

square <- ggplot(runs, aes(x, y)) + geom_point() + coord_fixed(ratio = 1)

print(class(zoomed$coordinates)[1])
print(class(square$coordinates)[1])
ggsave(tempfile(fileext = ".png"), clipped, width = 4, height = 3, dpi = 100)

How it works

  1. coord_cartesian zooms; ylim drops the rows outside it.
  2. That difference changes what a smoother fits.
  3. coord_fixed keeps one unit equal on both axes.

Keywords and builtins used here

The run, in numbers

Lines
20
Characters to type
588
Tokens
201
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 115 seconds.

Type this snippet

Step 4 of 4 in Panels & scales, step 11 of 15 in ggplot2.

← Previous Next →