EpiMan
EpiMan

Reputation: 839

overlay two plots with different x scale

I would like to overlay two plots:

plot1

t1 <- c(0,1,2,3,4,5,6,7,8,9,10)
d1 <- c(0,2,4,6,8,10,12,14,16,18,20)

plot2

t2 <- c(0,1,2,3,4,5)    
d2 <- c(1,3,7,8,8,8)

I tried

plot(d1~t1, col="black", type="l")
par(new=T)
plot(d2~t2, col="black", type="l")

But the problem is: in this way, both x axes also overlay each other while x in plot1 is 1:10 and plot2 1:5

Upvotes: 1

Views: 2095

Answers (1)

Sven Hohenstein
Sven Hohenstein

Reputation: 81693

You can use lines for the second plot (instead of plot). Furthermore, we scale the x-axis values of the second plot (t2) with 2 (I(2 * t2)).

plot(d1 ~ t1, col="black", type="l", xlim=c(0,10))
lines(d2 ~ I(2 * t2), col="black", type="l", xlim=c(0,5))

In this way, the x-range of the second plot is identical to the x-range of the first one.

enter image description here

Upvotes: 4

Related Questions