Search code examples
pythonggplot2facet-wrappython-ggplot

yhat ggplot facet_wrap type error


I have a pandas dataframe that I read in via txt file:

training = pd.read_csv('training_data.txt')

These are the columns:

>> print training.columns.values`  
['segment' 'cookie_id' 'num_visits' 'num_page_views']

I'm interested in a graph that shows the density of num_page_views by segment, like so:

plot = ggplot(training, aes(x='num_visits')) + geom_density()
        + xlim(0,20) + facet_wrap( ~ 'segment')  
print plot

And it gives the following error:

TypeError Traceback (most recent call last) in () 1 ----> 2 plot = ggplot(training, aes(x='num_visits')) + geom_density() +xlim(0,20) +facet_wrap( ~ 'segment') 3 print plot

TypeError: bad operand type for unary ~: 'str'


Solution

  • In yhat ggplot, the facet option does not take a "~". You can find this is in the documentation (copied here for convenience):

    import pandas as pd
    
    meat_lng = pd.melt(meat, id_vars=['date'])
    
    p = ggplot(aes(x='date', y='value'), data=meat_lng)
    p + geom_point() + \
        stat_smooth(colour="red") + \
        facet_wrap("variable")
    
    p + geom_hist() + facet_wrap("color")
    
    p = ggplot(diamonds, aes(x='price'))
    p + geom_density() + \
        facet_grid("cut", "clarity")
    
    p = ggplot(diamonds, aes(x='carat', y='price'))
    p + geom_point(alpha=0.25) + \
        facet_grid("cut", "clarity")