Is there a way to group labels when using plotnine's facet_grid
function?
For example, take the following DataFrame (I made the example up such that you can reproduce my problem. In real my DataFrame is much bigger but the structure is almost identical):
df_test = pd.DataFrame(data= {'rate_1': [0.0,0.1,0.2,0.3,0.0,0.1,0.2,0.3,0.3,0.2,0.1,0.0],
'rate_2': [0.0,0.1,0.2,0.3,0.0,0.1,0.2,0.3,0.3,0.2,0.1,0.0],
'rate_3':[0.0,0.1,0.2,0.3,0.0,0.1,0.2,0.3,0.3,0.2,0.1,0.0],
'rate_4': [0.0,0.1,0.2,0.3,0.1,0.2,0.3,0.0,0.2,0.3,0.0,0.1],
'samples': [50000, 100000, 50000, 100000,50000, 100000, 50000, 100000,50000, 100000, 50000, 100000],
'model': ['model 1', 'model 2', 'model 3', 'model 4','model 1', 'model 2', 'model 3', 'model 4','model 1', 'model 2', 'model 3', 'model 4'],
'metric': [0.5,0.4,0.3,0.2,0.5,0.4,0.3,0.2,1,0.3,0.4,0.3]})
I created the following function to plot the absolute metric depending on the rates, sample size and model:
# Plotting
my_plot = ggplot(data=df_test, mapping=aes('rate_1', 'rate_2', fill='metric'))
my_plot += geom_tile()
my_plot += theme(axis_text_x=element_text(rotation=90), figure_size=(16, 8), strip_background_x=element_text(width=1.))
my_plot += scale_x_continuous(breaks=[.0, .1, .2, .3, .4, .5])
my_plot += scale_y_continuous(breaks=[.0, .1, .2, .3, .4, .5])
my_plot += facet_grid('rate_3 + samples ~ model + rate_4', labeller=label_value)
my_plot += scale_fill_gradient2(midpoint=0, low='blue', mid="white", high="red")
my_plot
The resulting plot looks like:
Is there a way to group model names (model 1, model 2, model 3, model 4) and rate_4 when labeling the facet_grid? I'm looking for a result such that the 'columns' are named like the following (I used Excel for the illustration):