如何避免饼图中的标签重叠 [英] How to avoid label overlap in pie chart

查看:749
本文介绍了如何避免饼图中的标签重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在饼图中标注百分比有问题.由于空间不足以鲜明地显示它们,因此有些标签重叠.你能给我解决办法吗?另外,我也不想标注零百分比.

I have a problem with labeling percent in pie charts. Some labels are overlapped since the space is not enough to distinctively show them. Could you give me any solution? Also, I don't want to label zero percents.

数据是这样的:

  BRCA_R_Y_4<-          structure(list(Group = c("1. General", "1. General", "1. General", 
                            "1. General", "1. General", "1. General", "1. General", "2. Cancer", 
                            "2. Cancer", "2. Cancer", "2. Cancer", "2. Cancer", "2. Cancer", 
                            "2. Cancer", "3. Clinicians", "3. Clinicians", "3. Clinicians", 
                            "3. Clinicians", "3. Clinicians", "3. Clinicians", "3. Clinicians", 
                            "4. Researchers", "4. Researchers", "4. Researchers", "4. Researchers", 
                            "4. Researchers", "4. Researchers", "4. Researchers"), 


           Reasons = c( "1. Burdened by the cost", "2. Believe I would not develop breast cancer ", 
                        "3. I don’t have breast cancer yet (it won’t be too late to treat afterwards)", 
                        "4. Worried about the side effects", "5. Have childbirth plans", 
                        "6. Do not trust test result", "7. Other", "1. Burdened by the cost", 
                        "2. Believe I would not develop breast cancer ", "3. I don’t have breast cancer yet (it won’t be too late to treat afterwards)", 
                        "4. Worried about the side effects", "5. Have childbirth plans", 
                        "6. Do not trust test result", "7. Other", "1. Burdened by the cost", 
                        "2. Believe I would not develop breast cancer ", "3. I don’t have breast cancer yet (it won’t be too late to treat afterwards)", 
                        "4. Worried about the side effects", "5. Have childbirth plans", 
                        "6. Do not trust test result", "7. Other", "1. Burdened by the cost", 
                        "2. Believe I would not develop breast cancer ", "3. I don’t have breast cancer yet (it won’t be too late to treat afterwards)", 
                        "4. Worried about the side effects", "5. Have childbirth plans", 
                        "6. Do not trust test result", "7. Other"), 
           Percent = c(8.1130355515041,16.9553327256153, 57.0647219690064, 12.1239744758432, 2.91704649042844, 
                                                                                                                                           2.73473108477666, 0.0911577028258888, 3.85304659498208, 6.00358422939068, 
                                                                                                                                           73.7455197132617, 13.1720430107527, 0.896057347670251, 1.88172043010753, 
                                                                                                                                           0.448028673835125, 1.40845070422535, 2.8169014084507, 78.8732394366197, 
                                                                                                                                           2.8169014084507, 4.22535211267606, 0, 9.85915492957746, 1.63265306122449, 
                                                                                                                                           6.53061224489796, 71.8367346938775, 6.93877551020408, 6.12244897959184, 
                                                                                                                                           2.04081632653061, 4.89795918367347)) , row.names = c(NA, -28L) , class = c("tbl_df", 
                                                                                                                                                                                                                    "tbl", "data.frame"))

以下是我用于生成饼图的代码,该代码的参考是: ggplot,构面,饼图:放置文本在饼图切片中间

And the code that I used for generating the pie chart is the following and reference of this code is : ggplot, facet, piechart: placing text in the middle of pie chart slices

dat_pies4 <- left_join(BRCA_R_Y_4,
                       BRCA_R_Y_4 %>% 
                         group_by(Group) %>%
                         summarize(Cnt_total = sum(Percent))) %>%
  group_by(Group) %>%
  mutate(end_angle = 2*pi*cumsum(Percent)/Cnt_total, each pie slice
         start_angle = lag(end_angle, default = 0),  each pie slice
         mid_angle = 0.5*(start_angle + end_angle))  

rpie = 1
rlabel = 0.6 

 dat_pies4 <- mutate(dat_pies4,
                    hjust = ifelse(mid_angle>pi, 1, 0),
                    vjust = ifelse(mid_angle<pi/2 | mid_angle>3*pi/2, 0, 1))

rlabel = 1.05 * rpie # now we place labels outside of the pies

pie.4 <-
  ggplot(dat_pies4) + 
  geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = 0, r = rpie,
               start = start_angle, end = end_angle, fill = Reasons))+
  geom_text(aes(x = rlabel*sin(mid_angle), y = rlabel*cos(mid_angle), label = paste0 (round(Percent), "%"),
                hjust = hjust, vjust = vjust)) +
  coord_fixed() +
  scale_x_continuous(limits = c(-1.5, 1.5), name = "", breaks = NULL, labels = NULL) +
  scale_y_continuous(limits = c(-1.5, 1.5), name = "", breaks = NULL, labels = NULL) +
  facet_grid(.~Group, switch = "both")+
  theme_void()+
  scale_fill_grey()+
  theme(legend.position="bottom", legend.direction="vertical", legend.margin = margin(30, 0, 10, 0))+
  theme(plot.title = element_text(size = 12, hjust = 0.5))+
  labs(title = "<why do not want RRM>")+
  guides (fill =  guide_legend (title.theme = element_text (face = "bold")))

pie.4

结果是这样的:

我对新饼图的期望如下所示,但标签应位于饼图之外

My expectation for the new pie chart is like the following but labels should be outside of the pies:

来源

推荐答案

一种简单的方法是使用库ggrepel.

An easy way is to use the library ggrepel.

library(ggrepel)

然后替换:

geom_text

作者:

geom_text_repel

这篇关于如何避免饼图中的标签重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆