Having two kinds of graphics in same ggplot

361 views Asked by At

I'm experimenting with ggplot's port for Python. I can do very basic graphics without too much trouble, but it gets really complicated for me when trying some more fancy.

In this case, I want to plot 3 variables A, B and C at the same time along 'x', which can be done with the simple code below. However, I'd like to display a 4th variable (called 'Back') in the background of the display, as a series of rectangular shaped colours (using a colour gradient of sort). Something that could look a little bit like this admitibly bad photoshop mockup : https://www.dropbox.com/s/euqek8e6nebmau6/Capture%20d%27%C3%A9cran%202013-12-02%2014.29.16.png

Is there a straightforward way to accomplish this kind of graphic with ggplot for python?

If it could help answer the question, here is the xls file containing the data: https://dl.dropboxusercontent.com/u/73950/B.xls

And here is the code:

from ggplot import *
import pandas as pd
import statsmodels

xl = pd.ExcelFile('B.xls')
df = xl.parse("sheet1")

df_lng = pd.melt(df[['x', 'A', 'B', 'C','Back']], id_vars='x')
print ggplot(aes(x='x', y='value', colour='variable'), data=df_lng) + geom_line()
plt.show(1)

Oh, and here is a post that I think could help do the trick. This was done in R though... https://gist.github.com/dsparks/3866629

1

There are 1 answers

5
shadow On BEST ANSWER

Here's the R code to do this. Don't know how to do it with Python, but it appears that this would be similar.

ggplot(aes(x=x), data=df_lng) + geom_line(aes(y=value, colour=variable), size=1.3) +
  geom_rect(aes(fill=factor(Back), xmin=x, xmax=x+1, ymin=0, ymax=max(value)*1.07), alpha=.1) + 
  scale_fill_discrete(guide="none")