【问题标题】:Drawing arrows in pie chart with ggplot2用ggplot2在饼图中绘制箭头
【发布时间】:2020-09-17 02:28:55
【问题描述】:

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

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

【问题讨论】:

  • 查看 ggrepel 包。

标签: r ggplot2


【解决方案1】:

试试这个:

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

郑重声明:不要使用饼图

制作饼图之所以复杂是有原因的:您不应该使用它们。 条形图总是更好。

【讨论】:

  • 谢谢。但价值更多。我想把它们放在外面,用箭头指向它们。就我而言,要求是来自客户端的饼图 :) 这些值加起来是 360 所以。
  • 好吧等等..我会试着看看我能做些什么..但我认为你应该只对一定数量的案例进行分组以提高可读性。无论如何,不​​要向您的客户弯曲统计数据;-)
  • 非常感谢您的帮助
  • 我找到了一种将文本设置在外部而不裁剪它们的方法。看看我的编辑。我现在看看能不能连上一条线..
  • 是你要找的吗?如果是这样,您应该接受答案:它对其他用户也会更有用。否则,请告诉我。
猜你喜欢
  • 2018-04-24
  • 1970-01-01
  • 2022-08-13
  • 2012-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多