How to Set GGPlot Breaks



How to Set GGPlot Breaks

 

In this article, you will learn how to set ggplot breaks for continuous x and y axes. The function scale_x_continuous() and scale_y_continuous() can be used for ggplot axis breaks settings.

Related Book

GGPlot2 Essentials for Great Data Visualization in R

Prerequisites

Load the ggplot2 package and set the theme function theme_classic() as the default theme:

library(ggplot2)
theme_set(
  theme_classic() + 
    theme(legend.position = "top")
  )

Basic scatter plots

sp <- ggplot(cars, aes(x = speed, y = dist)) + 
  geom_point()
sp

Change axis ticks break interval

# Break y axis by a specified value
# a tick mark is shown on every 50
sp + scale_y_continuous(breaks=seq(0, 150, by = 50))

# Tick marks can be spaced randomly
sp + scale_y_continuous(breaks=c(0, 50, 65, 75, 150))

Remove breaks

# Remove y tick mark labels and grid lines
sp + scale_y_continuous(breaks=NULL)



Version: Français





No Comments

Give a comment

Want to post an issue with R? If yes, please make sure you have read this: How to Include Reproducible R Script Examples in Datanovia Comments