2
votes

I have the following data frame:

rs = data.frame(c("A1","A2","A3","A4","A5"), runif(5), runif(5), runif(5))
names(rs)=c("column", 2007,2008,2009)
rownames(rs) <- rs$column
rs <- rs[2:4]

         2007       2008       2009
A1 0.95786606 0.71139657 0.05623083
A2 0.22712574 0.84925031 0.80634618
A3 0.25313536 0.08571401 0.10015232
A4 0.03830769 0.23901711 0.73613390
A5 0.85697714 0.38597167 0.25826570

I would like to create a plot which plots 5 lines for each row in the data frame, e.g. line 1 would be corresponding to A1, plotting the three points (2007,0.95686606),(2008,0.71139657),(2009,0.05623083) and line 2 would be corresponding to A2, i.e. points (2007, 0.22712574) etc. So my columns would become the x-axis and the points the y-axis. Ideally I'd like these as separate graphs, but I can't find a way to put them into one graph right now.

Any help appreciated

1

1 Answers

3
votes
rs      <- as.data.frame(t(rs))
rs$year <- rownames(rs)
rs      <- melt(rs, id.vars=c("year"))


ggplot(rs, aes(x=as.numeric(year), y=value, color=variable)) + geom_line()

enter image description here

For multiple plots per page there are many solutions.

Simple example:

http://theanalysisfactor.com/r-multiple-graphs

Better example:

http://cran.r-project.org/web/packages/egg/vignettes/Ecosystem.html