jameselmore
jameselmore

Reputation: 442

Overlaying scatterplots with varying amount of data

I'm trying to plot three different data sets on the same plot. I have 32 datapoints for the first set, 38 for the second set, and 48 for the third set.

I can't bind them together in a data.frame in order to pass them into matplot and I'm not sure what to do.

Any thoughts / methods to do this (it's probably something easy that I've just never seen before)?

They are all completely independent of each other, and there's no reason why I shouldn't be able to over lay them.

Upvotes: 1

Views: 73

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226097

e.g.

d1 <- data.frame(x=runif(20),y=runif(20))
d2 <- data.frame(x=rnorm(10),y=rnorm(10))
d3 <- data.frame(x=rpois(5,5),y=rpois(5,5))
allD <- rbind(d1,d2,d3)
plot(y~x,data=d1,xlim=range(allD$x),ylim=range(allD$y))
with(d2,points(x,y,col=2))
with(d3,points(x,y,col=4))

or:

plot(y~x,data=d1,xlim=range(allD$x),ylim=range(allD$y),type="n")
mapply(function(x,c) with(x,points(x,y,col=c)),
       list(d1,d2,d3),c(1,2,4))

or:

allD$group <- rep(1:3,c(20,10,5))
plot(y~x,data=allD,col=allD$group)

or:

library(lattice)
xyplot(y~x,groups=group,data=allD)

or:

library(ggplot2)
ggplot(allD,aes(x,y,colour=factor(group)))+geom_point()

Upvotes: 2

Related Questions