Basic question for plotting x axis using row.names

9,366 views
Skip to first unread message

Art

unread,
Feb 24, 2011, 1:21:51 PM2/24/11
to ggp...@googlegroups.com
Is it possible to plot the row names along the axis in the following example data.frame?

       DM   DI   DL   VM   VI   VL
r1    2.7  6.8  6.5  2.5  7.3  5.0
r2    2.7 10.8 10.5  2.7  8.5 10.2
r2.5  9.5 16.5  9.7  7.3 13.2 10.7
r3   13.2 42.7 20.7  9.2 26.0 20.0
r4   17.7 49.2 27.5  6.3 35.7 26.5
i1   36.2 40.3 30.2 10.0 37.0 19.5
i2   52.0 34.2 20.3 34.0 36.0 25.5
i3   45.0 58.0 25.3 29.3 49.2 32.7
c1   32.3 44.3 16.2 23.5 42.7 22.3

For example: 

ggplot(jenn, aes(?, DL)) + geom_point()  ## What do you indicate for the x variable

A second statistical question related to this data.frame.

What is the easiest way to code the mean of the row (i.e. mean across columns for row r2)?
Is the data.frame the most efficient way to represent the data in order to calculate these values?

Thanks,
Art


Ista Zahn

unread,
Feb 24, 2011, 1:41:32 PM2/24/11
to ggp...@googlegroups.com, Art
Hi Art,

On Thu, Feb 24, 2011 at 1:21 PM, Art <john.arthu...@gmail.com> wrote:
> Is it possible to plot the row names along the axis in the following example
> data.frame?
>        DM   DI   DL   VM   VI   VL
> r1    2.7  6.8  6.5  2.5  7.3  5.0
> r2    2.7 10.8 10.5  2.7  8.5 10.2
> r2.5  9.5 16.5  9.7  7.3 13.2 10.7
> r3   13.2 42.7 20.7  9.2 26.0 20.0
> r4   17.7 49.2 27.5  6.3 35.7 26.5
> i1   36.2 40.3 30.2 10.0 37.0 19.5
> i2   52.0 34.2 20.3 34.0 36.0 25.5
> i3   45.0 58.0 25.3 29.3 49.2 32.7
> c1   32.3 44.3 16.2 23.5 42.7 22.3
> For example:
> ggplot(jenn, aes(?, DL)) + geom_point()  ## What do you indicate for the x
> variable

Just enter the values you want on the x-axis:
ggplot(jenn, aes(rownames(jenn), DL)) + geom_point()

>
> A second statistical question related to this data.frame.
> What is the easiest way to code the mean of the row (i.e. mean across
> columns for row r2)?

rowMeans(jenn)

Sometimes

jenn$row.mean <- rowMeans(jenn)

is helpful. Or, if you really only want the value of row r2, take your pick of

rowMeans(jenn)["r2"]
mean(jenn["r2" ,])

Best,
Ista


> Is the data.frame the most efficient way to represent the data in order to
> calculate these values?
> Thanks,
> Art
>

> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

Art

unread,
Feb 24, 2011, 2:51:45 PM2/24/11
to ggp...@googlegroups.com, Art
Thanks Ista,

Using the ggplot(jenn, aes(rownames(jenn), DL)) + geom_point()

How do I preserve row order (r1, r2, r2.5...)

That line of code represents the row names in alphabetical order.

Thanks, 
Art

James McCreight

unread,
Feb 24, 2011, 2:57:28 PM2/24/11
to ggp...@googlegroups.com, Art
Hi Art, 

what about your data frame before X became the rownames? Bring back X as a factor with your specified order and then use that.

jenn$X=row.names(jenn)
ggplot(jenn, aes(x=X, y=DL)) + geom_point()


James


Thanks, 
Art

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2



-

Art

unread,
Feb 24, 2011, 3:01:41 PM2/24/11
to ggp...@googlegroups.com, Art
Excellent!  Thanks James.

I got it to work with a similar approach to the one you suggest: by creating a new variable and applying the factor argument.

# variable with row names

jaxis = c("r1","r2","r2.5","r3","r4","i1","i2","i3","c1")

# To preserve row order

jaxis = factor(jaxis, levels=c("r1","r2","r2.5","r3","r4","i1","i2","i3","c1"))

# plot graph

ggplot(jenn, aes(jaxis, DL)) + geom_point()

Thanks again for your help.

Best,
 Art

Brandon Hurr

unread,
Feb 24, 2011, 3:02:50 PM2/24/11
to ggp...@googlegroups.com, Art
How about using as.factor()?

ggplot(jenn, aes(x=as.factor(rownames(jenn), levels= as.character(rownames(jenn))), y=DL) + geom_point()

Like James said... untested.  

dput() your dataframe next time and it'll make messing around much easier. 

Brandon


Thanks, 
Art

--

James McCreight

unread,
Feb 24, 2011, 3:06:59 PM2/24/11
to Brandon Hurr, ggp...@googlegroups.com, Art
ditto on the dput(). we all want to run it ourselves but dont like to do more than cut and paste.

also you may find even more instant gratification for less-ggplot centered questions by searching http://tolstoy.newcastle.edu.au/R/
eg rowMeans.

glad to see you're getting the hang of it.

J


--
-
******************************************************************************
James McCreight                                  mccreigh at colorado.edu
NASA Postdoctoral Fellow
cell: (831) 261-5149

Art

unread,
Feb 24, 2011, 3:09:48 PM2/24/11
to ggp...@googlegroups.com, Art
Thanks Brandon.

I was unfamiliar with as.factor.  I'll give it a try.

For future reference to help others with my posts how do I employ dput()?  

Thanks,
 Art

James McCreight

unread,
Feb 24, 2011, 3:18:12 PM2/24/11
to ggp...@googlegroups.com
this link is becoming more and more obscure... ha.

https://gist.github.com/270442

you'd paste the output of dput(jenn) into the email, preferably preceeded by "jenn <-"

even more generally ?dput


James


Thanks,
 Art

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options:

James McCreight

unread,
Feb 24, 2011, 3:00:08 PM2/24/11
to ggp...@googlegroups.com
ps if you want to plot more than DL, you could

jenn.melt=melt(jenn, id.vars='X')

ggplot(jenn.melt, aes(x=X, y=value, color=variable)) + geom_point()

or something similar. (code is untested)

that might be overkill for what you want, or the next logical step... 

Art

unread,
Feb 24, 2011, 5:26:49 PM2/24/11
to ggp...@googlegroups.com
Thanks James.

I will definitely use dput() in future questions.

Best,
 Art
Reply all
Reply to author
Forward
0 new messages