admin管理员组

文章数量:1287650

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:

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!

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:

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!

Share Improve this question asked Feb 23 at 17:55 AmpAmp 716 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

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)
  )

本文标签: Remove whitespace between axisline and panelborder in geomradial with R ggplot2Stack Overflow