繁体   English   中英

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

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

ggplot2,如何在饼图中间加一个白洞? 电流plot请参考下面的代码(左图)。 谢谢!

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))

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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