[R] plot xaxp issue
PIKAL Petr
petr.pikal at precheza.cz
Mon Jan 7 15:22:45 CET 2013
Hi
> -----Original Message-----
> From: Elaine Kuo [mailto:elaine.kuo.tw at gmail.com]
> Sent: Monday, January 07, 2013 1:45 PM
> To: PIKAL Petr
> Cc: r-help at r-project.org
> Subject: Re: [R] plot xaxp issue
>
> Thank you Petr, the code is wonderful.
>
> One more question,
> you used [as.numeric(together$sex)] to drawing plots many times
> (Par(new)).
It is not about drawing plot many times but coding points or graphic objects by some factor. In your case sex.
> Please kindly advise if there is a similar method to replace drawing
> ablines many times.
Instead of
boyline<-lm(body_weight ~ body_length, boy)
use collective data frame together and subset only one sex.
boyline<-lm(body_weight ~ body_length, data=together, subset(sex=="boy"))
abline(boyline,col="firebrick3",lwd=2)
In case of more than two sexes (some SF authors mentioned it is possible) you can use simple loop.
for (i in 1:2)) {
subs <- together$sex==levels(together$sex)[i]
line<-lm(body_weight ~ body_length, data=together, subset=subs)
abline(line, col=c("firebrick3","saddlebrown")[i],lwd=2)
}
> If not, I am afraid that the ablines will not follow the same Y and X-
> axis places.
They will. You can try it yourself. Sometimes R functions are quite close to magic.
Regards
Petr
>
> Elaine
>
>
> On Mon, Jan 7, 2013 at 8:11 PM, Elaine Kuo <elaine.kuo.tw at gmail.com>
> wrote:
> > Thanks a lot.
> > Please kindly indicate the meaning of the c(8,7).
> >
> >> together <- rbind(boy, girl)
> >> together$sex <- factor(rep(c("boy", "girl"), c(8,7)))
> >
> > Elaine
> >
> > On Mon, Jan 7, 2013 at 4:55 PM, PIKAL Petr <petr.pikal at precheza.cz>
> wrote:
> >> Hi
> >>
> >> Instead of two plots with par(new = TRUE) try to put boys and girls
> >> together (quite natural thing, they will be pleased 8-)
> >>
> >> together <- rbind(boy, girl)
> >> together$sex <- factor(rep(c("boy", "girl"), c(8,7)))
> >>
> >> plot(together$body_length, together$body_weight, ....,
> >> col=c("firebrick3","saddlebrown")[as.numeric(together$sex)], ....)
> >>
> >> Regards
> >> Petr
> >>
> >>
> >>> -----Original Message-----
> >>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> >>> project.org] On Behalf Of Elaine Kuo
> >>> Sent: Monday, January 07, 2013 9:27 AM
> >>> To: r-help at r-project.org
> >>> Subject: [R] plot xaxp issue
> >>>
> >>> Hello,
> >>>
> >>> I have data of Body length and Body weight of 8 boys and 7 girls.
> >>>
> >>> I want to draw the plot of Body length (for X) and Body weight (for
> >>> Y) based on sex.
> >>> Then the two plots want to be overlapped for comparison.
> >>>
> >>> I used the code below but found the unit length of X axis of boy
> and
> >>> girl plot are not the same.
> >>> For instance, the length between 0 and 1 of boy plot is larger than
> >>> that in girl plot.
> >>> The same thing happened to Y axis as well.
> >>> (In other words, though axap and yaxp were set to be the same, the
> >>> display were not the same.)
> >>>
> >>> Please kindly advise correction of the code.
> >>> Thank you.
> >>>
> >>> Elaine
> >>>
> >>> # plot code
> >>> boy<-read.csv("H:/boy_data.csv",header=T)
> >>> girl<-read.csv("H:/girl_data.csv",header=T)
> >>> par(mai=c(1.03,1.03,0.4,0.4))
> >>>
> >>> plot(boy$body_length, boy$body_weight,
> >>> xlab=" body_length (cm)",
> >>> ylab=" body_weight ( kg )",
> >>> xaxp=c(0,200,4),
> >>> yaxp=c(0,100,4),
> >>> type="p",
> >>> pch=1,lwd=1.0,
> >>> cex.lab=1.4, cex.axis=1.2,
> >>> font.axis=2,
> >>> cex=1.5,
> >>> las=1,
> >>> bty="l",col="firebrick3")
> >>>
> >>> boyline<-lm(body_weight ~ body_length, boy)
> >>> summary(boyline)
> >>> abline(boyline,col="firebrick3",lwd=2)
> >>>
> >>>
> #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >>> # graph
> >>> par(mai=c(1.03,1.03,0.4,0.4))
> >>>
> >>> par(new=T)
> >>>
> >>> plot(girl$body_length, girl$body_weight,
> >>> xlab=" body_length (cm)",
> >>> ylab=" body_weight ( kg )",
> >>> xaxp=c(0,200,4),
> >>> yaxp=c(0,100,4),
> >>> type="p",
> >>> pch=1,lwd=1.0,
> >>> cex.lab=1.4, cex.axis=1.2,
> >>> font.axis=2,
> >>> cex=1.5,
> >>> las=1,
> >>> bty="l",col="saddlebrown")
> >>>
> >>>
> >>> girlline<-lm(body_weight~ body_length, girl)
> >>> summary(girlline)
> >>> abline(girlline,col="saddlebrown",lwd=2)
> >>>
> >>> ______________________________________________
> >>> R-help at r-project.org mailing list
> >>> https://stat.ethz.ch/mailman/listinfo/r-help
> >>> PLEASE do read the posting guide http://www.R-project.org/posting-
> >>> guide.html and provide commented, minimal, self-contained,
> >>> reproducible code.
More information about the R-help
mailing list