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

Categorical ordering, it's not respected for Boxplot and violin plot #746

Closed
icedevil2001 opened this issue Mar 31, 2023 · 4 comments
Closed
Assignees
Labels

Comments

@icedevil2001
Copy link

icedevil2001 commented Mar 31, 2023

I am trying to order the X axis in chronological order, which has been set as a categorical variable. When I perform a facet plot, I find that the X-axis does not respect the categorical ordering when one or more chromosome arms have missing values for the entire chromosome arm.
I am very new to the Lets-plot library and have never used ggplot

Below is a minimal example

import pandas as pd
n = 100
np.random.seed(42)
chrom_order = ['chr1', 'chr2', 'chr4', 'chr5']

chrom = np.random.choice(chrom_order, size=n)
arm = np.random.choice(['p',"q"], size=n)
y = np.random.normal(size=n)

df = pd.DataFrame({'chrom': chrom, 'y': y, 'arm': arm})
df['chrom'] = pd.Categorical(df.chrom, categories=chrom_order, ordered=True)

df.loc[(df.chrom == "chr1")& (df.arm == 'p') , 'y'] = np.NAN
df.dropna(subset='y', inplace=True)
df = df.sort_values("chrom")
p3 = (
    ggplot(df, aes(x="chrom", y='y')) 
    + geom_boxplot()
    + facet_grid(y='arm')
    )

p3.show()

image

@alshan alshan added this to the 2023Q2 milestone Mar 31, 2023
@alshan alshan added the ASAP label Mar 31, 2023
@alshan
Copy link
Collaborator

alshan commented Mar 31, 2023

Related #679

@alshan alshan self-assigned this Mar 31, 2023
@alshan
Copy link
Collaborator

alshan commented Apr 4, 2023

Thank you for reporting this issue.
Meanwhile, you have couple of other options to achieve the required order of categories along the x-axis.

  1. Use x-scale limits: p + scale_x_discrete(limits=chrom_order)
  2. Order x-axis categories alphabetically:
from lets_plot.mapping import *

ggplot(df, aes(x=as_discrete("chrom", order=1), y='y')) + geom_boxplot() + facet_grid(y="arm")

@icedevil2001
Copy link
Author

Super!! Thank you for the quick response and both solutions work excellently!

@alshan
Copy link
Collaborator

alshan commented May 10, 2023

Fixed in v3.2.0

@alshan alshan closed this as completed May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants