pythonggplot2python-ggplotplotnine

Plotnine: How to use geom_col and geom_text to display mean by group


I have a dataset that I am using geom_col to visualize using stat='summary'. I can't quite figure out how to use geom_text() to display the mean values. Here is the code and plot I am able to produce:

import pandas as pd
import numpy as np
from plotnine import *

df = pd.DataFrame({'team': {4: 'Tampa Bay Rays',
  8: 'Tampa Bay Rays',
  85: 'Tampa Bay Rays',
  89: 'Tampa Bay Rays',
  1277: 'League',
  1393: 'League',
  3544: 'League',
  3660: 'League'},
 'inning': {4: 1, 8: 1, 85: 2, 89: 2, 1277: 1, 1393: 1, 3544: 2, 3660: 2},
 'value': {4: 3.0,
  8: 1.0,
  85: 1.0,
  89: 0.0,
  1277: 2.0,
  1393: 0.0,
  3544: 2.0,
  3660: 0.0}})

(ggplot(df, aes(x='inning', y='value', fill='team'))
 + geom_col(stat='summary', position='dodge')
 + scale_x_continuous(breaks=np.arange(1,10,1)))

enter image description here

I am looking to add these labels via geom_text(): enter image description here Those labels are essentially the result of this code, which I was hoping to avoid, and just have plotnine calculate: df[['team','inning','value']].groupby(['team','inning']).mean()


Solution

  • You need to give geom_text the same statistics and position settings as geom_col.

    df = pd.DataFrame({'team': {4: 'Tampa Bay Rays',
      8: 'Tampa Bay Rays',
      85: 'Tampa Bay Rays',
      89: 'Tampa Bay Rays',
      1277: 'League',
      1393: 'League',
      3544: 'League',
      3660: 'League'},
     'inning': {4: 1, 8: 1, 85: 2, 89: 2, 1277: 1, 1393: 1, 3544: 2, 3660: 2},
     'value': {4: 3.0,
      8: 1.0,
      85: 1.0,
      89: 0.0,
      1277: 2.0,
      1393: 0.0,
      3544: 2.0,
      3660: 0.0}})
    
    (ggplot(df, aes(x='inning', y='value', fill='team'))
     + geom_col(stat='summary', position='dodge')
     + geom_text(
         aes(label=after_stat("y"), color="team"),
         stat="summary",
         position=position_dodge(width=0.9),
         va="bottom"
       )
     + scale_x_continuous(breaks=np.arange(1,10,1))
     + scale_color_discrete(l=.4) 
    )
    

    enter image description here