Search code examples
rggplot2pie-chartlabel

Order piechart pieces


Although I am aware there are a few posts on this matter I still have not been able to figure out how to order the wedgesof the pie chart and match them with their according label.

I have the following dataset (called plotter data):

Code     Percentage
a          21,43
b          21,43
c          3.58
d          21,43
e          3.58
f          14.29
g          3.58
h          7.14
i          3.58

Following the ideas from this post (adding-percentage-labels-on-pie-chart-in-r) I have developed the following code:

library(ggplot2)
library(dplyr)
library(ggforce)
library(scales)
library(ggrepel)

plotter.data<-read.csv("plotter analysis.csv",header=T)
plotter.data$Code<-factor(plotter.data$Code)
plotter.data$Percentage<-round(plotter.data$Percentage, 2)

plotter.data<-plotter.data %>%
arrange(desc(Code))%>%
mutate(text_y = cumsum(Percentage) - Percentage/2)
plotter.data

plotter.plot<ggplot(data=plotter.data,aes(x=1,y=Percentage,fill=reorder(Code,-Percentage)))+
geom_bar(stat = "identity",color='black')+
geom_label_repel(aes(label = percent(Percentage/100),y = text_y), size=3, show.legend = F, nudge_x = 1)+
guides(fill = guide_legend(title = "Code"))+
coord_polar("y",start=0)+ 
labs(x='',y='',title='Code Plotter systems')+
theme(plot.title=element_text(hjust=0.5,face='bold',size=16))+
theme(axis.text.x=element_blank(),
 axis.text.y=element_blank(),
 legend.title=element_text(size=12,face='bold'))
plotter.plot

Which give me this figure enter image description here

As you can see in the plot, the order of the wedges is correct, since I reorder my levels with reorder() in the ggplot code, but however, my labels (text_y) do not follow the same order. I have tried to order the dataset previous to making the ggplot and still does not work. I was wondering if you could let me know why the labels appear in a different order and how to fix it.

I have also tried to calculate text_y prior and after ordering my dataframe but once using ggplot later the labels still appear in a different position.

Thanks a lot for any advice


Solution

  • Playing around I manage to solve my answer. Once plotting the ggplot and reordering my wedges according to the factor Code, I have to reorder them using the label positions (text_y) rather than the value of the wedge.

    So:

    1- Order you df according to the order you want to show the factor in the data frame

     plotter.data <- plotter.data[order(-plotter.data$Percentage),] 
    

    2-Calculate the position of the labels:

    plotter.data$text_y <- 100-(cumsum(plotter.data$Percentage) -plotter.data$Percentage/2)
    

    Use reorder() on ggplot using the location of your labels as the ordering variuable:

    reorder(Code,-text_y)
    

    Thus:

    plotter.plot<ggplot(data=plotter.data,aes(x=1,y=Percentage,fill=reorder(Code,-text_y)))+
        geom_bar(stat = "identity",color='black')+
        geom_label_repel(aes(label = percent(Percentage/100),y = text_y), size=3, show.legend = F, nudge_x = 1)+
        guides(fill = guide_legend(title = "Code"))+
        coord_polar("y",start=0)+ 
        labs(x='',y='',title='Code Plotter systems')+
        theme(plot.title=element_text(hjust=0.5,face='bold',size=16))+
        theme(axis.text.x=element_blank(),
         axis.text.y=element_blank(),
         legend.title=element_text(size=12,face='bold'))
        plotter.plot 
    

    enter image description here