[R] Row order in plot

0 views
Skip to first unread message

qroberts

unread,
Dec 11, 2008, 2:35:17 PM12/11/08
to r-h...@r-project.org

I'm new to R so forgive me if this seems like a simple question:

So I have table where the row titles are string variables. When I plot the
data with rows along the x-axis, the data is ordered alphabetically as
opposed to the order of the table.

How can I preserve the row order of the table in the plot?

Thanks in advance.
--
View this message in context: http://www.nabble.com/Row-order-in-plot-tp20962774p20962774.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-h...@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.

Sarah Goslee

unread,
Dec 11, 2008, 2:58:13 PM12/11/08
to qroberts, r-h...@r-project.org
It would be easier to answer your question if we knew what your
data look like, what R commands you've tried, and what result
you want.

One possibility: plot the data against 1:nrow(yourdata), and add
the row names as labels.

Sarah

On Thu, Dec 11, 2008 at 2:35 PM, qroberts <lvai...@bu.edu> wrote:
>
> I'm new to R so forgive me if this seems like a simple question:
>
> So I have table where the row titles are string variables. When I plot the
> data with rows along the x-axis, the data is ordered alphabetically as
> opposed to the order of the table.
>
> How can I preserve the row order of the table in the plot?
>
> Thanks in advance.

--
Sarah Goslee
http://www.functionaldiversity.org

qroberts

unread,
Dec 11, 2008, 3:12:21 PM12/11/08
to r-h...@r-project.org

This is the format of the table as it appears in R

>mice

X Green.1 Yellow.2 Blue.3 Gray.4
1 Base 469.5399 508.1532 487.1443 492.2544
2 PBS 459.6553 474.0124 417.2651 392.9518
3 25 Mch 359.6216 418.0417 377.7020 394.2102
4 50 Mch 206.1835 262.8818 252.8041 172.0568
5 Ext. 287.4200 279.8562 287.3744 236.5091

I have been plotting Green.1 vs X as follows

plot(Green.1~X,data=mice)

The order on the x axis is alphabetical e.g 25 Mch, 50 Mch, Base, Ext., PBS

When I would like it to be : Base, PBS, 25 Mch, 50 Mch, Ext. like in the
table.

--
View this message in context: http://www.nabble.com/Row-order-in-plot-tp20962774p20963678.html


Sent from the R help mailing list archive at Nabble.com.

______________________________________________

Peter Alspach

unread,
Dec 11, 2008, 4:36:35 PM12/11/08
to qroberts, r-h...@r-project.org
Tena koe

Try

mice$X <- factor(mice$X, levels=mice$X)
plot(Green.1~X,data=mice)

HTH ....

Peter Alspach

The contents of this e-mail are confidential and may be subject to legal privilege.
If you are not the intended recipient you must not use, disseminate, distribute or
reproduce all or any part of this e-mail or attachments. If you have received this
e-mail in error, please notify the sender and delete all material pertaining to this
e-mail. Any opinion or views expressed in this e-mail are those of the individual
sender and may not represent those of The New Zealand Institute for Plant and
Food Research Limited.

Sarah Goslee

unread,
Dec 11, 2008, 4:38:33 PM12/11/08
to qroberts, r-h...@r-project.org
Then yes, you can do something like I originally suggested, though there
are other possible approaches.

Making up fake data rather than typing yours in:

> testdata <- as.data.frame(matrix(1:15, ncol=3))
> testdata <- cbind(c("D", "E", "A", "C", "B"), testdata)
> colnames(testdata) <- c("X", "c1", "c2", "c3")
> testdata$X <- as.character(testdata$X)
> testdata
X c1 c2 c3
1 D 1 6 11
2 E 2 7 12
3 A 3 8 13
4 C 4 9 14
5 B 5 10 15


> plot(1:nrow(testdata), testdata$c1, xaxt="n")
> axis(1, at=1:nrow(testdata), labels=testdata$X)

Gives what I think you want.

Sarah

On Thu, Dec 11, 2008 at 3:12 PM, qroberts <lvai...@bu.edu> wrote:
>
> This is the format of the table as it appears in R
>
>>mice
>
> X Green.1 Yellow.2 Blue.3 Gray.4
> 1 Base 469.5399 508.1532 487.1443 492.2544
> 2 PBS 459.6553 474.0124 417.2651 392.9518
> 3 25 Mch 359.6216 418.0417 377.7020 394.2102
> 4 50 Mch 206.1835 262.8818 252.8041 172.0568
> 5 Ext. 287.4200 279.8562 287.3744 236.5091
>
> I have been plotting Green.1 vs X as follows
>
> plot(Green.1~X,data=mice)
>
> The order on the x axis is alphabetical e.g 25 Mch, 50 Mch, Base, Ext., PBS
>
> When I would like it to be : Base, PBS, 25 Mch, 50 Mch, Ext. like in the
> table.
>
>
>
--

Sarah Goslee
http://www.functionaldiversity.org

Sarah Goslee

unread,
Dec 11, 2008, 4:41:58 PM12/11/08
to Peter Alspach, r-h...@r-project.org, qroberts
Though the results of using plot() with a factor may not always be what you
expect, as plot.factor() differs from plot().

Sarah

On Thu, Dec 11, 2008 at 4:36 PM, Peter Alspach
<PAls...@hortresearch.co.nz> wrote:
> Tena koe
>
> Try
>
> mice$X <- factor(mice$X, levels=mice$X)
> plot(Green.1~X,data=mice)
>
> HTH ....
>
> Peter Alspach
>

--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________

qroberts

unread,
Dec 12, 2008, 7:31:56 AM12/12/08
to r-h...@r-project.org

Thanks very much for the replies.

Both suggestions worked perfectly.

That's one more step towards understanding R.

Thanks again.


--
View this message in context: http://www.nabble.com/Row-order-in-plot-tp20962774p20974800.html


Sent from the R help mailing list archive at Nabble.com.

______________________________________________

Reply all
Reply to author
Forward
0 new messages