How to Extract Specific Elements from a Date in R
How to Use a Function in R
How to Enter Data into the R Text Editor

How to Work with Scales in a ggplot2 in R

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().

image0.jpg
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Work with Files and Folders in R
How to Recognize and Fix List Errors in R
How to Change Plot Options in R
How to Traverse Data with Apply Functions in R
How to Make a Minimal Reproducible Example to Get Help with R
Advertisement

Inside Dummies.com