How to group by multiple variables in python ggplot

Viewed 195

I want to creat a ggplot in python from the output of cayenne package. My dataset looks like this, I have 10 replicates and 3 types of population: S (Susceptible), I (Infected) and R (Recovered):

df

  time rep type  population 
     0   1    S        99.0  
     1   1    S        98.0  
     2   1    S        97.0 
     3   1    S        96.0  
     4   1    S        96.0  
    ..  ...         ...  ..
    95   5    R         NaN  
    96   5    R         NaN  
    97   5    R         NaN  
    98   5    R         NaN  
    99   5    R         NaN  

I want to get a plot like this: Number of Susceptibles, Infecteds and Recovereds over time

In R ggplot there is way to use multiple variables in ggplot group argument, like this:

group = interaction(rep,type)

Is there any equivalent way to do interaction in python ggplot? Finally I ended up combining two variables in a column and use it as a group:

df2 = df
df2['rep'] = df2['rep'].apply(str)
df2 = df2 >> mutate(comb=X.rep+X.type)

print(ggplot(df2)+aes(x='time',y='population',group='comb',colour='type')
      + geom_line() + theme_bw())

is there a better way to do it like in R?

0 Answers
Related