rggplot2polar-coordinates

Remove whitespace between axis.line and panel.border in geom_radial with R ggplot2


I am trying to create a simple radial/polar plot in R ggplot2 using coord_radial(), which is similar to coord_polar but with more options. Below is an example plot with data:

library(ggplot2)

plot_data <- data.frame('xval' = seq(1, 100),
                        'yval' = seq(100, 1))

ggplot(plot_data, aes(x = xval, y = yval))+
  geom_col()+
  scale_y_continuous(expand = c(0, 0))+
  coord_radial(theta = 'x', start = 0)+
  
  theme_void()+
  theme(panel.border = element_rect(fill = NA),
        axis.line.x = element_line())

Which produces a plot that looks as follows: enter image description here

What I want to do is remove the spacing between the inner circle (defined by axis.line.x) and the outer rectangle (defined by panel.border). In other words, I need the circle to be touching the outer panel.border. I have tried manipulating the expand parameters in scale_x/y_continuous and adjusting the theme() but to no avail. The "expand" and "clip" parameters in coord_radial() also had no effect.

The end goal is that I want to be able to save the plot as an image where the borders are defined by the circle limits and not by the outer rectangle. If there is another way of doing this besides removing the spacing, that should work too.

Thank you in advance!


Solution

  • Following this comment by @teunbrand on a related GH issue you can get rid of the extra white space by adding

    ggproto(
      NULL, coord_radial("y", start=0, expand = FALSE),
      inner_radius = c(0, 0.5)
    )
    

    to your plot.

    library(ggplot2)
    
    plot_data <- data.frame(
      "xval" = seq(1, 100),
      "yval" = seq(100, 1)
    )
    
    ggplot(plot_data, aes(x = xval, y = yval)) +
      geom_col() +
      theme_void() +
      theme(
        panel.border = element_rect(fill = NA, color = "red"),
        axis.line.x = element_line(color = "green")
      ) +
      ggproto(
        NULL, coord_radial("x", start = 0, expand = FALSE),
        inner_radius = c(0, 0.5)
      )
    

    enter image description here