[R] Plotting two y-axis vs non-numeric x-axis

Jim Lemon jim at bitwrit.com.au
Sun Jun 16 02:48:03 CEST 2013


On 06/16/2013 04:14 AM, Birdada Simret wrote:
> Thank you.
> @David: The example is exactly this:
> time <- seq(0,72,6)
> music <- c(0.05,0.18,0.25,0.31,0.32,0.34,0.35,
> 0.36,0.37,0.38,0.39,0.40,0.41)
> actor <- c(0,1000,2000,3000,4000,5000,6000,7000,8000,9000,10000,11000,12000)
> par(mar=c(5, 4, 4, 4) + 0.1)
> plot(time, music, pch=16, axes=F, ylim=c(0,1), xlab="", ylab="",
>   type="b",col="black", main="Enjoy")
>   axis(2, ylim=c(0,1),col="black")
> mtext("Music",side=2,line=2.5)
>   box()
> par(new=T)
>   plot(time, actor, pch=15,  xlab="", ylab="", ylim=c(0,13000), axes=F,
>   type="b", col="red")
>   mtext("Actor",side=4,col="red",line=2.5)
> axis(4, ylim=c(0,13000), col="red",col.axis="red")
>   axis(1,pretty(range(time),10))
> mtext("Time (Hours)",side=1,col="black",line=2.5)
> legend(5,13000,legend=c("Music","Actor"),text.col=c("black","red"),pch=c(16,15),col=c("black","red"))
>
> Now, what if time is replaced by month <-
> c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","Pag")
>
Hi Birdada,
You can get the month labels using something like this:

twoord.plot(time,music,time,actor,lylim=c(0,1),type=c("l","b"),
  xtickpos=seq(0,70,by=10),xticklab=month.abb[1:8])

Hi arun,
This question may already have been answered, but to change the symbols, 
you have to use "lpch=" and "rpch=". I get the appropriate types of plot 
when using the above, and I tried several combinations of point, line, 
both that all worked okay. If I figure out what happened in your example 
I'll email the answer.

Jim



More information about the R-help mailing list