繁体   English   中英

使用 ggplot2 在饼图中绘制箭头

[英]Drawing arrows in pie chart with ggplot2

对于下面的数据框,如何在圆圈外显示带有箭头指向的标签?

asd <- data.frame(a=c("fs","dfg","gf"), b=c(3,5,6))
ggplot(asd, aes(x="", y= b, fill = factor(a))) + 
  geom_bar(stat = "identity", width =1) + 
  coord_polar(theta = "y") + theme_void() + 
  geom_text(aes(label=paste(a, sep = " ", b, "%"), x= 1.3, angle = 0))

试试这个:

library(ggplot2)
library(dplyr)

asd <- data.frame(a = c("fs","dfg","gf"), 
                  b = c(3,10,5)) # changed to show that order doesnt matter

asd %>% 
 mutate(prop = b/sum(b)) %>% 
 arrange(desc(a)) %>%
 mutate(lab.ypos = cumsum(prop) - 0.5*prop) %>% 
 
 ggplot(aes(x = "", y = prop, fill = a)) +
 geom_bar(width = 1, stat = "identity", color = "white") +
 coord_polar("y", start = 0, clip = "off")+
 geom_segment(aes(x = 1, y = lab.ypos, xend = 2, yend = lab.ypos, colour = a),
              size = 1) +
 geom_label(aes(y = lab.ypos, label = paste(a, scales::percent(prop))),
           x = 2,
           size = 5,
           color = "white") +
 theme_void() +
 theme(legend.position = "none")

在此处输入图片说明

为了记录:不要使用 pie chart

做饼图很复杂是有原因的:你不应该使用它们。 条形图总是更好。

暂无
暂无

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

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