如何解决ggforce::geom_arc_bar 没有出现在 ggplot2 饼图上
我正在尝试按照 this post 中给出的步骤制作多面饼图。
dat_pies = structure(list(sam_Pop = c("Buganda_Baganda","Cameroon_arabe","Cameroon_Bamun","Cameroon_Dooyaayo","Cameroon_Noni","Congo_Bembe","Congo_Kuni","Congo_Mbere","Congo_Mboshi","Congo_Sundi","Congo_Vili","Dinka_Dinka","Esan_Nigeria","Ethiopia_Agew","Ethiopia_Anuak","Ethiopia_Nuer","Ethiopia_Oromo","Ethiopia_Somali","Gambian_GWD","Ghana_Asante","Ghana_brosa","Ghana_Bulsa","Ghana_Dagaati","Ghana_Ewe","Ghana_Fante","Ghana_Gonja","Ghana_Kasena","Ghana_Sefwi","Ghana_Sisaali","Kikuyu_Ayodo","Luhya_Kenya","Luo_Ayodo","Malawi_Chewa","Malawi_Tumbuka","Malawi_Yao","Mende_SierraLeone","Mozambique_Mozambique","Nigeria_Anang","Nigeria_Efik","Nigeria_Ejagham","Nigeria_EjaghamEkoi","Nigeria_Ibibio","Nigeria_Igbo","Nigeria_Oron","Senegal_Manjo","Senegal_WOF","Somali_Ayodo","Sudan_Jaali","Sudan_Korongo","Sudan_Robatab","Tanzania_Chagga","Wambo_Wambo","Yoruba_Yoruba","Zimbabwe_Shona","Zulu_Zulu"),Country = c("Uganda","Cameroon","Congo","Sudan","Nigeria","Ethiopia","Gambia","Ghana","Kenya","Malawi","SierraLeone","Mozambique","Senegal","Somalia","Tanzania","Namibia","Zimbabwe","SouthAfrica"),Freq = c(91,1,2,3,14,8,47,25,113,203,18,4,30,27,7,17,42,24,13,9,5,2109,20),Freq_total = c(91,31,11,2177,52,373,37,51,15,end_angle = c(6.28318530717959,1.25663706143592,2.51327412287183,5.02654824574367,6.28318530717959,0.608050191017379,3.44561774909848,3.64830114610428,4.25635133712166,4.66171813113324,0.571198664289053,0.0086585006529806,0.12083048667653,0.241660973353061,0.483321946706122,0.604152433382653,0.421125020588444,0.437970021411982,0.45481502223552,0.505350024706133,2.4088351177659,5.82837028494407,6.13158029976775,6.16527030141482,6.21580530388544,5.09447457338885,5.60392203072774,3.32639222144802,4.18879020478639,0.0115446675373075,0.0144308344216343,0.10101584095144,0.170283846175285,0.190487014365573,0.1933731812499,0.196259348134227,0.837758040957278,2.85599332144527,3.42719198573432,6.28318530717959),start_angle = c(6.28318530717959,mid_angle = c(6.28318530717959,6.28318530717959)),row.names = c(NA,-55L),groups = structure(list(
Country = c("Cameroon","SouthAfrica","Uganda","Zimbabwe"),.rows = structure(list(2:5,6:11,14:18,19L,20:29,30:32,33:35,37L,52L,c(13L,38L,39L,40L,41L,42L,43L,44L,53L),45:46,36L,47L,55L,c(12L,48L,49L,50L),51L,1L,54L),ptype = integer(0),class = c("vctrs_list_of","vctrs_vctr","list"))),-18L),class = c("tbl_df","tbl","data.frame"),.drop = TRUE),class = c("grouped_df","tbl_df","data.frame"))
我运行的代码如下:
library(ggplot2)
library(dplyr)
library(ggforce)
rpie = 1
rlabel = 0.6 * rpie
ggplot(dat_pies) +
ggforce::geom_arc_bar(
aes(x0 = 0,y0 = 0,r0 = 0,r = rpie,start = start_angle,end = end_angle,fill = sam_Pop)
) +
geom_text(aes(x = rlabel*sin(mid_angle),y = rlabel*cos(mid_angle),label = Freq),hjust = 0.5,vjust = 0.5) +
coord_fixed() +
scale_x_continuous(limits = c(-1,1),name = "",breaks = NULL,labels = NULL) +
scale_y_continuous(limits = c(-1,labels = NULL) +
facet_wrap(Country~.)
但是,它只生成以下没有饼图的图,我不确定为什么会这样:
解决方法
如评论中所述,您的起始角度等于结束角度。如果这些类似于 Freq
列的饼图,您可以按如下方式重新计算角度,假设 dat_pies
在您的问题中发布:
library(ggplot2)
library(dplyr)
library(ggforce)
rpie = 1
rlabel = 0.6 * rpie
df <- dat_pies %>% group_by(Country) %>%
mutate(start = head(cumsum(c(0,Freq)),-1),end = cumsum(Freq),start = start / max(end) * 2 * pi,end = end / max(end) * 2 * pi,mid = (start + end)/2)
ggplot(df) +
ggforce::geom_arc_bar(
aes(x0 = 0,y0 = 0,r0 = 0,r = rpie,start = start,end = end,fill = sam_Pop)
) +
geom_text(aes(x = rlabel*sin(mid),y = rlabel*cos(mid),label = Freq),hjust = 0.5,vjust = 0.5) +
coord_fixed() +
scale_x_continuous(limits = c(-1,1),name = "",breaks = NULL,labels = NULL) +
scale_y_continuous(limits = c(-1,labels = NULL) +
guides(fill = "none") + # Just added so legend doesn't crowd out panels
facet_wrap(Country~.)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。