It is important for the readability of your graph to label correctly the groups/categories. ggplot usually displays the names of the corresponding variables found in the data frame. But sometimes, it may be useful to rename the categories in the graph without affecting the original data frame.

Here we will see how to modify category labels with a simple example, a jitter plot where the 4 displayed categories were originally labeled Gr1, Gr2, Gr3 and Gr4 in the data frame.

Let’s start with the code for a reference plot. This plot is stored in the object baseplot so that we can reuse it throughout the whole tutorial:

baseplot <- ggplot(df, aes(category, values, color=category)) +
  geom_jitter()
baseplot



To rename the labels, we use scale_x_discrete(labels=) in the following way:

baseplot +
  scale_x_discrete(labels=c("Group A", "Group B", "Group C", "Group D"))

As you may see in the resulting plot above, the labels have been modified on the X-axis as expected, but not in the legend. Indeed scale_x_discrete() takes care of the X-axis only, but nothing else. To modify the legend as well, you need to add the matching function scale_color_discrete(labels=) to the code:

baseplot +
  scale_x_discrete(labels=c("Group A", "Group B", "Group C", "Group D")) +
  scale_color_discrete(labels=c("Group A", "Group B", "Group C", "Group D"))

Note the we have used scale_color_discrete() here because the code for our plot originally used color=category to group and color the data. In other cases where groups are colored with fill=, we would use scale_fill_discrete().