在ggplot2中如何向圆环图添加角度标签

and*_*ang 2 r ggplot2

下面的代码可以将圆环图绘制为附图中的左侧图表,如何将标签更改为右侧图表?谢谢!

library(tidyverse)
pie_data <- data.frame(category=c('A','B','C'),sales=c(70,25,40))
pie_data %>% ggplot(aes(x=1,y=sales,fill=category))+
  geom_col()+
  scale_fill_brewer(direction = -1)+
  geom_text(position=position_stack(vjust=0.5),aes(label=paste0('Category ',category,':',sales)))+
  xlim(-1,2)+
  coord_polar(theta = 'y')+
  theme_void()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

All*_*ron 5

您可以使用geom_textpathgeomtextpath 包,该包现在位于 CRAN 上。本质上只需替换geom_textgeom_textpath并将角度设置为 90 度即可。

library(geomtextpath)
library(dplyr)

pie_data <- data.frame(category = c('A', 'B', 'C'),
                       ,sales = c(70, 25, 40))
pie_data %>% ggplot(aes(x = 1, y = sales, fill = category)) +
  geom_col() +
  scale_fill_brewer(direction = -1) +
  geom_textpath(position = position_stack(vjust = 0.5), angle = 90, size = 8,
            aes(label = paste0('Category ', category, ':', sales))) +
  xlim(-1, 2) +
  coord_polar(theta = 'y') +
  theme_void()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述