Skip to content

facet_grid of data containing empty combinations (NaN) causes error #664

Closed

Description

@kasuskasus1

I have a set of data in one dataframe which has x and y values to be plotted in separate plots using x_plot and y_plot columns. The problem is that some of x_plot and y_plot combinations contain only np.nan values. However, I need a grid which will show all plots including empty ones for NaN combinations.

So when plotting

(
    ggplot(data, aes(x='x', y='y')) +
    geom_line() +
    facet_grid('x_plot ~ y_plot')
)

everything is fine, but when I try:

(
    ggplot(data, aes(x='x', y='y')) +
    geom_line() +
    facet_grid('x_plot ~ y_plot', scales="free_y")
)

I get ValueError: Axis limits cannot be NaN or Inf, while using R this works without any problems.

Activity

  1. changed the title [-]facet_grid of data containing empty combinations (NaN) causes [/-] [+]facet_grid of data containing empty combinations (NaN) causes error[/+] on Feb 17, 2023
  2. has2k1 commented on Feb 20, 2023

    @has2k1
    Owner

    Please create a minimal reproducible example for the issue.

  3. added
    ReprexNeeds a Minimal Reproducible Example
    on Feb 20, 2023
  4. kasuskasus1 commented on Feb 22, 2023

    @kasuskasus1
    Author

    Running the following code in R:

    library(ggplot2)
    d <- data.frame(
      x_plot = c('XA','XA','XA','XA','XA','XA','XA','XA','XA','XB','XB','XB','XB','XB','XB','XB','XB','XB'),
      y_plot = c('YA','YA','YA','YB','YB','YB','YC','YC','YC','YA','YA','YA','YB','YB','YB','YC','YC','YC'),
      x=c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3),
      y=c(5,6,7,NA,NA,NA,1,2,3,4,5,6,NA,NA,NA,1,2,3)
    )
    ggplot(d, aes(x=x, y=y))+
      geom_line()+
      facet_grid('y_plot ~ x_plot', scales='free_y')
    

    results in the following plot:
    image

    At the same time identical code in python:

    import numpy as np
    import pandas as pd
    from plotnine import *
    
    d = {
            'x_plot':pd.Series(['XA','XA','XA','XA','XA','XA','XA','XA','XA','XB','XB','XB','XB','XB','XB','XB','XB','XB']),
            'y_plot':pd.Series(['YA','YA','YA','YB','YB','YB','YC','YC','YC','YA','YA','YA','YB','YB','YB','YC','YC','YC']),
            'x':[1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3], 
            'y':[5,6,7,np.NAN,np.NAN,np.NAN,1,2,3,4,5,6,np.NAN,np.NAN,np.NAN,1,2,3],
            
         }
    df = pd.DataFrame(data=d)
    (
        ggplot(df, aes(x='x', y='y'))+
        geom_line()+
        facet_grid('y_plot ~ x_plot', scales='free_y')
    )
    

    results in the following output:

    ValueError: Axis limits cannot be NaN or Inf
    

    My question is - is there a way to treat NA (or np.NAN) values in python using your lib in the same way as in R just leaving NA combinations of factors with empty plots?

  5. added a commit that references this issue on May 9, 2023
    1dca172
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ReprexNeeds a Minimal Reproducible Example

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions