简体   繁体   中英

How to fill pie chart in the entire panel in ggplot2 in R?

I want to create a pie chart with only the pie-circle displayed using ggplot2 and geom_col in R, so that the entire plot is just the pie-circle without any space on each side. Below is an example of my code:

library('ggplot2')
df = data.frame(group=c("a","b","c"), 
                value=c(0.2,0.4,0.4))

p=ggplot(df,aes(x="", y=value, fill=group)) + 
  geom_col(position="fill") + 
  coord_polar(theta = "y") +
  guides(fill=F) +
  theme(
    panel.background = element_rect(fill = "grey", colour = NA), 
    #"grey" is just to show the panel area
    plot.background = element_rect(fill = "transparent", colour = NA),
    panel.grid = element_blank(),
    panel.border = element_blank(),
    plot.margin = unit(c(0, 0, 0, 0), "null"),
    panel.spacing = unit(0, "null"),
    axis.ticks = element_blank(),
    axis.text = element_blank(),
    axis.title = element_blank(),
    axis.line = element_blank(),
    legend.position = "none",
    axis.ticks.length = unit(0, "null"),
    axis.ticks.margin = unit(0, "null"),
    legend.margin = margin()
  )

Below is the figure it created. What I wanted is to have the top and bottom of the circle be at the top and bottom edge of the grey area. How do I do that?

数字

I think this is hard-coded in ggplot2. You can dig under the hood by entering trace(ggplot2:::r_rescale, edit = TRUE) into the console (see function definition for the unexported r_rescale at the bottom of this GH page ) and change the rescale value from 0.4 to 0.5 .

Result from re-running the code after editing the function:

阴谋

When you are done, run untrace(ggplot2:::r_rescale) & things will return to normal.


ps On an unrelated note, you can skip all the theme specifications in your code if you specify theme_void() (to override default theme_grey() ).

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