0

Not able to located complete Yhat doc to answer this question, using the R version of ggplot I've attempted to iteratively back into a solution.

What is the correct syntax for annotating a Python ggplot plot with text in generally, more specifically using a variable from Statsmodels (everything works except the last line of this code block below)?

from ggplot import *
    ggplot(aes(x='rundiff', y='winpct'), data=mlb_df) +\
    geom_point() + geom_text(aes(label='team'),hjust=0, vjust=0, size=10) +\
    stat_smooth(method='lm', color='blue') +\
    ggtitle('Contenders vs Pretenders') +\
    ggannotate('text', x = 4, y = 7, label = 'R^2')

Thanks.

user3741230
  • 307
  • 1
  • 2
  • 11
  • There is currently no "ggplot-way" to annotate a plot ([Bug](https://github.com/yhat/ggplot/issues/337)). You can do it in matplotlib by getting the matplotlib figure: `g = ggplot(...) + ...; figure = g.draw()`. For annotating in matplotlib see http://matplotlib.org/users/annotations_intro.html – Jan Katins Jul 04 '14 at 09:14
  • Thanks,@JanSchulz for the response. The typical use case is to support adding the R-squared, P-value, & the model equation to the plot. FYI, Seaborn plot supports R2 & P-values annotations but doesn't directly support Statsmodels as well as GGPlot. – user3741230 Jul 05 '14 at 16:27
  • ggplot will probably not support directly printing such information (maybe if someone adds a `annotate_statsmodels(...)` to an extra package or so...), but if you want to have such a thing in seaborn, just ask there :-) – Jan Katins Jul 05 '14 at 18:41
  • @JanSchulz: It's been previously raised by others as a request several months ago for ggplot. From your reply on 'someone' is ggplot in maintenance mode only & no further active development? – user3741230 Jul 07 '14 at 16:57
  • 1
    No, ggplot is maintained and worked on, just not as fast as one would like :-) The "annotate_statsmodel" is as far as I understand not in the scope of ggplot, which tries to be as near as possible to ggplot2, which AFAIK has not "annotate_lm". A more generic "annotate" is of course needed but up to now noone has found the time :-/ – Jan Katins Jul 09 '14 at 20:48

1 Answers1

0

You can use geom_text as a provisional solution

from ggplot import *
import pandas as pd
    dataText=pd.DataFrame.from_items([('x',[4]),('y',[7]),('text',['R^2'])])
    ggplot(aes(x='rundiff', y='winpct'), data=mlb_df) +\
    geom_point() + geom_text(aes(label='team'),hjust=0, vjust=0, size=10) +\
    stat_smooth(method='lm', color='blue') +\
    ggtitle('Contenders vs Pretenders') +\
    geom_text(aes(x='x', y='y', label='text'), data=dataText)
ilciavo
  • 3,069
  • 7
  • 26
  • 40