如何将标签对齐到圆环图中的中间/中心

How to align the labels to the middle / center in a donut chart

我正在使用 geom_textpath 向双圆环图添加标签,但标签未与中间对齐。

如何在每个段的中间或中心对齐标签?

library(magrittr)
library(ggplot2)
library(geomtextpath)
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=year,y=sales,fill=category))+
  geom_col(position='fill',width=1,color='white')+
 lims(x=c(2018,2023))+
  geom_textpath(position='fill',angle=90,hjust=2,alpha=1,
                aes(color=factor(year),
                    label=paste0(category,':',sales)))+
 coord_polar(theta = 'y')+
  
  theme_void()

要在使用 position="fill" 时定位标签,最好知道 position_fill 有一个参数 vjust 可以使用 vjust = .5 将标签定位在条形中间:

library(magrittr)
library(ggplot2)
library(geomtextpath)

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 = year, y = sales, fill = category)) +
  geom_col(position = "fill", width = 1, color = "white") +
  lims(x = c(2018, 2023)) +
  geom_textpath(
    position = position_fill(vjust = .5), angle = 90, alpha = 1,
    aes(
      color = factor(year),
      label = paste0(category, ":", sales)
    )
  ) +
  coord_polar(theta = "y") +
  theme_void()