简体   繁体   中英

Reduce margins around ggplot2 pie chart

I am including a pie chart in a document and the other charts are in ggplot2 , so I'd like to use ggplot2 to keep the charts consistent. However, the pie chart does not extend to the margins

dat <- structure(list(y = c(0.0714285714285714, 
                            0.0714285714285714, 
                            0.107142857142857, 
                            0.160714285714286, 
                            0.25, 
                            0.339285714285714), 
                      x = structure(c(1L, 3L, 4L, 5L, 6L, 2L), 
                                    .Label = c("1 to 5", "6", "7", "8", "9", "10"), 
                                    class = "factor")), 
                 class = "data.frame", 
                 .Names = c("y", "x"), row.names = c(NA, -6L))

ggplot(dat, aes(x = factor(1), y = y, fill = factor(x))) + 
  geom_bar(width = 1, stat = "identity") + 
  coord_polar(theta  = "y") + 
  theme_grey()

I'd be using theme_void() ; I've used theme_grey to illustrate the point: the edges of the pie chart don't extend far enough in the panel. How can I make the diameter of the pie equal (or very close to) the width of the panel? Modifying width=1 or scale_x_discrete(expand = c(0,0)) has no effect. The arguments to coord_polar don't seem to provide any clue either.

在此处输入图片说明

This doesn't actually solve your the problem but it does enlarge the area of the pie chart to approximately the initial size of the panel.

ggplot(dat, aes(x = factor(1), y = y, fill = factor(x))) + 
geom_bar(width = 1, stat = "identity") + 
coord_polar(theta  = "y") + 
guides(fill=guide_legend(override.aes=list(colour=NA))) +
theme(panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      axis.text.x=element_blank(),
      plot.margin = unit(c(-.75,-.75,-.75,-.75),"cm")
      )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM