2

I have two data sets. I would like to plot both data sets on the same scatter chart. How can I do this easily with R? I appreciate your suggestions.

  x1    y1   x2     y2
42.39   2.1 53.05   8.4
38.77   2.1 43.81   2.6
44.43   2.6 42.67   2.4
42.37   2   41.74   3.4
48.79   3.6 42.99   2.9
46.00   2   
53.71   2.7 
47.38   1.8 
43.75   3.1 
46.95   3.9 
alan
  • 25
  • 2
  • 1
    keywords: `melt` and `ggplot2` –  Nov 09 '15 at 06:59
  • Possible duplicate of [Plot two graphs in same plot in R](http://stackoverflow.com/questions/2564258/plot-two-graphs-in-same-plot-in-r) – KannarKK Nov 09 '15 at 09:02
  • @KannarKK I think, my question is different. because x1 and x2 have different values. I need to plot x1 vs. y1 and x2 vs.y2. – alan Nov 10 '15 at 09:58

2 Answers2

2

We change the name of column x2 in the df2 to match the name of the corresponding column x1in df1. Then we create a unique data frame df with rbind. We use melt from the reshape2 package to convert data to a long format and keep a identifier of the original data frame: df$variable. Finally we plot the scatterplot using ggplot2 and colours to differentiate between the two data frames.

library(reshape2)
library(ggplot2)
names(df2) <- c("x1", "y2")
df <- rbind(melt(df1, id.vars = "x1"), melt(df2, id.vars = "x1"))
ggplot(df, aes(x1, y = value, colour = variable)) + 
  geom_point() + labs(x = "x", y = "y") +
  scale_colour_manual(values = c("red", "blue"), labels = c("df1", "df2"))

enter image description here

Data

df1 <- structure(list(x1 = c(42.39, 38.77, 44.43, 42.37, 48.79, 46, 
53.71, 47.38, 43.75, 46.95), y1 = c(2.1, 2.1, 2.6, 2, 3.6, 2, 
2.7, 1.8, 3.1, 3.9)), .Names = c("x1", "y1"), class = "data.frame", row.names = c(NA, 
-10L))
df2 <- structure(list(x2 = c(53.05, 43.81, 42.67, 41.74, 42.99), y2 = c(8.4, 
2.6, 2.4, 3.4, 2.9)), .Names = c("x2", "y2"), class = "data.frame", row.names = c(NA, 
-5L))
mpalanco
  • 10,839
  • 1
  • 53
  • 63
1

simple example

set.seed(100)
    df <- data.frame(x1 = rnorm(10, 10, 20), y1 = rnorm(10, 10, 2), x2 = rnorm(10, 10, 4), y2= rnorm(10, 10, 4))

    plot(df$x1, df$y1)
    points(df$x2, df$y2, pch = 16)
Mateusz1981
  • 1,703
  • 15
  • 29