plotnine icon indicating copy to clipboard operation
plotnine copied to clipboard

Is it possible to access facetted data?

Open ootsby opened this issue 5 years ago • 2 comments

Apologies for asking a question via an issue. Let me know if there's a more appropriate route you'd prefer.

I'm trying to create a facetted plot using facet_wrap that includes a geom_polygon that renders a shaded confidence interval around a fitted model. As is, to achieve this I would need to be able to access the facet filtered data to generate the polygon dataframe and I can't see a way to do this. I suspect that I'm either missing something obvious or I've ended up down a dead-end and need to rethink my approach entirely. Any guidance would be appreciated.

Psuedo-code illustration:

    p = ( 
        p9.ggplot(
            melted_df,
            p9.aes(
                x="x",
                y="y",
            ),
        )
    
    + p9.facet_wrap('variable', scales='free', ncol=2)
    + p9.geom_point()

    ##confidence interval polygon
    + p9.geom_polygon(
            data=get_ci_df( ***would want to pass facet data in here*** ),
            mapping=p9.aes(x="x", y="ci", size="0.6"),
            fill="grey",
            alpha=0.3,
        )
    )

ootsby avatar Mar 25 '21 11:03 ootsby

I think you want to have your own stat, i.e stat_my_ci! Look at a simple stat like stat_qq. Implement your ci logic in the compute_group method and return a dataframe with the right columns.

has2k1 avatar Mar 25 '21 12:03 has2k1

Aha! I took some wrong turns (I've never really used stats with ggplot so I wasn't clear on how the mapping is meant to be specified across the geom and stat) but I have it working now.

Thanks for the pointer on this problem and the library overall.

ootsby avatar Mar 26 '21 14:03 ootsby