ggplot2中,如何在饼图中间加一个白洞

In ggplot2, how to add a white hole in the middle of the pie chart

在ggplot2中,如何在饼图中间加一个白洞? 请参考以下代码了解当前情节(左图)。谢谢!

library(tidyverse)
pie_data <- data.frame(category=c('A','B','C','A','B','C'),
                       year=c(2020,2020,2020,2021,2021,2021),
                       sales=c(40,30,20,10,15,10))




pie_data %>% ggplot(aes(x=factor(year),y=sales,fill=category))+
  geom_col(position='fill',width=1,color='white')+
 coord_polar(theta = 'y')+
  theme_void()

只需扩大 x 轴的范围(如果不将年份转换为因数,这样做会更容易):

pie_data %>% ggplot(aes(x = year, y = sales, fill = category))+
  geom_col(position = 'fill', width = 1, color = 'white') +
  coord_polar(theta = 'y') + 
  lims(x = c(2019, 2022)) +
  theme_void()

你可以通过改变上面代码中的2019来控制白洞的大小。年份越早,坑越大:

pie_data %>% ggplot(aes(x = year, y = sales, fill = category))+
  geom_col(position = 'fill', width = 1, color = 'white') +
  coord_polar(theta = 'y') + 
  lims(x = c(2017, 2022)) +
  theme_void()

您可以在中心添加一个空心条:

  pie_data %>% ggplot(aes(x=factor(year),y=sales,fill=category))+
      geom_col(position='fill',width=1,color='white')+
      coord_polar(theta = 'y')+
      geom_col(aes(x=0,y=0))+
      theme_void()

如果您想要更大的圆,请使用负 x 坐标:

geom_col(aes(x=-1,y=0))