[R] Plotting two y-axis vs non-numeric x-axis
David Winsemius
dwinsemius at comcast.net
Sat Jun 15 20:45:53 CEST 2013
On Jun 15, 2013, at 11: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")
>
> when I tried"
> I got error like
> "In pretty.default(range(month), 10) : NAs introduced by coercion" I am
> newbie to R ;) many thanks
Er, Not really. The error was the line above that warning:
"Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf"
You are sending a character vector to plot() as the first argument, and plotting dispatch system cannot find a 'plot.character' method so it sends it to 'plot.default' which is expecting a numeric vector for x and things all fall apart from there.
You could use: seq_along(month) # instead of month
(And obviously the xlab would need to be changed, since you are no longer working with "Hours".)
And. Please learn to post in plain text.
-- David
> @Arun, It is really helpful, thank you. though only the red marked plot is
> perfectly shown.
>
>
> On Sat, Jun 15, 2013 at 6:20 PM, arun <smartpink111 at yahoo.com> wrote:
>
>>
>>
>> Hi,
>>
>> You could use ?twoord.plot() from library(plotrix):
>> library(plotrix)
>> dat2<- data.frame(month,music,actor)
>> dat2$month<- factor(month,labels=c(month.abb,"Pag"))
>> dat2New<-dat2[order(dat2$month),]
>>
>> with(dat2New,twoord.plot(month,music,actor,
>> lylim=c(0,1),rylim=c(0,13000),
>> ylab="Music",
>> rylab="Actor",main="Enjoy",
>>
>> type=c("p","b"),lcol=1,rcol="red",xtickpos=month,xticklab=month))
>>
>> legend(5,13000,legend=c("Music","Actor"),text.col=c("black","red"),col=c("black","red"))
>>
>> #I tried to change the plotting symbol using "pch" inside the
>> twoord.plot(), but it seems to be not working.
>> with(dat2New,twoord.plot(month,music,actor,
>> lylim=c(0,1),rylim=c(0,13000),
>> ylab="Music",
>> rylab="Actor",main="Enjoy",
>>
>> type=c("p","b"),pch=c(1,4),lcol=1,rcol="red",xtickpos=month,xticklab=month))
>> #Error in localWindow(xlim, ylim, log, asp, ...) :
>> # formal argument "pch" matched by multiple actual arguments
>>
>> A.K.
>>
>>
>>
>> ----- Original Message -----
>> From: Birdada Simret <birdada85 at gmail.com>
>> To: r-help at r-project.org
>> Cc:
>> Sent: Saturday, June 15, 2013 8:18 AM
>> Subject: [R] Plotting two y-axis vs non-numeric x-axis
>>
>> Hi dear all, the following code is correct. but I want to use non-numeric
>> x-axis, for example
>> if I replace time <- seq(0,72,6) by
>> month <-
>>
>> c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","Pag")
>>
>> Ofcourse I use factor(month) instead of time; but I didn't get similar
>> plot as the example shown. any help is greatful ;)
>>
>> month <-
>>
>> c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","Pag")
>> 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(factor(month), 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(factor(month), 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(month),10))
>> mtext("Month",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"))
>>
>> [[alternative HTML version deleted]]
>>
>>
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list