Skip to content
if(!require('ggh4x')) {
    install.packages('ggh4x')
    library('ggh4x')
}
# Make a plot
p <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  theme(axis.line = element_line(colour = "black"))

# Setting the default truncated axis
p + guides(x = "axis_truncated")

# Truncating in data units
p + guides(x = guide_axis_truncated(
  trunc_lower = 2.5, trunc_upper = 4.5
))

# Truncate by setting units
p + guides(x = guide_axis_truncated(
  trunc_lower = unit(0.1, "npc"),
  trunc_upper = unit(0.9, "npc")
))

# Truncating with functions
p + guides(x = guide_axis_truncated(
  trunc_lower = function(x) {x - 0.2},
  trunc_upper = function(x) {x + 0.2}
))

# Recolouring the axes outside the theme
p + guides(x = guide_axis_colour(colour = "red"),
           y = guide_axis_colour(colour = "blue"))