-3

I have data like this:

      test_data <- data.frame(
      var0 = 100 + c(0, cumsum(runif(49, -20, 20))),
      var1 = 150 + c(0, cumsum(runif(49, -10, 10))),
      date = seq.Date(as.Date("2002-01-01"), by="1 month", length.out=100))

To plot both time series var0 and var1 on the same graph, with date on the x-axis, using ggplot2

             ggplot(test_data, aes(date)) + 
             geom_line(aes(y = var0, colour = "var0")) + 
             geom_line(aes(y = var1, colour = "var1"))

This will work fine and plot two time series in different colors but the title of Y axis and the legend will be"var0".

  • How to change the title of Y axis and the legend to,for example,variable , value
  • how to change the colors of both var0 and var1 lines

Thanks

hyat
  • 1,047
  • 4
  • 15
  • 34
  • 1
    All your questions have been already answered on stackoverflow, for example, [here](http://stackoverflow.com/questions/10438752/adding-x-and-y-axis-labels-in-ggplot2) and [here](http://stackoverflow.com/questions/5171263/r-changing-line-colors-with-ggplot) and [here](http://stackoverflow.com/questions/5911567/how-can-i-change-the-title-of-a-ggplot2-legend) – Didzis Elferts Nov 23 '13 at 08:36

1 Answers1

4

Try this:

ggplot(test_data, aes(date)) + 
geom_line(aes(y = var0, colour = "var0")) + 
geom_line(aes(y = var1, colour = "var1")) +
scale_colour_manual(values=c("green","yellow"), name="Experimental Condition", labels=c("Var 1", "Var 2")) +
ylab("Var X")
TWL
  • 2,290
  • 1
  • 17
  • 21
  • 1
    no worries, add argument `size=2` (2 being an example) to the `geom_line` function. – TWL Nov 23 '13 at 09:01