![](https://assets.isu.pub/document-structure/230101182555-1179cabb6e9ce02cd5c0b77a54fb27cf/v1/1401fc4ff9f3c18939693cd26f3c29df.jpeg?width=720&quality=85%2C50)
1 minute read
Exercises����������������������������������������������������������������������������������������������������������������������
Chapter 4 ■ Visualizing Data
Figure 4-24. Iris data plotted with a factor on the x-axis
Advertisement
Since Species is a factor, the x-axis will be discrete, and you can show the data as a boxplot and the individual data points using the jitter geometry. If you want to modify the x-axis, you need to use scale_x_ discrete() instead of scale_x_continuous().
You could, for instance, use this to modify the labels on the axis to put the species in capital letters:
iris %>% ggplot(aes(x = Species, y = Petal.Length)) + geom_boxplot() + geom_jitter(width = 0.1, height = 0.1) + scale_x_discrete(labels = c("setosa" = "Setosa", "versicolor" = "Versicolor", "virginica" = "Virginica"))
You just provide a map from the data levels to labels. There is more than one way to set the labels, but this is by far the easiest.
Scales are also used to control colors. You use the various scale_color_ functions to control the color of lines and points, and you use the scale_fill_ functions to control the color of filled areas.
102