Labeling Axes
So far all our axis labels have been the names of our variables, but often that does not look very professional, so it’s easy to change them. We can add a labs() command and then specify the axis labels within that. If we have a color or fill variable, we can also use that to change the legend title.
Let’s start by changing the x and y axis labels on the histogram we made. To do so, I just add a + and another line of code and specify the labels I want within the labs() function.
We can also add a title using this format. Fill in the title name with whatever you want to appear above the graph.
Note that because x, y, and title are all within the labs() function you don’t need to add a plus at the end of the line. Really the way R sees that code is like the following, but for us it’s nicer to look at if we put things on separate lines. Anytime you use a comma, you can also make a new line if you want.
ggplot(data = penguins, aes(x = body_mass_g, fill = species)) +
geom_histogram() +
labs(x = "Weight in Grams", y = "Total Number of Penguins", title = "______")We can also change the legend title with labs by adding how we want fill to be labeled.