7

I am trying to get data from two separate dataframes onto the same scatterplot. I have seen solutions in R that use something like:

ggplot() + geom_point(data = df1, aes(df1.x,df2.y)) + geom_point(data = df2,aes(df2.x, df2.y))

But in python, with the ggplot module, I get errors when I try to use ggplot() with no args. Is this just a limitation of the module? I know I can likely use another tool to do the plotting but I would prefer a ggplot solution if possible.

My first data frame consists of Voltage information every 2 minutes and temperature information every one hour, so combining the two dataframes is not 1 to 1. Also, I would prefer to stick with Python because the rest of my solution is in python.

Eduard Florinescu
  • 16,747
  • 28
  • 113
  • 179
scld
  • 173
  • 1
  • 6

1 Answers1

12

Just giving one dataframe as argument for ggplot() and the other inside the second geom_point declaration should do the work:

ggplot(aes(x='x', y='y'), data=df1) + geom_point() + 
       geom_point(aes(x='x', y='y'), data=df2)

(I prefer using the column name notation, I think is more elegant, but this is just a personal preference)

prl900
  • 4,029
  • 4
  • 33
  • 40
  • That works, thank you. So, the 'base' ggplot needs its own dataset and subsequent geom_point() usages can add different datasets. Is that the gist of it? – scld May 21 '14 at 13:30
  • It also surprised me the first time I realised about that. I don't really know the reason behind not allowing an empty ggplot() and including data in subsequent geom_point(). Maybe one of the main developers of the project could answer this better. – prl900 May 22 '14 at 06:43
  • I think the error on an empty `ggplot()` call comes from the time where it wasn't allowed to specify a new df in each geom. – Jan Katins Jan 06 '15 at 21:47
  • 1
    Doesn't seem to work for me with ggplot 0.11.2. I get `ValueError: First argument must be a sequence`. The first `ggplot(...)+geom_point()` part is working, the second `geom_point(aes(...), data = df2)` part is also working, but I can't `+` the two – jf328 Sep 04 '16 at 20:45