3

I have a pie graph. I want to reduce the white space in the panel. I made the background colored green for you to see the area occupied by the white space.

Here is my data

structure(list(PER = c(34.4413912651734, 64.2857433622907, 1.2728653725359
), CAT = structure(1:3, .Label = c("Yes", "No", "Don’t know/Refused"
), class = "factor")), row.names = c("Yes", "No", "Don’t know/Not sure"
), class = "data.frame")

Here is what I tried so far:

xfall <- ggplot(table_fall_FRQ, aes(x="", y=PER, fill=CAT)) +
      geom_bar(width = 1, size = 1, stat = "identity") +
      coord_polar("y") +
      geom_text(aes(label = paste0(round(PER), "%")), 
              position = position_stack(vjust = 0.5), color="white",
              size=14) +
      labs(#title="", 
        #subtitle="", 
        caption="n= respondents with income less than $60,000 year \nSource: Sample",
        x="",
        y="") +
  scale_fill_viridis(discrete=TRUE) +
  guides(fill = guide_legend(reverse = FALSE)) +
  theme_classic() +
  theme(axis.text.x = element_blank(),
        axis.line.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.line = element_blank(),
        axis.text=element_text(size=14, color="white"),
        #axis.title=element_text(size=14,face="bold"),
        #panel.grid.major = element_blank(),
        panel.spacing.x=unit(0, "lines"),
        panel.spacing.y=unit(0, "lines"),
        legend.position="top",
        legend.box.spacing = unit(0.1, 'cm'),
        legend.margin = margin(0, 0, 0, 0, "cm"),
        legend.title = element_blank(),
        plot.caption = element_text(hjust = 0, lineheight=1.5),
        #plot.subtitle = element_text(color="blue"),
        plot.background = element_rect(fill="green"),
        plot.margin = margin(0, 0, 0, 0, "cm")
        ) 

xfall

piegraph Which part shall I update in order for the white space to shrink? id like my legend and caption to get closer to the pie.

dixi
  • 680
  • 1
  • 13
  • 27
  • 1
    Try `plot.margin=unit(c(0,0,-12,-5), "mm")` as seen [here](https://stackoverflow.com/questions/40407498/how-to-remove-margins-in-ggplot2-chart/40408281#40408281). The green area on the left goes away. – Rui Barradas Aug 26 '19 at 18:35
  • thanks for the response but Id like the white space surrounding the pie itself to shrink. – dixi Aug 26 '19 at 19:09

1 Answers1

2

the post of Rui helped me find it.

xfall <- ggplot(table_fall_FRQ, aes(x="", y=PER, fill=CAT)) +
  geom_bar(width = 1, size = 1, stat = "identity") +
  coord_polar("y") +
  geom_text(aes(label = paste0(round(PER), "%")), 
            position = position_stack(vjust = 0.5), color="white",
            size=14) +
  labs(#title="", 
    #subtitle="", 
    caption="n= respondents with income less than $60,000 year \nSource: Sample",
    x="",
    y="") +
  scale_fill_viridis(discrete=TRUE) +
  guides(fill = guide_legend(reverse = FALSE)) +
  theme_classic() +
  theme(axis.text = element_blank(), 
        axis.ticks.length = unit(0, "mm"),
        axis.line = element_blank(),
        #axis.title=element_text(size=14,face="bold"),
        #panel.grid.major = element_blank(),
        panel.spacing.x=unit(0, "lines"),
        panel.spacing.y=unit(0, "lines"),
        legend.position="top",
        legend.box.spacing = unit(-1, 'cm'),
        legend.margin = margin(0, 0, 0, 0, "cm"),
        legend.title = element_blank(),
        plot.caption = element_text(hjust = 0,margin = unit(c(-15,0,0,0), "mm")),
        #plot.subtitle = element_text(color="blue"),
        plot.background = element_rect(fill="green"),
        plot.margin = margin(0, 0, 0, 0, "cm")
  ) 

xfall

You can further change the position of the caption by using plot.caption and the position of the legend by using a vector, like c(0.5, 0.5) instead of "top".

Hope this is helpfull