Scales, Axes & Coordinate systems
Within ggplot there are a bunch of
scales that control how a plot maps data values to the visual values of an aesthetic. To change the mapping, add a custom scale. These scales look something like (the ggplot2-cheatsheet is helpful as well):
scale_x_continuous; change your x-axis when your x-variable is continuous (
scale_x_discrete; change your x-axis when your x-variable is categorical (
scale_*_identity; use data values as visual values (e.g.,
scale_colour_identitywhen your variable in
colour = variablecontains the names of colours). * can be
scale_*_manual(values = c()); map discrete values to manually chosen visual values (e.g.,
scale_fill_manual(values = c("4" = "blue", "r" = "red", "f" = "black"))* can be
Let’s see what
scale_y_continuous can do.
We’ll change features from this graph
Zooming in and out with limits
For instance, often we want to zoom in or out. Let’s zoom in on the y-axis and zoom out on the x-axis.
It works! Importantly, though, we must realise that there are many datapoints not visualized currently, as the warning message in R already suggests.
Using the limits in this way can be a bit dangerous. Let’s see why in an example with mean + error plots:
Now let’s see what happens if we give limits that exclude some points
This is certainly not what we had in mind! We have changed our underlying data; all 8 datapoints that were larger than 25 were removed and then the data were plotted. Compare the bar and error for Honda and Volkswagen in the two graphs.
An alternative and preferred way of doing this, is doing it in the following way (also see
ggplot adds some space to the bottom and the side of your axes. If you do not want this, use the
expand() function. Compare the following two graphs:
But do note that you are risking throwing away datapoints again if you specify limits that exlude particular datapoints.
There are also functions that allow you to transform the scales of the axes. For instance, you can think of having a logarithmic scale, or a square-root-transformed scale.
Look closely at the x-axis. We have transformed the x-axis. This works much better for variables that have more skewed distributions.
ggplot can also do something nifty with the labels of the axes. Let’s look at four examples to get some impressions (after we install a necessary package):
Cool that we can do this but obviously doesn’t make much sense.
Also nice that we can do this, but obviously not appropriate.
When your x-axis is discrete (or categorical), you can use
scale_x_discrete to change all sorts of relevant stuff. Let’s see how that works for the following graph:
Identity uses that values that are specified within a variable. Let’s create a column with colour names for the different types of
We can now use this variable:
Fill works too:
We could have created the above graph also with
Again, better to be a bit specific, to be more sure that the colour is added to the right group:
Let’s try to control the order and labels of the legend!
ggplot has several coordinate systems, you’ll mostly use the default
coord_cartesian, but also
coord_flip might come in handy.
coord_caresian we can zoom:
coord_fixed forces a specified ratio between the physical representation of data units on the axes. This is very useful when the variables on the x-axis and on the y-axis have the same units: