Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set axis range R

Tags:

r

axis

I'm having trouble setting my axis range in r. My data only has values from 2 to 9 on the x axis but I want it to go from 1 to 10. Any quick tips please?

head(SS)
  Phase Bed Site ACC X.M.SA
1     1   1    1  NG     NO
2     1   1    2  NG     NO
3     1   1    3  SG     NO
4     1   1    4  SG     NO
5     1   1    5  SG     NO
6     1   2    1  SG     NO

XMSA<-factor(SS$X.M.SA)
ACC<-factor(SS$ACC,ordered = TRUE,levels=c("NG","SG","LG","MG","HG"))

boxplot(ACC[XMSA=="MSSA"]~SS$Bed[XMSA=="MSSA"],
xlab="Bed",ylab="Growth",
las=1, yaxt="n",ylim=c(1,5),xlim=c(1,10))
axis(2, at=c(1,2,3,4,5),labels=c("NG","SG","LG","MG","HG"),las=1)

enter image description here

like image 679
HCAI Avatar asked Apr 01 '16 09:04

HCAI


1 Answers

Without data, I tried to reproduce your plot error:

plot(x=as.factor(2:8),y=2:8,xlim = c(1,10)

Which gives the following plot:

enter image description here

Changing your plot to:

boxplot(x= as.numeric(as.character(SS$Bed[XMSA=="MSSA"])),
y= ACC[XMSA=="MSSA"]
xlab="Bed",ylab="Growth",
las=1, yaxt="n",ylim=c(1,5),xlim=c(1,10))
axis(2, at=c(1,2,3,4,5),labels=c("NG","SG","LG","MG","HG"),las=1)

Might solve your problem.

Edit

It seems that the formula changes to factors and orders it from 1 to the number of items, so I'll use your trick on the y axis to solve this.

boxplot(ACC[XMSA=="MSSA"]~SS$Bed[XMSA=="MSSA"],
xlab="Bed",ylab="Growth",
las=1, yaxt="n",ylim=c(1,5),xlim=c(0,9),xaxt="n")
axis(2, at=1:5,labels=c("NG","SG","LG","MG","HG"),las=1)
axis(1, at=0:9,labels=1:10,las=1)
like image 166
DeveauP Avatar answered Oct 04 '22 01:10

DeveauP