Ggplot percent sticker

I am trying to add percentage marks to a donut chart, but I could not create a clear representation of the percentage values ​​(rounded and not overlapping)

## my data
library(ggplot2)
col <- c("white", "black", "transparent", "grey", "blue", "yellow", "green", "red", "pink", "orange", "brown")
freq <- c(101, 68, 34, 18, 14, 5, 5, 3, 2, 1, 1)
## create data frame
colour.df <- data.frame(col, freq)
colour.df

## calculate percentage 
colour.df$percentage = colour.df$freq / sum(colour.df$freq)* 100
colour.df = colour.df[rev(order(colour.df$percentage)), ]
colour.df$ymax = cumsum(colour.df$percentage)
colour.df$ymin = c(0, head(colour.df$ymax, n = -1))
colour.df

## reorder colour levels
colour.df$col <- reorder(colour.df$col,
                             new.order = c(10, 1, 9, 5, 2, 11, 4, 8, 7, 6, 3))

Everything is ready for a conspiracy. Perhaps I did it in a peculiar way, since I have to create several donuts for other categories that include color, but I can’t wrap myself around this (facets?).

## DONUNT ##
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(aes(label = paste(percentage,"%"), 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

I played with the following code:

colour.df$percentage = colour.df$freq / sum(colour.df$freq)* 100
## to this
colour.df$percentage = round(colour.df$freq / sum(colour.df$freq)* 100, digits = 1)

But it knocks ymax to 100.1. Applies to 3 decimal points but does not sort the overlap.

I also came across geom_label and geom_text ggplot2: how to add percentage marks to a donut chart and Rounding% Labels on a histogram in ggplot2

. , , , ?

donutchart

+4
1

( , 2) geom_label_repel ggrepel

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

, , ggrepel ( , /):

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

enter image description here

+7

Source: https://habr.com/ru/post/1649676/


All Articles