Graphic overlay on R

Asked

Viewed 7,652 times

3

how can I overlay two or more graphs without the limits of the axes also appear superimposed on 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

3 answers

6


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

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

It is also possible to do with the 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'))

4

Instead of drawing the two graphs, you can draw the first graph, and then add only the lines of the second series. With this you will have the two graphics "superimposed"

a = rnorm(1000)
b = runif(1000)
plot(a, type = "l")
lines(b, col = "blue")

0

You can disable the axes of one of the graphs to avoid overlapping, using axes=FALSE. To avoid overlapping subtitles, you can use ann=FALSE.

Below is your code with the tips already added:

a = rnorm(1000) 
b = runif(1000)
plot(a, type = "l")
par(new = T)
plot(b, type = "l", col = "blue", axes = FALSE, ann = FALSE)
#os eixos de y não estão mais sobrepostos

Browser other questions tagged

You are not signed in. Login or sign up in order to post.