[R] (no subject)

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Tue Jun 24 16:52:34 CEST 2014


Reproducibility == good (except for mixing in invalid code and failing to 
set RNG seed)
HTML email == bad
Missing subject == bad
Using matrices with ggplot == pushing a rope
Using for loops to build a ggplot == missing the point
Using wide-form data with ggplot == pushing a rope

Study the following (the str() function is your friend, and learning how 
factors work will help you map colors where you want them in more 
complicated examples):

library(ggplot2)
library(reshape2)
set.seed(42)
xaxis <- rnorm( 30 ) + 10
mydata <- replicate( 9, rnorm( 30 ) + 20 )
mydf <- data.frame( xaxis=xaxis, mydata )
colour_scheme <- rainbow( ncol( mydata ) )
mydf.long <- melt( mydf, id.vars="xaxis" )
ggplot( mydf.long, aes( x=xaxis, y=value, color=variable ) ) +
   geom_line() +
   scale_colour_manual( values=colour_scheme, guide=FALSE )


On Tue, 24 Jun 2014, Zayd Farah wrote:

> I hope the following script is reproducible enough to highlight my issue,
> which is to automatically (in this case by loop, but it does not
> necessarily have to be by a loop) add geom_line layers for each column of
> mydata to the ggplot variable p1. for some reason doing this only works by
> manually adding each layer:
>
> library(ggplot2)
> library(gtable)
>
>
> #creates set of reproducible data
> xaxis<-rnorm(30)+10
> xaxis
> mydata<-replicate(9, rnorm(30)+20)
>
> #creates a matrix of colors to identify each line on the final plot of
> #each column of mydata
>
> colour_scheme<-palette(rainbow(dim(mydata)[2]))
>
> # Main scatterplot
> p1 <- ggplot(NULL)
>
> #ideal code that would create a geom_line layer for each column of mydata
> vs. xaxis
> for (i in 1:dim(mydata)[2])
>    {
>      p1=p1+geom_line(aes(xaxis,mydata[,i]),colour=colour_scheme[i])
>    }
> p1
>
> #run the following code to see what it SHOULD look like:
> p1 <- ggplot(NULL)
> p1=p1+geom_line(aes(xaxis,mydata[,1]),colour=colour_scheme[1])
> p1=p1+geom_line(aes(xaxis,mydata[,2]),colour=colour_scheme[2])
> p1=p1+geom_line(aes(xaxis,mydata[,3]),colour=colour_scheme[3])
> p1=p1+geom_line(aes(xaxis,mydata[,4]),colour=colour_scheme[4])
> p1
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
>

---------------------------------------------------------------------------
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