[R] ggplot2 graphing multiple lines of data

John Kane jrkrideau at yahoo.ca
Mon Sep 14 16:51:42 CEST 2009


Some day I may figure out how ggplot2 works.

I am trying to plot 5 columns of data on a graph (similar to a simple matplot)

===========================================================================
library(ggplot2)

bmi <- structure(list(pct = 2:21, P10 = c(14.6, 14.5, 14.2, 13.9, 13.7, 
13.7, 13.9, 14.2, 14.5, 14.8, 15.3, 15.9, 16.6, 17.2, 17.8, 18.1, 
18.3, 18.4, 18.5, 18.6), P25 = c(15.2, 15, 14.8, 14.6, 14.4, 
14.5, 14.8, 15.1, 15.5, 15.9, 16.4, 17.1, 17.8, 18.5, 19.1, 19.4, 
19.6, 19.7, 19.8, 19.9), P50 = c(16, 15.8, 15.5, 15.4, 15.4, 
15.6, 16, 16.4, 16.9, 17.4, 18, 18.7, 19.5, 20.2, 20.8, 21.1, 
21.4, 21.5, 21.6, 21.7), P75 = c(16.8, 16.7, 16.5, 16.4, 16.5, 
16.9, 17.5, 18.1, 18.7, 19.4, 20.1, 20.9, 21.6, 22.4, 23, 23.4, 
23.6, 23.8, 24, 24.1), P90 = c(17.6, 17.5, 17.5, 17.5, 17.8, 
18.4, 19.2, 20.1, 21, 21.8, 22.6, 23.4, 24.2, 24.9, 25.5, 25.9, 
26.3, 26.6, 27, 27.3)), .Names = c("pct", "P10", "P25", "P50", 
"P75", "P90"), class = "data.frame", row.names = c(NA, -20L))

# simple matplot example
matplot(bmi[,2:6], type="l")
===========================================================================
I thought that I should be able to do it following the example from pg.50 of Hadley's  ggplot pdf book.
===========================Hadleys example===========================
library(nlme) # to get Oxboy dataset
p <- ggplot(Oxboys, aes(age, height, group = Subject)) +
geom_line()
p


========================NON-WORKING CODE=============================
bmm <- melt(bmi, id=c("pct"))
names(bmm) <- c("age", "pp", "bm")
bmm[,2] <- as.ordered(bmm[,2])

p  <- ggplot(bmm, aes(x="age", y="bm", colour="pp", group="pp")) 
p <- p + geom_line()
p

===============================================================

[[elided Yahoo spam]]
Would someone kindly point out what supidity I am commiting here?
Thanks




More information about the R-help mailing list