Home > database >  Why doesn't gplot2::labs() overwrite/update the name argument of scales functions (i.e. ggplot2
Why doesn't gplot2::labs() overwrite/update the name argument of scales functions (i.e. ggplot2

Time:05-27

Context: sometimes I need to change only the labels of a plot (e.g. to change the language) but I do not want to make the whole plot again as the code might be very long in some cases.

Problem: they are at least two ways to define the aesthetics labels:

  1. With the function ggpot2::labs(x = ..., y = ...) and,
  2. With the name argument of the scales functions ggplots::scale_*_*(name = ...)

According to the example below, it seems that, for a plot p, p labs() can overwrite labs defined with the labs() function but not labs defined with scale_*_*(name = ...).

Question: How to avoid this behavior? Is it a bug or am I doing something wrong?

Example:

This is working as intended:

library(ggplot2)
# This is working as expected
p1 <- ggplot(data = iris, aes(x = Sepal.Width, y = Petal.Width))  
  scale_x_continuous()  
  scale_y_continuous()  
  labs(x = "A name",
       y = "Another name")
p1
# trying to change the labs without making the plot again
p1   labs(
  x = "The new x title",
  y = "The new y title"
)

While this does not:

library(ggplot2)
# This is not working
p2 <- ggplot(data = iris, aes(x = Sepal.Width, y = Petal.Width))  
  scale_x_continuous(name = "A name")  
  scale_y_continuous(name = "Another name")
p2
p2   labs(
  x = "The new x title",
  y = "The nex y title"
)

CodePudding user response:

This is just explaining what is happening. There are multiple ways to set labels in a plot, which take different priorities. Here are the priorities from highest to lowest.

(0. In some scale extensions, the make_title() method can be overwritten. This generally doesn't apply to the vast majority of scales though. I know of exactly 0 such scales, but it is a theoretical possibility.)

  1. The guide title.
  2. The scale name.
  3. The labs() function.
  4. The captured expression in aes().

Most of the disambiguation happens in this line of ggplot2 source code, where priorities 1-3 are resolved. What the labs() function in essence does is to override the automatically generated label from the captured expression, thereby giving (3) precedence over (4).

Try commenting out some parts of the plot below to double check yourself.

library(ggplot2)

df <- iris
names(df)[1] <- "Last Priority"

ggplot(df, aes(`Last Priority`, Sepal.Width))  
  scale_x_continuous(
    name  = "Second Priority",
    guide = guide_axis(title = "First priority")
  )  
  labs(x = "Third Priority")

So your best bet to override the title for the x-axis is to use:

plot   guides(x = guide_axis(title = "My new title"))

Since the axis guide is typically guide_axis() or guide_none(), you have a decent chance of this working most of the time (except when the plot doesn't have an axis). Moreover, if you have a function generating a plot with a predefined scale for x, this should be fine (unless they set specific options in the guide).

  • Related