4.18 Customization of Labels and Titles

In Section 4.11, we discussed the customization of the x and y axes in ggplot. Now, we will introduce other types of customization, and the concept of themes.

Let’s first review the scatterplot we created between liv_area and sale_price and save it into as an object g.

library(r02pro)
library(tidyverse)
g <- ggplot(data = sahp, 
            mapping = aes(x = liv_area, y = sale_price)) + 
  geom_point()

a. customize x and y labels and title

By default, the x and y labels are the variables names, and there is no title for the plot. You can customize the x and y labels using the xlab() and ylab() function, and add a title with the ggtitle() function.

g1 <- g + 
  xlab("Living Area") + 
  ylab("Sale Price") + 
  ggtitle("Price vs. Area")
g1 

b. customize the font of the x and y breaks

In addition, you can further customize the font of the x and y breaks using the theme() function with the argument axis.text.

g1 + theme(axis.text = element_text(size = 25, color = "red"))

c. customize the font of labels

To customize the font, you can use axis.title argument to change the size, color, and face of the labels.

g1 + theme(axis.title = element_text(size = 18, 
                                     color = "red", 
                                     face = "italic"))

d. customize the font of the title

Similarly, you can use the plot.title argument to customize the font of the title.

g1 + theme(plot.title = element_text(size = 24, 
                                     color = "magenta", 
                                     face = "bold"))

e. center the title

Sometimes, we may want to center the title. We can achieve this by setting the hjust parameter.

g1 + theme(plot.title = element_text(size = 24, 
                                     color = "magenta", 
                                     face = "bold",
                                     hjust = 0.5)) 

f. mix

Apparently, you are free to mix all the different customizations. Let’s see an example as below.

g1 + theme(axis.title = element_text(size = 18, 
                                     color = "red", 
                                     face = "italic"), 
           axis.text = element_text(size = 12, 
                                    color = "blue"), 
           plot.title = element_text(size = 24, 
                                     color = "magenta", 
                                     face = "bold", 
                                     hjust = 0.5))

g. save as a theme

As you can see from the code, the code gets complicated if we want to customize many things at the same time. To save time, you can actually save the desired into an R object and reuse it later.

mytheme <- theme(axis.title = element_text(size = 18, 
                                           color = "red", 
                                           face = "italic"), 
                 axis.text = element_text(size = 12, 
                                          color = "blue"), 
                 plot.title = element_text(size = 24, 
                                           color = "magenta", 
                                           face = "bold", 
                                           hjust = 0.5))

Then, we can generate the same plot with mytheme using

g1 + mytheme

Similarly, you can add another layer of smoothline fit and apply mytheme.

g1 + 
  geom_smooth() + 
  mytheme

You may notice that title is not accurate. So, we want to change the title to reflect the addition of the smoothline fit. Let’s simply add another ggtitle() function to overwrite the existing one in the theme.

g1 + 
  geom_smooth() + 
  mytheme + 
  ggtitle("Scatterplot + smoothline")