[R] Multi-line graph?

Dieter Menne dieter.menne at menne-biomed.de
Fri Oct 15 19:51:50 CEST 2010



barnhillec wrote:
> 
> I'm trying to graph some simple music psychology data. Columns are musical
> intervals, rows are the initials of the subjects. Numbers are in beats per
> minute (this is the value at which they hear the melodic interval split
> into two streams). So here's my table:
> 
>      Tenth Fifth Third
> GG 112    152   168
> EC  100    120   140
> SQ  160   184    NA
> SK   120   100   180
> 
> I want a multi-line graph where the intervals are on the X axis, and the y
> axis is the beats per minute, and each subject has a different line. 
> 
> 
The most difficult part of it (at least that's what my students think) is
getting the data into the right format. If you have the changes to start
with the data in the "long" format, use it. What you need is:

init interval beats
GC   Tenth    112

In this case, reformatting almost works with the default version of the melt
function in package reshape.

It's good that you supplied a data example, but in general it is better if
you could provide it in a copy-and-paste format as shown below. I needed
more time to reformat the data than to write the rest.


Dieter


library(lattice)
library(reshape)
dt = data.frame(
           init  = c("GG","EC", "SQ","SK"),
           Tenth = c(112,100,160,120),
           Fifth = c(152,120,184,100),
           Third = c(168,140,NA,180))

# The data should look like this
#init interval beats
#GC   Tenth    112
#dtlong = melt(dt) # almost does it, but column "variable" is ugly
dtlong = melt(dt,variable_name="beats") # better
dtlong
           
xyplot(value~variable,groups=init,data=dtlong,type="l", 
    auto.key=list(lines=TRUE))





-- 
View this message in context: http://r.789695.n4.nabble.com/Multi-line-graph-tp2997402p2997435.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list