ggplot: Assign axis labels using variable pairs did not work

I wonder why the last case didn't work while the 1st and 2nd ones worked. I'm sure I'm missing something obvious here. Thank you for any help!


library(ggplot2)

p <- ggplot(mtcars, aes(wt, mpg))

lab1 <- as.character(seq(1, 5, 1))
lab2 <- c("One", "Two", "Three", "Four", "Five")

#1:works
p + geom_point(aes(size = qsec, fill = qsec), shape = 21, alpha = 0.7) +
  scale_fill_viridis_c(guide = "legend") +
  scale_size_continuous(range = c(1, 15)) +
  scale_x_continuous(breaks = seq(1, 5, 1),
                     labels = lab2)


#2:works too
p + geom_point(aes(size = qsec, fill = qsec), shape = 21, alpha = 0.7) +
  scale_fill_viridis_c(guide = "legend") +
  scale_size_continuous(range = c(1, 15)) +
  scale_x_continuous(breaks = seq(1, 5, 1),
                     labels = c("1" = "One", 
                                "2" = "Two",
                                "3" = "Three",
                                "4" = "Four",
                                "5" = "Five"))

#3:error
p + geom_point(aes(size = qsec, fill = qsec), shape = 21, alpha = 0.7) +
  scale_fill_viridis_c(guide = "legend") +
  scale_size_continuous(range = c(1, 15)) +
  scale_x_continuous(breaks = seq(1, 5, 1),
                     labels = c(lab1[1] = lab2[1], 
                                lab1[2] = lab2[2],
                                lab1[3] = lab2[3],
                                lab1[4] = lab2[4],
                                lab1[5] = lab2[5]))
Error: unexpected '=' in:
"  scale_x_continuous(breaks = seq(1, 5, 1),
                     labels = c(lab1[1] ="
>                                 lab1[2] = lab2[2],
Error: unexpected ',' in "                                lab1[2] = lab2[2],"
>                                 lab1[3] = lab2[3],
Error: unexpected ',' in "                                lab1[3] = lab2[3],"
>                                 lab1[4] = lab2[4],
Error: unexpected ',' in "                                lab1[4] = lab2[4],"
>                                 lab1[5] = lab2[5]))
Error: unexpected ')' in "                                lab1[5] = lab2[5])"

You don't need a named vector for labels. Your 2nd example works fine without the names in the vector.

p + geom_point(aes(size = qsec, fill = qsec), shape = 21, alpha = 0.7) +
  scale_fill_viridis_c(guide = "legend") +
  scale_size_continuous(range = c(1, 15)) +
  scale_x_continuous(breaks = seq(1, 5, 1),
                     labels = c("One", 
                                "Two",
                                "Three",
                                "Four",
                                "Five"))

The order of the breaks and labels is what creates the correct 'mapping' of new labels to the axis.

1 Like

What I wanted was to supply the exact label pairs. Maybe there were cases where labels could be mismatched because of the wrong orders? Thank you

Sure, you should use caution about the order, but using a named vector in labels doesn't give you any extra protection because the names are ignored in that named vector.

If you get rid of the breaks argument in #2, you get an error

p + geom_point(aes(size = qsec, fill = qsec), shape = 21, alpha = 0.7) +
  scale_fill_viridis_c(guide = "legend") +
  scale_size_continuous(range = c(1, 15)) +
  scale_x_continuous(labels = c("1" = "One", 
                                "2" = "Two",
                                "3" = "Three",
                                "4" = "Four",
                                "5" = "Five"))

Error in f(..., self = self) : Breaks and labels are different lengths
1 Like

I thought that ggplot2 would have option to prevent the mismatch between the actual labels and the axis values like this case

p + geom_point(aes(size = qsec, fill = qsec), shape = 21, alpha = 0.7) +
  scale_fill_viridis_c(guide = "legend") +
  scale_size_continuous(range = c(1, 15)) +
  scale_x_continuous(breaks = seq(1, 5, 1),
                     labels = c("Two", 
                                "One",
                                "Five",
                                "Four",
                                "Three"))

When you venture into the realm of custom labels, it is up to you to program defensively to prevent such an error.

2 Likes