ggplot donut chart percentage labels

核能气质少年 提交于 2019-12-05 07:44:55

For rounding we could replace percentage with round(percentage,2) and for the overlap we could use geom_label_repel from the ggrepel package

library(ggrepel)
donut = ggplot(colour.df, aes(fill = col, ymax = ymax, ymin = ymin, xmax = 100, xmin = 80)) +
    geom_rect(colour = "black") +
    coord_polar(theta = "y") + 
    xlim(c(0, 100)) +
    geom_label_repel(aes(label = paste(round(percentage,2),"%"), x = 100, y = (ymin + ymax)/2),inherit.aes = F, show.legend = F, size = 5)+
    theme(legend.title = element_text(colour = "black", size = 16, face = "bold"), 
        legend.text = element_text(colour = "black", size = 15), 
        panel.grid = element_blank(),
        axis.text = element_blank(),
        axis.title = element_blank(),
        axis.ticks = element_blank()) +
    annotate("text", x = 0, y = 0, size = 15, label = "Micro")
    donut

Note that there are warnings produced with ggrepel (Also I skipped the reorder colour levels step, feel free to correct/comment):

In min(x) : no non-missing arguments to min; returning Inf
In max(x) : no non-missing arguments to max; returning -Inf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!