0

I'm having trouble adding vertical spacing to the y axis on a pie chart, without also stretching the color boxes, is there a way to keep the color boxes the same size (square), while increasing the vertical spacing between each element?

I've tried a bunch of different things from modifying theme(legend.text = element_text(margin = margin(t = 1,b = 1, unit ='cm'))) to legend.spacing.y, and keyheight, but nothing seems to work so far.

Example:

library(tidyverse)

df1 <- data.frame(Color = c("Blue", "Red", "Green"), pcts = c(.45, .25, .30))
df1$ymax <- cumsum(df1$pcts)
df1$ymin <- lag(df1$ymax, 1, default = 0)

df1 %>% ggplot(aes(ymax = ymax, ymin = ymin, xmax = 4, xmin = 2.85, fill = Color)) + 
  geom_rect() +
  coord_polar(theta = "y") + 
  theme(legend.text = element_text(margin = margin(t = 1, b = 1, unit = 'cm')))

Also, the below charts are rendered using Cario, but the default graphics device produces the same result.

Example #1

Desired legend:

enter image description here

Mako212
  • 6,787
  • 1
  • 18
  • 37
  • Using the same code I have your desired legend. Maybe the problem is related to some graphics option and not to the code... – Terru_theTerror Aug 23 '18 at 15:52
  • @Terru_theTerror are you on a Mac? – Mako212 Aug 23 '18 at 15:52
  • Hmm... just tried on a Mac and I get the same result in my question, any idea what the graphics setting could be? – Mako212 Aug 23 '18 at 15:54
  • I'm on windows. – Terru_theTerror Aug 23 '18 at 16:01
  • Marking as a duplicate because [this](https://stackoverflow.com/questions/49668265/increase-space-between-legend-keys-without-increasing-legend-keys) solves the problem, note that for a custom color palette, you need to add `aesthetics = c("fill", "color")` to your `scale_color/fill` call – Mako212 Aug 23 '18 at 22:53

0 Answers0