-4

I have a multiple dataframes each having a x and y column. The x values are same for all dataframes but the y values differ.

I wish plot on a single graph the y values for all dataframes,
corresponding to each x value.

I have tried using the plot function, it doesn't seem to offer such a functionality.

Please help me out. Thanks in advance!

Abhishek Bhatia
  • 9,404
  • 26
  • 87
  • 142
  • 2
    I have voted down because you don't show that you have spent any effort. – Roland Aug 18 '14 at 16:37
  • 1
    You should always post a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample data. Also include what code you've tried to make it work and be clear how it fails. See [how to ask a good question](http://stackoverflow.com/help/how-to-ask). – MrFlick Aug 18 '14 at 16:37
  • I'm with @Roland. Show that you've made an effort to solve this yourself and the down-votes will probably get retracted. – Rich Scriven Aug 18 '14 at 16:58
  • Will do in next 18 hrs as well as improve question...am caught up in something right now...apologies for the delay. – Abhishek Bhatia Aug 19 '14 at 01:14

3 Answers3

2

Using Roland's data:

set.seed(42)
DF1 <- data.frame(x=1:5, y=rnorm(5))
DF2 <- data.frame(x=1:5, y=rnorm(5))
DF3 <- data.frame(x=1:5, y=rnorm(5))

Try:

ggplot()+
    geom_point(data=DF1, aes(x,y), color='red')+
    geom_point(data=DF2, aes(x,y), color='blue')+
    geom_point(data=DF3, aes(x,y), color='green')

enter image description here

rnso
  • 23,686
  • 25
  • 112
  • 234
1
#some data
set.seed(42)
DF1 <- data.frame(x=1:5, y=rnorm(5))
DF2 <- data.frame(x=1:5, y=rnorm(5))
DF3 <- data.frame(x=1:5, y=rnorm(5))

#merge the data
dat <- Reduce(function(X, Y) merge(X, Y, by="x"), list(DF1, DF2, DF3))

#make it a matrix
mat <- as.matrix(dat)

#plot
matplot(mat, pch=1)
Roland
  • 127,288
  • 10
  • 191
  • 288
1

A simple plot + points approach using Roland's example:

#some data
set.seed(42)
DF1 <- data.frame(x=1:5, y=rnorm(5))
DF2 <- data.frame(x=1:5, y=rnorm(5))
DF3 <- data.frame(x=1:5, y=rnorm(5))

Make the first plot:

# Finding the y limits
max.y = max(DF1$y,DF2$y,DF3$y)
min.y = min(DF1$y,DF2$y,DF3$y)


plot(x = DF1$x, y = DF1$y,pch = 19,xlab = "X", ylab = "Y",ylim = c(min.y,max.y)

Add other points:

points(x = DF2$x, y = DF2$y,col = "blue",pch = 19)
points(x = DF3$x, y = DF3$y,col = "red",pch = 19)

enter image description here

Bernardo
  • 426
  • 3
  • 16