Skip to content

coord_fixed() option to force equally-spaced breaks #2888

Closed as not planned
Closed as not planned

Description

@corybrunson

In most cases that call for coord_fixed(), it is also helpful to have gridlines that are equally spaced along the horizontal and vertical axes, so that the background grid consists of squares. This can be done manually by choosing the same regular breaks (see below), but

  • this is inconvenient, especially when generating many plots at different scales, and
  • the internal break_info() functions that automatically produce elegant choices of breaks cannot be used.

It seems natural to have an option, e.g. coord_fixed(breaks = "square"), that generates elegant breaks that (a) occur at the same frequency on both axes and, perhaps optionally, (b) occur at a subset of the same set of equally-spaced values. The default is compared to (a) and (b) below in a simple example. (The option might be useful even for general cartesian coordinates.)

I had a go at building a new Coord*() ggproto, but i quickly realized that it would take a deeper dive into the ggplot2 internals than i'd prepared for. I'm glad to give it a serious try, and to send a PR if it seems to work, but i wanted to check with the maintainers first to be sure i haven't overlooked something and that the feature makes sense.

Thank you!

library(ggplot2)

# default breaks form non-square background grid
ggplot(mtcars, aes(mpg, qsec)) + geom_point() + coord_fixed()

# inelegant way to force a square grid
break_at_2 <- function(limits) {
  seq(ceiling(limits[1]), floor(limits[2]), 2)
}
ggplot(mtcars, aes(mpg, qsec)) + geom_point() + coord_fixed() +
  scale_x_continuous(breaks = break_at_2) +
  scale_y_continuous(breaks = break_at_2)

# inelegant way to force breaks onto the same sequence
break_by_2 <- function(limits) {
  seq(ceiling(limits[1] / 2) * 2, floor(limits[2] / 2) * 2, 2)
}
ggplot(mtcars, aes(mpg, qsec)) + geom_point() + coord_fixed() +
  scale_x_continuous(breaks = break_by_2) +
  scale_y_continuous(breaks = break_by_2)

Activity

  1. added
    feature
    on May 27, 2019
  2. teunbrand commented on Mar 5, 2024

    @teunbrand
    Collaborator

    While I understand the desire to have this functionality, I don't think we should implement this.
    In particular, I don't think we should mingle scale functionality into coords when we don't have to.
    I do not like that coords have xlim and ylim functionality, as I think that should belong to the scale, and I think giving control over breaks would make this breach of the grammar worse.
    I'll go ahead and close this as 'not planned', but we can reopen if other maintainers disagree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurescales 🐍

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions