ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

coord_polar _ remove extra grid line

Open jfmoyen opened this issue 3 years ago • 4 comments

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."

jfmoyen avatar Apr 26 '22 07:04 jfmoyen

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?

thomasp85 avatar May 12 '22 08:05 thomasp85

@thomasp85 no idea 😄

hadley avatar May 12 '22 20:05 hadley

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.

teunbrand avatar Aug 13 '22 19:08 teunbrand

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?

AllanCameron avatar Aug 13 '22 21:08 AllanCameron

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

teunbrand avatar Jan 23 '23 18:01 teunbrand

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.

clauswilke avatar Jan 23 '23 18:01 clauswilke

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.

teunbrand avatar Jan 23 '23 19:01 teunbrand

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.

AllanCameron avatar Jan 23 '23 19:01 AllanCameron