[R] ggplot simple question.

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sat May 12 18:08:42 CEST 2012


On Sat, 12 May 2012, rmje wrote:

> I have a matrix like this
>
> Name                                   1                            2
> 3                                 4                            5
> NM_001039514	1.033557047	0.7469879518	0.9004524887	0.8613861386	0.7952499048
> NM_001039723	1.0759493671	1.2315789474	0.8666666667	1.1142857143
> 0.9428011471
> NM_001042605	0.9897435897	0.8870431894	1.1038062284	0.7407407407	0.744530664
> NM_001048207	1.0070422535	0.9319727891	0.9015151515	0.8296438884
> 0.7290217712
>
> I want to plot each row in a single graph and then use multiplot to put them
> all together in a single graph.
> How do you plot the rows?

Please use dput() to generate R commands to recreate your sample data.
For example, I type "dta <- " and then copy-paste the output of dput(dta):

dta <- structure(list(Name = structure(1:4, .Label = c("NM_001039514",
"NM_001039723", "NM_001042605", "NM_001048207"), class = "factor"),
     X1 = c(1.033557047, 1.0759493671, 0.9897435897, 1.0070422535
     ), X2 = c(0.7469879518, 1.2315789474, 0.8870431894, 0.9319727891
     ), X3 = c(0.9004524887, 0.8666666667, 1.1038062284, 0.9015151515
     ), X4 = c(0.8613861386, 1.1142857143, 0.7407407407, 0.8296438884
     ), X5 = c(0.7952499048, 0.9428011471, 0.744530664, 0.7290217712
     )), .Names = c("Name", "X1", "X2", "X3", "X4", "X5"), class = 
"data.frame", row.names = c(NA,
-4L))

You have stated that this is a matrix, but it obviously contains text for 
row names.  The standard output of write.table doesn't put a heading above 
row names, so this looks more like a data frame. I don't know how your row 
names were set up as numerics... when I used read.table() to pull your 
example in, the headings were changed to valid data frame column names by 
prepending "X".  Your use of dput() would have made all thes questions 
irrelevant.

AFAIK all plotting in R assumes data series are vectors or columns, not 
rows, so some data structure transformation is needed before your 
desired plots can be generated.

There is probably more than one way to skin this cat, but here is one way:

library(reshape2)
library(ggplot2)
dtalong <- melt( dta, "Name" )
dtalong$x <- as.numeric( substring( as.character( dtalong$variable ), 2 ) )
ggplot( dtalong, aes( x=x, y=value ) ) +
   geom_point() +
   facet_wrap( ~Name, ncol=1 )

If you are not familiar with melt, use ?melt to read the help and 
experiment with it, and use the str() function to study dta and dtalong 
to see how the data are moved around.

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k



More information about the R-help mailing list