Skip to content

coord_polar _ remove extra grid line #4815

Closed

Description

@jfmoyen

The issue has been discussed on https://stackoverflow.com/questions/72005238/scale-y-axis-on-a-polar-plot-in-ggplot/72005779#72005779 from which I copy the exchange:

When plotting the following,

library(ggplot2)
library(tibble)

foo <- tibble(theta = c(0, 10, 20), r = c(10, 30, 90))

ggplot(foo) +
  geom_point(aes(x = theta, y = r)) +
  coord_polar() +
  scale_y_continuous(limits = c(0, 90), expand = c(0, 0), breaks = 90) +
  scale_x_continuous(limits = c(0, 360), expand = c(0, 0), breaks = c(0, 90, 180, 270)) +
  theme(
    panel.grid.minor = element_blank(),
    panel.grid.major.y = element_line(colour = "red")
  )

the Y-axis (= radius) expands to 100 instead of 90. In addition there are two circles corresponding to major grid elements, one at 90 as required and one at 100. I added the "theme" statement to demonstrate that both correspond to major grid lines.

A workaround proposed by user Allan Cameron was

ggplot(foo) +
  geom_point(aes(x = theta, y = r)) +
  coord_polar() +
  scale_y_continuous(limits = c(0, 90), expand = c(0, 0), breaks = 90) +
  scale_x_continuous(limits = c(0, 360), expand = c(0, 0), breaks = c(0, 90, 180, 270)) +
  theme(
    panel.grid.minor = element_blank(),
    panel.grid.major.y = element_line(colour = c("red", NA))
  )

... which works, but is hardly satisfactory ! Quoting A.C. :
"There is no outer axis line in coord_polar, and a grid line seems to be used in its place, without respecting axis.line theme elements."

Activity

  1. thomasp85 commented on May 12, 2022

    @thomasp85
    Member

    I can see in the code that the outer ring gets added explicitly as a final grid line...

    I see two paths:

    1. Make the addition user-controllable with an add.outer.grid or something like that
    2. Make the style pull its parameters from panel.border or even something unique so it can be turned off or changed in the theme

    @hadley do you remember any reasoning for adding the outer grid circle?

  2. hadley commented on May 12, 2022

    @hadley
    Member

    @thomasp85 no idea 😄

  3. teunbrand commented on Aug 13, 2022

    @teunbrand
    Collaborator

    I think @AllanCameron's comment makes sense.

    "There is no outer axis line in coord_polar, and a grid line seems to be used in its place, without respecting axis.line theme elements."

    It is conceptually similar to an axis line, but on polar coordinates, so styling it with the axis.line theme element seems intuitive to me. However, it would also invite people's request for also controlling axis ticks on polar coordinates.

  4. AllanCameron commented on Aug 13, 2022

    @AllanCameron

    Make the style pull its parameters from panel.border or even something unique so it can be turned off or changed in the them

    Pulling the style from panel.border loses the flexibility of adding a rectangular panel border, which I have seen used on occasion with polar plots.

    I agree with @teunbrand that it would be nice to be able to use axis.line.x or axis.line.y depending on which aesthetic is mapped to theta, but that would mean coord_polar has a black circle round it by default. My guess is that most users wouldn't want that. Of course, coord_polar could turn off the axis line by default, but this feels like it would break the modular design ethos a bit. Perhaps a novel theme element as @thomasp85 suggests like panel.border.polar?

    it would also invite people's request for also controlling axis ticks on polar coordinates

    I actually think this would be feasible, though maybe better in an extension package.

    It feels a bit small print, but with ragg and extension packages like geomtextpath, it is now possible to make some very attractive polar plots - I have seen some great examples in recent months, and I think it would be useful to add a themeable outer line to polar plots at some point. Worth a PR?

  5. teunbrand commented on Jan 23, 2023

    @teunbrand
    Collaborator

    Maybe, we can have coord_polar() have a proper guide_axis_theta() to control details over the axis drawing.

  6. clauswilke commented on Jan 23, 2023

    @clauswilke
    Member

    Honestly, the fact that we have to manipulate x and y axes in polar coordinates has always been confusing to me. Not sure technically how it would work, and what the situation with backwards compatibility would be (there are hundreds of thousands of polar plots out there manipulating x and y axes), but definitely massive room for improvement.

  7. teunbrand commented on Jan 23, 2023

    @teunbrand
    Collaborator

    The way I'm imagining this is that after the guide rewrite, we can pull coord_polar() and coord_sf() into the fold of coords that use guides. Because axis guides are mostly controlled by the coords, we can have the polar coord draw the axis into the panel instead of next to the panel. I'm sure we could mostly parameterise the axis guide such that the current look will be preserved by default.

  8. AllanCameron commented on Jan 23, 2023

    @AllanCameron

    I really like the idea of guides drawing axes in polar and sf coordinates, and I'm sure it could be done without affecting older plot codes.
    A recent Q&A on SO shows the kind of thing that I suspect many end-users would like coord_sf to be able to do, and I guess this would be possible using guides.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions