4

Suppose that I have two data frames

df1 = data.frame(x=1:10)
df2 = data.frame(x=11:20)

and I want a scatter plot with these two series defining the coordinates. It would be simple to do

plot(df1$x,df2$x)

From what I can tell so far about ggplot2, I could also do

df = data.frame(x1 = df1$x, x2 = df2$x)
ggplot(data = df, aes(x=x1, y=x2)) + geom_point()
rm(df)

but that would be slower (for me) than not creating a new data frame, is hard to read, and could lead to increased mistakes (deleting the wrong data frame, writing over a needed data frame, forgetting to remove the excess clutter, etc.). Do I really need to create a separate data frame just to house the data that are already there? Why does the first line of the following work even though it only lists one of the data frames under "data" while the second line does not?

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

Here is an example image of basically what I want: Desired output

5
  • 1
    How about ggplot(data.frame(x=df1$x, y=df2$x), aes(x,y)) + geom_point()?
    – eipi10
    Commented Aug 31, 2015 at 23:45
  • You could do the following, ggplot() + geom_point(aes(x=df1$x, y=df2$x)), ggplot(data=NULL, aes(x=df1$x, y=df2$x)) + geom_point(), if you insist on not creating another data.frame
    – chappers
    Commented Aug 31, 2015 at 23:46
  • All three of those work.
    – randy
    Commented Aug 31, 2015 at 23:54
  • 1
    @randy you know that ggplot is just making another data frame anyway, right?
    – hrbrmstr
    Commented Sep 1, 2015 at 1:31
  • Sure, but coming from using plot(df1$x,df2$x) and trying to convert my code to make use of ``ggplot` is a nightmare that would be so much worse if I needed to take each of my hundred 4000x100 dimension data frames corresponding to different experiments and then manage thousands more built just for individual plots.
    – randy
    Commented Sep 1, 2015 at 17:37

2 Answers 2

1

Any line of the following (all taken from comments) should work:

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

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

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

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

I prefer the last line (taken from a comment that was deleted). As mentioned in comments on the question, ggplot() will create a data.frame anyway. What these solutions do is permit the user to ignore this aspect of data management somewhat (admittedly in ways that some users would find abhorrent).

3
  • I have the same issue @randy. But I have multiple columns from each dataframes that I need to put on the same plot. How should I plot that?
    – Jack
    Commented Apr 14, 2017 at 20:42
  • Can you clarify your question, @Jack? This might need to be a new question.
    – randy
    Commented Apr 15, 2017 at 21:31
  • Jack, you can specify data and aesthetic arguments for each geom separately.
    – randy
    Commented Jun 23, 2019 at 0:47
0

This was going to be a comment, but I'm not reputable enough yet.

You could also try qplot(x = df1$x, y = df2$x). Note that from ?qplot that qplot will create a data frame for you from the inputs provided, if the data argument is left unset.

1
  • ggplot also creates a data frame as well even when you specify a data frame. qplot is a junior demon spawn in ggplot that rly shld not be used (said fact is emphasized in the upcoming update to the official ggplot2 book).
    – hrbrmstr
    Commented Sep 1, 2015 at 1:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.