Skip to content

Simple plot() question

4 messages · michael watson (IAH-C), Dimitris Rizopoulos, James W. MacDonald +1 more

#
Hi

First a simple question to do with plot().  How do I change the x-axis
labels on a plot?

For example, I am plotting each row of a matrix, and I want each row to
be a line on my graph.  Simple really.  Eg:

plot(sg[1,], type="l")

When I do this, the x-axis is labelled 1:38, as I have 38 columns in my
matrix.  When I do:

plot(sg[1,order(sg[1,])], type="l")

Ideally I would like the x-axis labels to reflect the new order of the
columns, but they're still numbered 1:38... I've read the plot() docs
and the par() docs and can't figure it out - I know I'm missing
something obvious, but what?

Cheers

Mick

Michael Watson
Head of Informatics
Institute for Animal Health,
Compton Laboratory,
Compton,
Newbury,
Berkshire RG20 7NN
UK

Phone : +44 (0)1635 578411 ext. 2535
Mobile: +44 (0)7990 827831
E-mail: michael.watson at bbsrc.ac.uk
#
try something like

plot(..., axes=FALSE)
axis(2); axis(1, labels=c("your labels"))

Best,
Dimitris

----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/396887
Fax: +32/16/337015
Web: http://www.med.kuleuven.ac.be/biostat
     http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm


----- Original Message ----- 
From: "michael watson (IAH-C)" <michael.watson at bbsrc.ac.uk>
To: <r-help at stat.math.ethz.ch>
Sent: Tuesday, November 16, 2004 3:45 PM
Subject: [R] Simple plot() question
#
michael watson (IAH-C) wrote:
You want to add xaxt="n" so the x-axis labels are not printed. Then add 
your new labels using axis(1, at=1:38, labels=<some vector of label names>)

HTH,

Jim

  
    
#
Hi Mick:

I'm a little unsure if this is what you are after but does this do it?

foo.mat <- matrix(rnorm(100), nrow = 10, ncol = 10)
plot(foo.mat[1,], type="l", xlab = "Crud", ylab = "More Crud")
plot(foo.mat[1,order(foo.mat[1,])], type="l", xaxt = "n", xlab = "Crud",
ylab = "More Crud")
axis(1, at=1:length(foo.mat[1,]), labels= order(foo.mat[1,]))


Seems like a barplot might be a nice way to go too:
barplot(foo.mat[1,order(foo.mat[1,])], names.arg = order(foo.mat[1,]))


A reproducible example would help the list see what you are after as I might
be totally off on this.
HTH, Andy
http://www.R-project.org/posting-guide.html