Using the mycars data, create the following basic plot.
Code
ggplot(mycars, aes(x = mpg, y = disp, color = cyl)) +geom_point(size =4) +scale_color_manual(values =c(`4`="purple", `6`="orange", `8`="forestgreen"), name ="Number of\ncylinders") +labs(x ="Miles/gallon", y ="Displacement") +theme_bw(base_size =13)
Add labels to points
The first extension we will make to the basic plot is to add a label to each point. We will use the model column. Note that the labels are added in such a way that they do not overlap (more precisely, different labels are designed to repel each other to avoid overlaps whenever possible).
Code
ggplot(mycars, aes(x = mpg, y = disp, color = cyl, label = model)) +geom_point(size =4) +geom_text_repel(min.segment.length =0, max.overlaps =Inf) +scale_color_manual(values =c(`4`="purple", `6`="orange", `8`="forestgreen"), name ="Number of\ncylinders") +labs(x ="Miles/gallon", y ="Displacement") +theme_bw(base_size =13)
Format axis labels using markdown
Next, we turn our attention to the axis labels. The ggtext package provides functionality to format the axes (or other text elements) using markdown. Use this functionality to add formatting (italics and bold) to the x-axis title, and add a unit to the y-axis title (here, it is helpful to note that HTML code can be interpreted by markdown).
Code
ggplot(mycars, aes(x = mpg, y = disp, color = cyl, label = model)) +geom_point(size =4) +geom_text_repel(min.segment.length =0, max.overlaps =Inf) +scale_color_manual(values =c(`4`="purple", `6`="orange", `8`="forestgreen"), name ="Number of\ncylinders") +labs(x ="_Miles/gallon_ (**mpg**)", y ="Displacement (in<sup>3</sup>)") +theme_bw(base_size =13) +theme(axis.title.x.bottom =element_markdown(),axis.title.y.left =element_markdown())
Add special characters to labels
Next, we will change the axis titles completely (the x-axis title to \(\alpha\), and the y-axis title to \(\beta^4\)). Note that these labels don’t necessarily make much sense in this case, but they serve as a useful example for general special characters. We can achieve our goal in several ways - for example, using the R expression() function:
Code
## Using expressionggplot(mycars, aes(x = mpg, y = disp, color = cyl, label = model)) +geom_point(size =4) +geom_text_repel(min.segment.length =0, max.overlaps =Inf) +scale_color_manual(values =c(`4`="purple", `6`="orange", `8`="forestgreen"), name ="Number of\ncylinders") +labs(x =expression(~ alpha), y =expression(~ beta ^4)) +theme_bw(base_size =13)
or by using Unicode characters characters (note that these may not be properly displayed unless the plot backend is set to Cairo):
Code
## Using Unicodeggplot(mycars, aes(x = mpg, y = disp, color = cyl, label = model)) +geom_point(size =4) +geom_text_repel(min.segment.length =0, max.overlaps =Inf) +scale_color_manual(values =c(`4`="purple", `6`="orange", `8`="forestgreen"), name ="Number of\ncylinders") +labs(x ="\U03B1", y ="\U03B2\U2074") +theme_bw(base_size =13)
Resources
Some useful examples of using expression and bquote to add special characters to plot labels. Also see ?plotmath.