ggpie icon indicating copy to clipboard operation
ggpie copied to clipboard

Adding options for easy pie label order

Open akhst7 opened this issue 1 year ago • 1 comments

@showteeth,

I have a following count table (data.table obj) ;

p
    TCR.clonotype count
            <ord> <int>
 1:    clonotype1    29
 2:    clonotype2    22
 3:    clonotype3    19
 4:    clonotype4    19
 5:    clonotype5    18
 6:    clonotype7    16
 7:    clonotype6    16
 8:    clonotype9    15
 9:    clonotype8    14
10:   clonotype11    14
11:   clonotype12    14
12:   clonotype10    13
13:   clonotype14    12
14:   clonotype13    12
15:   clonotype15    12
16:   clonotype16    11
17:   clonotype17    11 

I created a pie chart by following;

ggpie(p, 
      group_key = "TCR.clonotype", 
      count_type = "count", 
      label_info = c("group" , "ratio"), 
      fill_color = P50[1:17], 
      label_pos = "out", 
      label_type = "horizon", 
      nudge_x = 0.2)

A resulting figure is as follows;

Rplot01

It looks great except that I want a clockwise direction from the largest to the smallest. I could sort p according to the decreasing order of count, as follows;

>p.rev<-p[order(-TCR.clonotype)]
> p.rev
    TCR.clonotype count
            <ord> <int>
 1:   clonotype17    11
 2:   clonotype16    11
 3:   clonotype15    12
 4:   clonotype13    12
 5:   clonotype14    12
 6:   clonotype10    13
 7:   clonotype12    14
 8:   clonotype11    14
 9:    clonotype8    14
10:    clonotype9    15
11:    clonotype6    16
12:    clonotype7    16
13:    clonotype5    18
14:    clonotype4    19
15:    clonotype3    19
16:    clonotype2    22
17:    clonotype1    29

I get a following pie; Rplot

Labelling flipped but not the pie. In order to get the right pie, I have to a following;

> p.rev[, TCR.clonotype :=fct_rev(TCR.clonotype)]
#checking a reversed factor level
> str(p.rev)
Classes ‘data.table’ and 'data.frame':	17 obs. of  2 variables:
 $ TCR.clonotype: Ord.factor w/ 17 levels "clonotype17"<..: 1 2 3 4 5 6 7 8 9 10 ...
 $ count        : int  11 11 12 12 12 13 14 14 14 15 ...
 - attr(*, ".internal.selfref")=<externalptr> 
#plotting needs "guide" to reverse an order of the legend
> ggpie(p.rev, 
+       group_key = "TCR.clonotype", 
+       count_type = "count", 
+       label_info = c("group" , "ratio"), 
+       fill_color = P50[1:17], 
+       label_pos = "out", 
+       label_type = "horizon",  
+       nudge_x = 0.2)
+       guides(fill=guide_legend(reverse = T))

Rplot02

Could you possible add the easier option to reverse the order of pie slices and corresponding legend ?

Thanks again.

akhst7 avatar Feb 18 '23 02:02 akhst7

Thanks for your kind advice!
ggpie is designed to set through factor levels to deal with diverse needs. In your example, you only need to add single-line code to meet your needs, so it's not very complicated in my opinion.

YB

showteeth avatar Feb 18 '23 05:02 showteeth