Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python ggplot cannot facet wrap with two lines

Tags:

python

ggplot2

I'm trying to plot a Pandas DataFrame with python ggplot. The DF has four columns: Date, Val1, Val2, Condition. What I want is a line plot with both Val1 and Val2 as y-coordinate, Date as the x coordinate and facet wrap with Condition.

I can do either of these in isolation but I'm not able to do both things at once. Here is what I tried:

ggplot(DF, aes(x='Date', y = 'Val1')) + geom_line()+ geom_line(aes(x='Date', y = 'Val2'), colour='red') + facet_wrap('Condition')

However, this only shows the facet_wrap plots of Val1 but coloured red. If I omit the facet_wrap, it plots both line plots perfectly.

What am I doing wrong?

Any help will be much appreciated. Thanks in advance!

like image 600
Pablo Avatar asked May 03 '26 23:05

Pablo


1 Answers

If you restructure your dataframe to instead have 3 columns instead (Date, Val, Condition) it will split into two plots by condition.

ggplot(DF, aes(x='Date', y = 'Val1')) + geom_line() + facet_wrap('Condition')

If you look at the dataframe used in the examples found here https://yhat.github.io/ggpy/notebook.html?page=build/docs/examples/Faceting%20Basics.html you'll see that there is only one column for values.

like image 196
Haley Avatar answered May 05 '26 13:05

Haley