How to Work with Scales in a ggplot2 in R - dummies

How to Work with Scales in a ggplot2 in R

By Andrie de Vries, Joris Meys

In ggplot2 in R, scales control the way your data gets mapped to your geom. In this way, your data is mapped to something you can see (for example, lines, points, colors, position, or shapes).

The ggplot2 package is extremely good at selecting sensible default values for your scales. In most cases, you don’t have to do much to customize your scales. However, ggplot2 has a wide range of very sophisticated functions and settings to give you fine-grained control over your scale behavior and appearance.

In the following example, you map the column mtcars$cyl to both the shape and color of the points. This creates two separate, but overlapping, scales: One scale controls shape, while the second scale controls the color of the points:

> ggplot(mtcars, aes(x=hp, y=mpg)) +
+   geom_point(aes(shape=factor(cyl), colour=factor(cyl)))

The name of a scale defaults to the name of the variable that gets mapped to it. In this case, you map factor(cyl) to the scale. To change the appearance of a scale, you need to add a scale function to your plot.

The specific scale function you use is dependent on the type of scale, but in this case, you have a shape scale with discrete values, so you use the scale_shape_discrete() function. You also have a color scale with discrete value, so you can control that with scale_colour_discrete().

To change the name that appears in the legend of the plot, you need to specify the argument name to these scales. For example, change the name of the legend to “Cylinders” by setting the argument name=”Cylinders”:

> ggplot(mtcars, aes(x=hp, y=mpg)) +
+   geom_point(aes(shape=factor(cyl), colour=factor(cyl))) +
+   scale_shape_discrete(name="Cylinders") +
+   scale_colour_discrete(name="Cylinders")

Similarly, to change the x-axis scale, you would use scale_x_continuous().