tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.4k stars 2k forks source link

Increasing the margin/whitespace within facet panel borders #2698

Closed everydayduffy closed 2 years ago

everydayduffy commented 6 years ago

When exporting this plot, the x-axis labels are cut-off by the borders of the individual facet panels. The only way I can currently see to solve this is to reduce the text size, but that makes it unreadable. Is there a way to use margin to add margins/whitespace to the plotting area in each panel within a facet plot? If not, is there a case for a feature request here?

library(ggplot2)

cars <- c(rep("ford",8),rep("vauxhall",8),rep("renault",8),rep("tesla",8))
mtcars <- cbind(mtcars, cars)

ggplot(data=mtcars, aes(x = cars,y=mpg)) +
  geom_bar(stat="identity", fill = "darkgrey", colour = NA, alpha = 0.75) +
  theme_bw() +
  theme(axis.text = element_text(size = 8),
        axis.text.x = element_text(size = 12, margin = margin(t = 50)),
        panel.spacing = unit(1, "lines")) +
  coord_polar(start = -0.4) +
  facet_grid(gear ~ am, scales = "free") 

ggsave("C:/Sandbox/myplot.png",plot = last_plot(), width = 130, height = 177, units = "mm", dpi = 900)
clauswilke commented 6 years ago

I'm not entirely sure what the desired result is. In any case, your example does not run with the latest ggplot2, because coord_polar() doesn't support free scales:

library(ggplot2)

cars <- c(rep("ford",8),rep("vauxhall",8),rep("renault",8),rep("tesla",8))
mtcars <- cbind(mtcars, cars)

ggplot(data=mtcars, aes(x = cars,y=mpg)) +
  geom_bar(stat="identity", fill = "darkgrey", colour = NA, alpha = 0.75) +
  theme_bw() +
  theme(axis.text = element_text(size = 8),
        axis.text.x = element_text(size = 12, margin = margin(t = 50)),
        panel.spacing = unit(1, "lines")) +
  coord_polar(start = -0.4) +
  facet_grid(gear ~ am, scales = "free") 
#> Error: coord_polar doesn't support free scales

For more sophisticated plots in polar coordinates, I recommend drawing them in cartesian coordinates using the appropriate geoms from ggforce, as described in this SO answer.

everydayduffy commented 6 years ago

@clauswilke - strange that free scales have been deprecated. I'm using version 2.2.1. Anyway, my initial question still stands as it is an issue regardless of the scales chosen. How can you increase the whitespace in the plotting area, so that labels do not get cut off by the borders?

clauswilke commented 6 years ago

@everydayduffy I understand. And the second part of the SO answer I linked to addresses that exact question. Whether the same effect can be achieved in polar coordinates I don't know, but I think not. Maybe @hadley or @thomasp85 know the answer.

Also, @hadley, should coord_polar allow for free scales? It seems that the relevant argument for the other fixed-aspect-ratio coordinate systems doesn't apply here, since polar coordinates always have a 1:1 ratio regardless of scale limits.

thomasp85 commented 6 years ago

I would personally be against free scales in coord_polar - it’s a mapping of x and y values to radii and angle, which will be violated if the aspect ratio is not kept at 1:1

As for the other, I’ll have to be in front of a computer to understand what the actual issue are, unless someone can show some plots illustrating it

everydayduffy commented 6 years ago

roses

thomasp85 commented 6 years ago

Ah - I see. There’s unfortunately no way to do this automatically. I think it would make sense to add arguments comparable to xlim/ylim in the other coords, so that users can zoom out and in in a polar coordinate system, but for this to work here it shouldn’t affect the relative placement of x-axis labels...

everydayduffy commented 6 years ago

Could this turn into a feature request then? Or do you feel that it's just a 'not going to happen' sort of thing. Thanks all for your feedback so far.

hadley commented 2 years ago

It's unlikely to ever happen sorry; we don't even have a good way to model with problem with in ggplot2.