R 如何将百分比标签移到ggplot2中饼图的外部?

R 如何将百分比标签移到ggplot2中饼图的外部?,r,ggplot2,charts,R,Ggplot2,Charts,因此,我有一个许多hpv亚型的饼图,这是有问题的,因为有些是1%,百分比标签不合适。因此,我想知道如何将%标签移到饼图之外,使其可读 我的代码是: #Creating my data frame HPV <- c("HPV16","HPV18","HPV45","HPV58","HPV68","HPV52","HPV31","HPV39&qu

因此,我有一个许多hpv亚型的饼图,这是有问题的,因为有些是1%,百分比标签不合适。因此,我想知道如何将%标签移到饼图之外,使其可读

我的代码是:

#Creating my data frame
HPV <- c("HPV16","HPV18","HPV45","HPV58","HPV68","HPV52","HPV31","HPV39","HPV30","HPV33","HPV56","HPV59","HPV73","HPV35","HPV70","HPV69")
proportion <- c(0.609467456,0.159763314,0.059171598,0.029585799,0.00591716,0.029585799,0.017751479,0.017751479,0.00591716,0.017751479,0.00591716,0.017751479,0.00591716,0.00591716,0.00591716,0.00591716)
df<-data.frame(HPV,proportion)

library(ggplot2)

#Creating the pie chart
pie <- ggplot(data = df, aes(x="", y=proportion, fill=HPV)) +
  geom_col(color = "black") + 
  coord_polar("y", start=0) +
  geom_text(aes(label=paste0(round(proporción*100), "%")),
            position = position_stack(vjust=0.5)) +
  theme(panel.background = element_blank(),
        axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 18)) +
  ggtitle("Subtypes of HPV in GDC TCGA cervical cancer (CESC)") + 
  scale_fill_manual(values = c("#F46698","#F36DDB","#DD6DF3","#AC6DF3","#7355FC","#5562FC","#5562FC","#55B5FC","#55C7FC","#55E8FC","#56EDEB","#93F9EF","#61F9BF","#5BEC75","#58D64B","#91D64B","#B4D64B","#D6D64B","#FFDB57"))

print(pie)
#创建我的数据帧

HPV这有点麻烦,但您可以将x坐标指定为略偏于法线条形图的右侧,然后将条形图包装成饼图时,coord_polar会将其略偏外。默认的x坐标是1,因此使用1.5将它们放置在图表的边缘,1.6仅位于图表的外部。所有代码基本相同,但请注意新的
x=1.6
添加到
aes()
调用
geom\u text()


是的,它解决了大部分问题,除了最后的百分比,但还是要感谢您。
ggplot(data = df, aes(x="", y=proportion, fill=HPV)) +
  geom_col(color = "black") + 
  coord_polar("y", start=0) +
  geom_text(aes(x=1.6, label=paste0(round(proportion*100), "%")),
            position = position_stack(vjust=0.5)) +
  theme(panel.background = element_blank(),
        axis.line = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        plot.title = element_text(hjust = 0.5, size = 18)) +
  ggtitle("Subtypes of HPV in GDC TCGA cervical cancer (CESC)") + 
  scale_fill_manual(values = c("#F46698","#F36DDB","#DD6DF3","#AC6DF3","#7355FC","#5562FC","#5562FC","#55B5FC","#55C7FC","#55E8FC","#56EDEB","#93F9EF","#61F9BF","#5BEC75","#58D64B","#91D64B","#B4D64B","#D6D64B","#FFDB57"))