Skip to content

plot xaxp issue

9 messages · PIKAL Petr, Elaine Kuo, David Winsemius

#
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)
#
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
#
Thanks a lot.
Please kindly indicate the meaning of the c(8,7).
Elaine
On Mon, Jan 7, 2013 at 4:55 PM, PIKAL Petr <petr.pikal at precheza.cz> wrote:
#
Thank you Petr, the code is wonderful.

One more question,
you used [as.numeric(together$sex)] to drawing plots many times (Par(new)).
Please kindly advise if there is a similar method to replace drawing
ablines many times.
If not, I am afraid that the ablines will not follow the same Y and
X-axis places.

Elaine
On Mon, Jan 7, 2013 at 8:11 PM, Elaine Kuo <elaine.kuo.tw at gmail.com> wrote:
#
Hi
It is not about drawing plot many times but coding points or graphic objects by some factor. In your case sex.
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) 
}
They will. You can try it yourself. Sometimes R functions are quite close to magic. 

Regards
Petr
#
Hello,

Thanks again.
But something wrong with the subset after lm
boy<-read.csv("H:/boy_data.csv",header=T)
girl<-read.csv("H:/girl_data.csv",header=T)
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)], ....)

boyline<-lm(body_weight ~ body_length, data=together, subset(sex=="boy"))
I updated it aas
But an error showed, saying:
error in xj[i] : useless type of  'list'

Please kindly help and thanks again.
BTW, the code below might need update as well.
Elaine
The original code with advice by Petr
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
boy<-read.csv("H:/boy_data.csv",header=T)
girl<-read.csv("H:/girl_data.csv",header=T)
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)], ....)

boyline<-lm(body_weight ~ body_length, data=together, subset(sex=="boy"))
abline(boyline,col="firebrick3",lwd=2)
#
Hello,

I figured out that the code should be

boyline<-lm(body_weight ~ body_length, data=subset (together,,sex=="boy"))

However, the "" could be omitted if the field name happened to be
numeric, such as 1, 2, or 3.
Please kindly explain why the "" could be omitted for numbers.
Thanks again.

Elaine
On Tue, Jan 8, 2013 at 8:40 AM, Elaine Kuo <elaine.kuo.tw at gmail.com> wrote:
#
Hi

I actually do not know what you are talking about. I believe that the code I provided works (if i did not make typing mistake), but you failed to provide data to test so I made my own data and subset inside lm works as expected.

fake<-data.frame(weight<-rnorm(15), length=weight*3+rnorm(15), sex=rep(c("b","g"), c(7,8)))
names(fake)[1]<-"weight"
plot(fake$length, fake$weight, col=as.numeric(fake$sex))
bl<-lm(weight~length, data=fake, subset=sex=="b")
abline(bl)
gl<-lm(weight~length, data=fake, subset=sex=="g")
abline(gl, col=2)

Beware that R is smart but not smart enough to correct user typing mistakes (missing parntheses, missing commas, etc.). Typing mistakes are probably the most common source of failing code.

Regards

Petr
#
On Jan 7, 2013, at 5:02 PM, Elaine Kuo wrote:

            
The interpreter parses atoms composed only of [digits, "-", "."] ,  
i.e. "numeric",  differently than it parses atoms with leading  
[alphas]. Please study the introductory material more thoroughly where  
this is all explained and illustrated. And do provide minimal  
reproducible examples. That might be especially important here , since  
failing to include the quotes could be very prone to error if the  
underlying object is a factor variable.