Overlap chart in R

3

How can I superimpose two or more graphs without the axis limits also overlapping in the figure?

a = rnorm(1000)
b = runif(1000)
plot(a, type = "l")
par(new = T)
plot(b, type = "l", col = "blue", xlab = "", ylab = "")
#os eixos de y ficam sobrepostos
    
asked by anonymous 08.09.2015 / 18:27

2 answers

6

If your goal is the same as the example, one solution is to use matplot()

a = rnorm(1000)
b = runif(1000)
matplot(cbind(a, b), type = 'l')

You can also do with ggplot :

library(ggplot2)
ggplot(data = data.frame(a = a, b = b, x = 1:1000)) + geom_line(aes(x = x, y = a, colour = 'a')) + geom_line(aes(x = x, y = b, colour = 'b'))
    
08.09.2015 / 18:36
4

Instead of drawing the two graphs, you can draw the first graph, and then add only the lines of the second series. This will give you both "overlapping" graphics

a = rnorm(1000)
b = runif(1000)
plot(a, type = "l")
lines(b, col = "blue")
    
08.09.2015 / 19:08