question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Facetted grouped boxplot

See original GitHub issue

In the seaborn plotting library exists catplot() that allows facetted grouped boxplots:

import pandas as pd

## the following code needs the "xlrd" library installed for reading Excel files.
## alternatively, you can use the attached data.csv which contains the same data using:
# dataProp = pd.read_csv('https://github.com/altair-viz/altair/files/4565572/data.zip')

link = 'https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/812408/Gas_and_Electric_Consumption_Multiple_Attributes_Table_2019.xlsx'
data = pd.read_excel(link,'2017 Consumption')
dataGas = data.loc[data['Gas present']=='Yes',]
dataProp = dataGas.loc[dataGas['Electricity type']=='Standard',]
import seaborn as sns

sns.catplot(x='Number of bedrooms',
            y='Median - Gas', 
            col='Region', 
            hue='Property age',
            kind="box",
            data=dataProp,
            col_wrap=4,
            hue_order=['Pre 1919','1919-44','1945-64','1965-1982','1983-92','1993-99'])

grafik

I tried to replicate this using altair, but the boxplots get stacked on themselfs:

import altair as alt

alt.Chart(dataProp).mark_boxplot().encode(
    x='Number of bedrooms:O',
    y='Median - Gas',
    color=alt.Color('Property age:O',
                    sort=['Pre 1919','1919-44','1945-64','1965-1982','1983-92','1993-99']),
    facet=alt.Facet('Region:N', 
                    columns=4),
).interactive()

visualization

I tried to combine facet=... and column=... parameters in encode() and tried my hands on the repeat() method without success. Is there a way to show the grouped boxplots next to each other like in seaborn’s catplot()?

Thank you for you help.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
jakevdpcommented, May 3, 2020

No, there’s no way to do this yet in Altair/Vega-Lite (the way to make grouped boxplots is to use facet, and vega-lite does not support facets of facets).

A future vega-lite release will hopefully include an offset encoding which will make this type of chart possible.

2reactions
joelostblomcommented, Aug 4, 2022

@VietTralala Sorry I should have mentioned that this has been added in the development version on GitHub, but it has not yet been released. As you noticed, you can install directly from GitHub to get access to the X/YOffset encodings. I am not sure why it is not working for you, but you should not have to do anything special with the import, so try importing as usual import altair as alt. It is important that you close down all notebooks (or clear their output) and the reopen jupyterlab. If there is a plot open that has been created by an with an old version of vegalite, that can create issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

R ggplot: grouped boxplot using group-variable in facet
Create two seperate graphs and combine them with a shared legend. Create two different y-axes in one graph; Use facet grid. Since 1....
Read more >
Grid of clustered/grouped boxplots - bioST@TS
In this tutorial, we will see how to make a grid of clustered/grouped boxplots using facet_wrap() and facet_grid() . Such a grid may...
Read more >
ggplot2 facet : split a plot into a matrix of panels - Easy Guides
The facet approach partitions a plot into a matrix of panels. Each panel shows a different subset of the data. This R tutorial...
Read more >
Grouped boxplot with ggplot2 - The R Graph Gallery
A grouped boxplot is a boxplot where categories are organized in groups and subgroups. Here we visualize the distribution of 7 groups (called...
Read more >
Chapter 5 Using ggplot2 | Data Science with R - GitHub Pages
Produce scatter plots, boxplots, and time series plots using the ggplot2 package ... First we need to group the data and count records...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found