Search code examples
rggplot2pie-chart

ggplot - Making pie graph


I have a df (named: cant_masivos_trim) as follows:

            Descripcion Freq
    Cargos Jubilaciones 2185
      Faltantes de Caja  470
        ATM Diferencias  201
   Previsiones Legales   34
  Gastos Corresponsalía   22
          Multas SICORE   19
               Sumarios   17
            ATM Fraudes   10
           Multas ANSeS    7
            Multas AFIP    5

I want to create a pie graph with ggplot2, and I have a problem with the labels as you can see in the image.

enter image description here

I don't know why the labels are not in the correct place and I can't figure it out.

The code I'm using:

pmas <- ggplot(cant_masivos_trim, aes(x=1, y=Freq, fill=Descripcion)) +
        geom_bar(stat="identity") +
        ggtitle(paste("Cantidad de Reportes - Carga Masiva"))
pmas <- pmas + coord_polar(theta='y')
pmas <- ggplot(cant_masivos_trim, aes(x=1, Freq, fill=Descripcion)) +
        ggtitle(paste("Cantidad de Reportes - Carga Masiva")) +
        coord_polar(theta='y')
pmas <- pmas + geom_bar(stat="identity", color='black') + guides(fill=guide_legend(override.aes=list(colour=NA)))
pmas <- pmas + theme(axis.ticks=element_blank(),  # the axis ticks
          axis.title=element_blank(),  # the axis labels
          axis.text.y=element_blank()) # the 0.75, 1.00, 1.25 labels.
y.breaks <- cumsum(cant_masivos_trim$Freq) - cant_masivos_trim$Freq/2
pmas <- pmas +
    # prettiness: make the labels black
    theme(axis.text.x=element_text(color='black')) +
    scale_y_continuous(
        breaks=y.breaks,   # where to place the labels
        labels= (paste(cant_masivos_trim$Freq, percent(cant_masivos_trim$Freq/sum(cant_masivos_trim$Freq)), sep='\n'))) # the labels

Can anyone help me? Thanks!


Solution

  • This has to do with the order the slices of the pies are plotted by default. It is easiest to see what is going on by looking at the bar chart (prior to coord_polar). A ggplot2 bar chart plots from top to bottom based on the order of the levels of your Descripcion factor.

    To calculate the breaks as you have you need to put your dataset in order by Descripcion before calculating cumulative frequencies. To match the default ggplot2 order, you order by the reverse of the variable.

    cant_masivos_trim = cant_masivos_trim[rev(order(cant_masivos_trim$Descripcion)), ]
    

    Once that is done, calculate the breaks based on the cumulative frequencies and center them as before.

    y.breaks = cumsum(cant_masivos_trim$Freq) - cant_masivos_trim$Freq/2
    
    ggplot(cant_masivos_trim, aes(x  =1, y = Freq, fill = Descripcion)) +
        geom_bar(stat="identity") +
        coord_polar(theta = "y") +
        scale_y_continuous(breaks = y.breaks,   # where to place the labels
            labels = (paste(cant_masivos_trim$Freq, 
                         scales::percent(cant_masivos_trim$Freq/sum(cant_masivos_trim$Freq)), sep='\n'))) +
        theme(legend.position = "none",
             axis.ticks=element_blank(),  
             axis.title=element_blank(),  
             axis.text.y=element_blank())
    

    enter image description here