Palace Chan
Palace Chan

Reputation: 9203

How do I get these two scatters on the same plot for comparison?

Right now The R snippet I am using is:

a <- read.table('A.out')
a <- cbind(1:nrow(a), a)
colnames(a) <- c('Observation','Time')

med.a <- median(a$Time)
plot(a$Observation, a$Time, xaxt="n", yaxt="n", xlab="",
     ylab="", type="b", col="red", pch=19)
abline(med.a,0, col='red', lty=2)
grid(col='darkgray', lty=1)

#Overlay Someone else
b <- read.table('B.out')
b <- cbind(1:nrow(b), b)
colnames(b) <- c('Observation','Time')

par(new=TRUE)
med.b <- median(b$Time)
plot(b$Observation, b$Time, xaxt="n", ylab="units", type="b", col="blue", pch=19)
abline(med.b,0, col='blue', lty=2)

But this does not account for differences in scale. (i.e. even if A.out values are much larger than B.out values, they will be displayed on the same y-scale..how do I get the desired effect to be able to compare them?)

Here is the content of my A.out and B.out:

> a
  Observation     Time
1           1 11758000
2           2 10523000
3           3 10306000
> b
  Observation         Time
1           1 133721740000
2           2 133759475000
3           3 133724604000

Upvotes: 0

Views: 250

Answers (2)

IRTFM
IRTFM

Reputation: 263411

You should add this inside both plot calls:

  ..., xlim=range(c( a$Observation, b$Observation )), 
       ylim= range(c( a$Time, b$Time )),  ...

Upvotes: 0

ALiX
ALiX

Reputation: 1031

You are calling plot twice. Each call sets up a new coordinate system. Instead, use one call to plot to set up the axes and the coordinate system, then use lines to plot the actual points:

xrange <- range(c(a$Observation, b$Observation))
yrange <- range(c(a$Time, b$Time))
plot(0, type="n", xlim=xrange, ylim=yrange)
lines(a$Observation, a$Time, type="b", col="red", pch=19)
lines(b$Observation, b$Time, type="b", col="blue", pch=19)

From here, you should be able to add other things as you need, like median lines, axis labels, etc.

Upvotes: 4

Related Questions