2

I'm trying to change the x axis in a matplot, but this command doesn't work:

TimePoints=1997:2011
matplot(t(DataMatrix),type='l',col="black",lwd=1,xlab="Anni",ylab="Rifiuti",main="Produzione rifiuti")
axis(side=1,at=TimePoints,labels=TimePoints)

with plot I used this without problems. How can I fix it? Here you can find the objects: https://dl.dropboxusercontent.com/u/47720440/SOF.RData

1 Answer 1

13

I usually do this as follows:

  1. Omit the axes altogether.
  2. Add the axes with desired options one by one.

In R:

# Add argument axes=F to omit the axes
matplot(t(DataMatrix),type='l',col="black",lwd=1,xlab="Anni",ylab="Rifiuti",main="Produzione rifiuti",axes=F)

# Add Y-axis as is
axis(2)

# Add X-axis
# Note that your X-axis range is not in years but in the "column numbers",
# i.e. the X-axis range runs from 1 to 15 (the number of columns in your matrix)
# Possibly that's why your original code example did not work as expected?
axis(side=1,at=1:ncol(DataMatrix),labels=TimePoints)
1
  • 2
    For those new to R, I would like to point out that the length of "at" must be equal to the length of "labels".
    – scs
    Dec 3, 2015 at 8:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.