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.
Source: _tutorials/03/03.Rmd
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.
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…
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))
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?
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?
A pie chart is just a single stacked bar in polar coordinates. For instance, take this one:
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 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.
geom_smooth(method="lm")
. What is the difference between adding scale_x_log10()
and adding coord_trans(x="log10")
? Why?Finally, an important family of coordinate systems are map projections. Enter the fascinating world of how to map a sphere to a plane.
See ?mapproj::mapproject
for more info about projections.
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 ...".
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} }