Anderson N. Barbosa
Anderson N. Barbosa

Reputation: 174

How to make the graph's axis skip fixed values (ggplot2)

Based on the example below, how to make the graph's y-axis show values skipping from 25 to 25?

Input Code:

library(ggplot2)

# scatter plot
graph <-ggplot(cars, aes(x = speed, y = dist)) + 
        geom_point() +
        ylim(0,150) +
        xlim(0,30) +
        labs(y = 'y-axis', x = "x-axis") +
        theme_classic()
graph

Output of Input code: enter image description here

I read several tutorials and I couldn't find this function. Below are some tutorial links I read:

https://ggplot2.tidyverse.org/reference/lims.html

https://ggplot2.tidyverse.org/reference/coord_cartesian.html

https://ggplot2.tidyverse.org/reference/lims.html

http://www.sthda.com/english/wiki/ggplot2-axis-scales-and-transformations

https://ggplot2-book.org/scales.html

[some others...]

Upvotes: 1

Views: 973

Answers (1)

Tim Assal
Tim Assal

Reputation: 687

I think you're looking for scale_y_continuous:

 # scatter plot
 graph <-ggplot(cars, aes(x = speed, y = dist)) + 
   geom_point() +
   #ylim(0,150) +
   scale_y_continuous(breaks = seq(0, 150, by=25), limits=c(0,150))+
   xlim(0,30) +
   labs(y = 'y-axis', x = "x-axis") +
   theme_classic()
 graph

Upvotes: 5

Related Questions