组合型ggplot饼图
ggplot饼图
我们知道ggplot对于饼图的设计不够,这可能和作者不喜欢饼图有关吧,我们通过各种辅助的工具可以对饼图进行更好的修饰。
通过geom_text_repel函数,我们逐步绘制组合型饼图;并通过ggrepel包解决饼图标签相互重叠的情况
# https://stackoverflow.com/questions/52960015/how-to-avoid-label-overlap-in-pie-chart
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"))
library(tidyverse)## -- Attaching packages ---------------------------------------------------------------------------------------------------------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.2.0 √ purrr 0.3.2
## √ tibble 2.1.3 √ dplyr 0.8.3
## √ tidyr 0.8.3 √ stringr 1.4.0
## √ readr 1.3.1 √ forcats 0.4.0## -- Conflicts ------------------------------------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()# install.packages("ggforce")
library("ggforce")
library(ggplot2)
library("ggrepel")
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,
start_angle = lag(end_angle, default = 0),
mid_angle = 0.5*(start_angle + end_angle))## Joining, by = "Group"
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_repel(aes(x = rlabel*sin(mid_angle), y = rlabel*cos(mid_angle), label = paste0 (round(Percent), "%"),
hjust = hjust, vjust = vjust),nudge_x = .3,
segment.size = .7,
show.legend = FALSE) +
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