2

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

Hack-R
  • 22,422
  • 14
  • 75
  • 131
user33484
  • 740
  • 2
  • 9
  • 36

1 Answers1

4
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

Hack-R
  • 22,422
  • 14
  • 75
  • 131
  • 1
    Thanks! - Any ideas how to make it into multiple graphs? e.g. a graph for A1, for A2 for A3 etc. I have a solution which includes loops which I'm trying to avoid – user33484 Jul 06 '18 at 17:24
  • 1
    Simple example: https://www.theanalysisfactor.com/r-multiple-graphs/ Better example: https://cran.r-project.org/web/packages/egg/vignettes/Ecosystem.html There are also many other popular methods and libraries, `lattice`, `matplotlib`, etc. – Hack-R Jul 06 '18 at 17:47