-2

Given the following data:

data =

1 2.3
1 3.4
1 2.1
2 4.3
2 5.3
2 6.2
3 0.2
3 0.3
3 0.4

I need to plot these data as 3 different series:

  • 1st curve: when the 1st column is equal to 1

  • 2nd curve: when the 2nd column is equal to 2

  • 3rd curve: when the 3rd column is equal to 3

How can I do this in the most flexible way (using different colors)?

Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217
  • Your question mentions 1st column, 2nd column, 3rd column but your data does not have three columns. Nor do you mention what you've tried. Anyway, consider the `ggplot2` package; for instance, http://stackoverflow.com/questions/14688204/proper-way-to-plot-multiple-data-series-in-ggplot-with-custom-colors-legend-etc – Sam Firke Mar 23 '15 at 13:14

1 Answers1

1

In this solution I assume that your first column is your grouping variable; the second column the variable that you'd like in the Y-axis. I added a variable x for the time-series.

Data

df<-data.frame(v1=as.factor(c(1,1,1,2,2,2,3,3,3)), v2=as.numeric(c(2.3, 3.4, 2.1, 4.3, 5.3, 6.2, 0.2, 0.3, 0.4)))

df$x<-c(1,2,3,1,2,3,1,2,3)

ggplot

library(ggplot2)

ggplot(df, aes(x,v2, group=v1, colour=v1)) + geom_line()

enter image description here

Ruthger Righart
  • 4,799
  • 2
  • 28
  • 33