1

I have two time series data that I want to show on the same graph. Both series have three columns: Date, County and Value. Here is my code:

#Data
Series1 <- data.frame(Date = c(2000,2001,2000,2001), County = c("a", "a", "b", "b"),Total = c(100,150,190,130))
Series2 <- data.frame(Date = c(2000,2001,2000,2001), County = c("a", "a", "b", "b"),Total = c(180,120,140,120))

#Plot data
ggplot() + 
    geom_line(data = Series1, aes(x = Date, y = Total, color = County), linetype="solid") +
    geom_line(data = Series2, aes(x = Date, y = Total, color = County), linetype="dashed")

The plot looks like this:

plot

Now I just need to add one legend showing that solid line represents Series1 and dashed line represents Series2. How can I do this?

Sean
  • 13
  • 1
  • 4
  • 1
    Possible duplicate of [R: changing-the-line-type-in-the-ggplot-legend](http://stackoverflow.com/questions/14875582/changing-the-line-type-in-the-ggplot-legend) – PereG Jan 08 '16 at 15:30
  • Possible duplicate of [R: legend with points and lines being different colors (for the same legend item)](http://stackoverflow.com/questions/19053440/r-legend-with-points-and-lines-being-different-colors-for-the-same-legend-item) – aosmith Jan 08 '16 at 16:58
  • @PereG I tried +scale_linetype_manual("Type",values=c("solid"=2,"dashed"=1)), but nothing happen. – Sean Jan 08 '16 at 19:54
  • @Sean if you provide a minimum reproducible example, it would be much easier to help – PereG Jan 08 '16 at 20:05
  • @PereG I added a small subset of my data as example, hope it will help. TIA. – Sean Jan 08 '16 at 21:17

2 Answers2

1

Legends are created automatically when you use aes() to map a data column to an aesthetic. You don't have a data column that you're mapping to the linetype aesthetic, so we need to create one.

Series1$series = 1
Series2$series = 2
all_series = rbind(Series1, Series2)

Now we've all the data together and plotting is easy:

ggplot(all_series,
       aes(x = Date, y = Total, color = County, linetype = factor(series))) + 
    geom_line()

Automatic legend, only one geom_line call, using ggplot as it's meant to be used: with tidy data.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • I agree with Gregor that the right thing is to manipulate the data previously. – PereG Jan 08 '16 at 21:47
  • Thank you both @PereG@Gregor. It works well. My follow-up question is: When I made a bunch of figures, sometimes the "County" legend box comes first and sometimes the "Series" legend box does. Is there a way to make them consistent? – Sean Jan 11 '16 at 15:43
  • Explicitly define it using an `order` argument to `guide_legend` as shown here: http://stackoverflow.com/a/11397958/903061. And do make a habit of searching for answers before asking. This was the first hit searching Stack Overflow for "ggplot2 order legends". – Gregor Thomas Jan 11 '16 at 17:09
1

You are really close...

ggplot() + 
      geom_line(data = Series1, aes(x = Date, y = Total, color = County), linetype="solid") +
      geom_line(data = Series2, aes(x = Date, y = Total, color = County), linetype="dashed")+ scale_linetype_manual()

enter image description here

PereG
  • 1,796
  • 2
  • 22
  • 23