Search code examples
rggplot2line-plotx-axis

Want to change the values of x axis, but not change the graph


`

#example df
serial_numbers <- 1:5
car_names <- c("Toyota Camry", "Honda Civic", "Ford Mustang", "Chevrolet Cruze", "BMW X5")
tank_capacity <- c(6000, 5500, 7000, 5000, 6500)
df <- data.frame("Serial Number" = serial_numbers, "Car Name" = car_names, "Fuel Tank Capacity (millimeters)" = tank_capacity) 

library(ggplot2)

plt <-ggplot(df, aes(serial_numbers, tank_capacity))+
  geom_line()
 

I want to put 35, 36, 37, 38, 39, instead of 1, 2, 3, 4, 5. But want everything to be as it is, including the plot


Solution

  • If you don't want to change your data beforehand, You could change your labels using scale_x_continuous like this:

    library(ggplot2)
    
    plt <-ggplot(df, aes(serial_numbers, tank_capacity))+
      geom_line() +
      scale_x_continuous(labels = c(35:39))
    plt
    

    Created on 2023-07-02 with reprex v2.0.2