Skip to content

Could the theme support axis lines and ticks being _not_ on top of geoms? #6456

Open
@davidhodge931

Description

@davidhodge931

There are situations where it would nice to be able to easily make the the axis line/ticks not be on top of geoms.

I noticed the theme argument has a panel.ontop argument. So I wondered if you guys would consider adding equivalent theme arguments for the axis.

Something like theme(axis.ontop = TRUE, axis.ontop.x = TRUE, axis.ontop.y = TRUE).

I suppose for consistency with the existing theme grammar, these axis arguments should apply to the axis text too.

Thanks for your consideration, and the great ggplot2 dev work!

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
library(palmerpenguins)
#> 
#> Attaching package: 'palmerpenguins'
#> The following objects are masked from 'package:datasets':
#> 
#>     penguins, penguins_raw

d <- penguins |>
  add_row(
    flipper_length_mm = 190,
    body_mass_g = 2500,
    species = "Adelie",
  ) |> 
  add_row(
    flipper_length_mm = 165,
    body_mass_g = 3000,
    species = "Adelie",
  )

p <- d |>
  ggplot() +
  geom_point(aes(x = flipper_length_mm, y = body_mass_g),
             colour = "steelblue", size = 3) +
  theme_classic() +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  coord_cartesian(clip = "off")

p
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

#so something like this to make the axis line/ticks not ontop
# p +
#   theme(axis.ontop = FALSE) 
# 
# p +
#   theme(axis.ontop.x = FALSE) +
#   theme(axis.ontop.y = FALSE) 

Created on 2025-05-14 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions