Skip to content

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

@icedevil2001

Description

@icedevil2001

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions