同心环形图表示比例

2021-11-19  本文已影响0人  yingyonghui
df <- data.frame(
group = c("Female", "Male", "Child"),
number = c(25, 30, 45)
)

df$percent_value = round(df$number/sum(df$number) * 100)
df$percent_value <- paste0(df$percent_value, "%")
df$cs <- rev(cumsum(rev(df$number)))
df$pos <- df$number/2 + dplyr::lead(df$cs, 1, default = 0)

ggplot(df, aes(x=2, y=number, fill=forcats::fct_inorder(group)))+
    geom_bar(width=0.3, stat="identity") +
    coord_polar("y", start=0) +
    xlim(.2,2.5) +
    ggsci::scale_fill_npg() +
    theme_void() + 
    geom_text(aes(y=pos,label=percent_value),  size=5, col="black") +
    guides(fill=guide_legend(title=NULL))

成图


Rplots.png
上一篇下一篇

猜你喜欢

热点阅读