Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding options for easy pie label order #5

Open
akhst7 opened this issue Feb 18, 2023 · 1 comment
Open

Adding options for easy pie label order #5

akhst7 opened this issue Feb 18, 2023 · 1 comment

Comments

@akhst7
Copy link

akhst7 commented Feb 18, 2023

@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.

@showteeth
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants