How can I build two series from different data frames against each other using ggplot2 in R without creating a new data frame?

Suppose I have two data frames

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

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

plot(df1$x,df2$x)

From what I can say 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 it will be slower (for me) than creating a new data frame, it is difficult to read and can lead to an increase in errors (deleting an incorrect data frame, writing to the desired data frame, forgetting to remove excess clutter, etc.). Do I really need to create a separate data frame to accommodate the data that is already there? Why does the first line of work work, even if it displays only one of the data frames under "data", and the second line does not work?

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 Conclusion

+4
source share
2 answers

Any line from 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))

( , ). , ggplot() data.frame . ( , , ).

+1

, .

qplot(x = df1$x, y = df2$x). , ?qplot, qplot , .

0

All Articles