03. Coordinate Systems

Chapter 2

The meaning of position aesthetics and how they produce a 2D position on the plot depend on the coordinate system. This is also responsible for drawing the axes and panel backgrounds (grid lines, etc.). As with scales, Cartesian coordinates are applied by default unless stated otherwise, which preserves the common meaning of x and y.

Iñaki Ucar (Department of Statistics)
2022-10-05

Source: _tutorials/03/03.Rmd

Cartesian coordinates

Most of the plots we know and love use Cartesian coordinates, where the position of an element is given by orthogonal distances, x and y, to an origin.

library(ggplot2)

ggplot(mpg) +
  aes(displ, hwy) +
  geom_point(aes(color=class))

Even when one of the axis is a categorical variable, the same concept of “distance to an origin” applies, because internally those categories are mapped to positions 1, 2, 3…

ggplot(mpg) +
  aes(displ, class) +
  geom_col() +
  geom_text(aes(label=class), data.frame(
    displ = 200, class = c(1, 2, 3, 4, 5, 6, 7)
  ))

As with scales, coord_cartesian() is the default, and it does not need to be explicitly specified to produce a plot. However, this function has some useful options that makes some things easier. Particularly, you may recall from the previous tutorial that zooming in by setting the limits of a scale by default censors points that are out of bounds (meaning that they are marked as missing with a NA). For scales, an additional argument oob must be specified in order to keep those points (e.g. for smoothing calculations). In contrast, coord_cartesian() provides limits that do not censor data. Therefore, the following plots are equivalent:

p <- ggplot(mpg) +
  aes(displ, hwy) +
  geom_point(aes(color=class)) +
  geom_smooth()

p +
  scale_x_continuous(limits=c(3, 4), oob=scales::oob_keep) +
  scale_y_continuous(limits=c(20, 30), oob=scales::oob_keep)

p +
  coord_cartesian(xlim=c(3, 4), ylim=c(20, 30))

Checkpoint 1

  1. Function coord_flip() comes in handy to exchange the x and y axes. Take the first example and add geom_smooth(method="lm"). What is the difference between adding coord_flip() and directly exchanging the x and y mapping in the aes()? Why?

  2. Take the first example and add coord_fixed(). Then use variable cty instead of displ to compare. What does this function do? What could be a good use case for this?

How to make a pie

A pie chart is just a single stacked bar in polar coordinates. For instance, take this one:

p <- ggplot(mpg) +
  aes("cars", fill=class) +
  geom_bar()
p

Then, we just need to fold it using y (the count produced by geom_bar) as the angle:

p + coord_polar(theta="y")

So much effort for such a meaningless visualization. :)

Transformations

Transformations can be performed at two levels: at the scale level, via the trans argument to the scale functions (or the dedicated scales such as scale_x_log10()), or at the coordinate system level, via coord_trans(). The only difference is that the first ones occur before any statistics are computed, while the second ones occur after those.

Checkpoint 2

  1. Take the first example and add geom_smooth(method="lm"). What is the difference between adding scale_x_log10() and adding coord_trans(x="log10")? Why?

Maps

Finally, an important family of coordinate systems are map projections. Enter the fascinating world of how to map a sphere to a plane.

p <- ggplot(map_data("world")) +
  aes(long, lat, group=group) +
  geom_path()

p # No projection specified, simply long, lat as x, y
p + coord_map(xlim=c(-180, 180)) # Mercator projection by default
p + coord_map("orthographic", xlim=c(-180, 180))
p + coord_map("sinusoidal", xlim=c(-180, 180))

See ?mapproj::mapproject for more info about projections.

Checkpoint 3

  1. Click “Zoom” in the plot panel for each of the four plots proposed above. What happens if you stretch and shrink the width of the plot panel in each case? Why?

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Ucar (2022, Oct. 5). Data visualization | MSc CSS: 03. Coordinate Systems. Retrieved from https://csslab.uc3m.es/dataviz/tutorials/03/

BibTeX citation

@misc{ucar202203.,
  author = {Ucar, Iñaki},
  title = {Data visualization | MSc CSS: 03. Coordinate Systems},
  url = {https://csslab.uc3m.es/dataviz/tutorials/03/},
  year = {2022}
}