代码之家  ›  专栏  ›  技术社区  ›  anderwyang

在GPPLTT2中,如何在饼图中间添加一个白洞。

  •  1
  • anderwyang  · 技术社区  · 4 年前

    在GPPLTT2中,如何在饼图中间添加一个白色的洞? 当前图(左图)请参考以下代码。谢谢

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

    enter image description here

    1 回复  |  直到 4 年前
        1
  •  6
  •   Allan Cameron    4 年前

    只需扩大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()
    

    enter image description here

    您可以通过在上面的代码中更改大小来控制白洞的大小。一年中越早,洞越大:

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

    enter image description here

        2
  •  1
  •   Antonio Petraglia    4 年前

    可以在中心添加空心杆:

      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))
    
    推荐文章