2012年1月6日星期五

example for double y axis


Here I present an example for double y axis case:


# (1) you will want to generate a graph with the same X axis, but two plot with different y values.

x <- 1:10

y <- rnorm(10)

z <- runif(10, min=1000, max=10000) # second data set on a very different scale

par(mar = c(5, 4, 4, 4) + 0.3)  # Leave space for z axis

plot(x, y) # first plot

par(new = TRUE)

plot(x, z, type = "l", axes = FALSE, bty = "n", xlab = "", ylab = "")

axis(side=4, at = pretty(range(z)))

mtext("z", side=4, line=3)
Created by Pretty R at inside-R.org




# (2) if you only want one plot, but two different annotation of y axis.   

x <- 1:10

y <- rnorm(10)

z <- runif(10, min=1000, max=10000) # second data set on a very different scale

par(mar = c(5, 4, 4, 4) + 0.3)  # Leave space for z axis

plot(x, y, type = "n") # first plot, with type = "n", we will get the empty graph

par(new = TRUE)

plot(x, z, type = "l", axes = FALSE, bty = "n", xlab = "", ylab = "") # here, plot the plot you want

axis(side=4, at = pretty(range(z))) # becare of the pretty() function. it is really nice one.

mtext("z", side=4, line=3)
Created by Pretty R at inside-R.org




没有评论:

发表评论