DJC
DJC

Reputation: 1611

Modify all but last element of y-axis label

I would like to modify all y-axis labels on a chart except for the last one. In the example below, think of y as a cumulative count of cylinders. Thus, a car can have 1+, 2+, etc., but it cannot have more than 8.

How would I modify the code below to add the "+" sign to all y-axis elements except the last one? I would like my labels to be (4+, 5+, 6+, 7+, 8)?

library(tidyverse)

mtcars %>% 
  ggplot(aes(x = mpg, y = cyl)) +
  scale_y_continuous(labels = function(x) paste0(x, "+"))

enter image description here

Upvotes: 0

Views: 25

Answers (1)

Nicolás Velasquez
Nicolás Velasquez

Reputation: 5898

Try: library(tidyverse)

mtcars %>% 
    ggplot(aes(x = mpg, y = cyl)) +
    scale_y_continuous(labels = function(x) c(paste0(x[-length(x)], "+"), max(x)))

enter image description here

Upvotes: 1

Related Questions