[R] Tick and Label Color in Plot

Lorenzo Isella lorenzo.isella at gmail.com
Wed Dec 24 18:48:02 CET 2008

Dear All,
I would like to plot two quantities using two different scales along the 
two vertical y axes.
On top of this, I would like to use two different colors (let us say red 
and blue) for the two vertical axes, their labels, their ticks and the 
text for each tick.

I paste below a code snippet

x <- c(4,10,50,98)
temp <- c(1.3e-1,4.92e-2,9.48e-3,4.84e-3)
conc  <- c(1.92,5.08,26.35,51.56)
conc2 <- c(7.5e-1,1.14,2.12,4.7)

b <- diff(range(temp))/diff(range(conc))
a <- min(temp) - b*min(conc)

plot(x, temp, 
,ann=FALSE,yaxt="none", ylim=range(c(0.,1.4e-1)), xaxt="none",

lines(x, temp, col="blue",type="b",lwd=2,lty=1,pch=2)

ticks <-c(0, 0.035,0.07,0.105, 0.14)
axis(side=2,at=ticks,labels=FALSE, col = "blue",cex.lab=1.4,cex.axis=1.2)
mtext(side = 2, text = ticks, at = ticks, col = "blue", line = 
mtext(side = 2, text = expression(paste(D[k])), line = 
3,cex.lab=2.4,cex.axis=6.2, col="blue")

ticks <-c(0, 25,50,75, 100)
axis(side=1,at=ticks, labels = ticks, col = 

lines(x, a + b*conc, col="red",type="b",lwd=2,lty=1,pch=4)
lines(x, a + b*conc2, col="red",type="b",lwd=2,lty=1,pch=6)

ticks <-c(0,10,20,30,40,50)
 axis(4, at=a + b*ticks, labels=ticks,cex.lab=1.4,cex.axis=1.2, 
col="red") #this controls the 4th axis and what I want to do there!
mtext(expression(paste(R[g(m)])),cex=1.4, 4, 3, col="red")
legend(50,0.1, c(expression(paste(D[k])),
lwd=c(2,2,2),lty=c(2,1,1),pch = c(1,4,6),col=c("blue", 
,xjust = 1, yjust = 1)


The result is NOT what I am looking for, since I am having a lot of 
troubles in tuning both the color and the size of the labels, tick 
locations and so on.
Can anyone "fix" this example? I had a look at


but obviously I am not quite there yet.
Kind Regards and merry Xmas


More information about the R-help mailing list