[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)

pdf("D_k_and_R_mobility.pdf")
 par(mar=c(4.5,5,2,4.2)+0.1)
plot(x, temp, 
type="b",lwd=2,col="transparent",lty=2,xlab=expression(paste(k))
,ann=FALSE,yaxt="none", ylim=range(c(0.,1.4e-1)), xaxt="none",
     xlim=range(c(0,100)),cex.lab=1.4,cex.axis=1.2)

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 = 
1,cex.lab=1.4,cex.axis=1.2)
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 = 
"black",cex.lab=1.4,cex.axis=1.2)

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])),
                     expression(paste(R[m])),expression(paste(R[g]))), 
lwd=c(2,2,2),lty=c(2,1,1),pch = c(1,4,6),col=c("blue", 
"red","red"),box.lwd=1,box.lty=1,
,xjust = 1, yjust = 1)

dev.off()

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

http://tolstoy.newcastle.edu.au/R/help/03b/2708.html
and
http://www.mail-archive.com/r-help@stat.math.ethz.ch/msg07033.html

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

Lorenzo



More information about the R-help mailing list