-5

I have three columns of data, titled Date, Upstream, and Downstream. I would like to create a graph that has Date on the x-axis and Upstream and Downstream as two separate lines.

I formatted 'Date' as yyyy-mm-dd, but I am having troubles creating a line graph using this format. Here's what I've tried so far.

ggplot(data1, aes(Date, Upstream)) + 
  geom_line() +
  scale_x_date (format = "%b-%Y") + 
  xlab("") + ylab("Height") 
hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
  • What code did you try? Please make a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) if you would like further assistance. – MrFlick Jun 16 '14 at 19:19
  • Welcome to Stack Overflow! Please don't just ask us to solve the problem for you. Show us how *you* tried to solve the problem yourself, then show us *exactly* what the result was and tell us why you feel it didn't work. See "[What Have You Tried?](http://whathaveyoutried.com/)" for an excellent article that you really need to read. – starsplusplus Jun 16 '14 at 19:36
  • I tried the following: ggplot(data1, aes(Date, Upstream)) + geom_line() + scale_x_date (format = "%b-%Y") + xlab("") + ylab("Height") – user3746122 Jun 16 '14 at 19:37
  • When you add new, important information like this, please edit your original question rather than adding it to the comments. – MrFlick Jun 16 '14 at 20:03
  • And when you edit your question, do put in a data example preferably using dput: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – IRTFM Jun 16 '14 at 20:07

1 Answers1

1

I'm going to assume you data looks something like this

data1 <- data.frame(
   date=seq(as.Date("2012-01-01"),as.Date("2012-01-31"), by="1 day"),
   Upstream = runif(31,5,7),
   Downstream = runif(31,2,3.5)
)

Now, in order to plot two different lines form different columns, we need to add a layer for each line. We do this with multiple calls to geom_line with different aesthetics. Here's a possibility using this test data.

library(ggplot2)
library(scales)

ggplot(data1, aes(x=date)) + 
   geom_line(aes(y=Upstream, color="up")) + 
   geom_line(aes(y=Downstream, color="down")) + 
   scale_x_date(labels = date_format("%b-%Y")) + 
   xlab("") + ylab("Height") +
   scale_color_manual(name="Stream", values=c(up="blue",down="red"))

Which results in

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295