1

I have a data frame, where I am talking about different flows of water at a dam (water units are kcfs—1000 cubic feet per second—if anyone is interested)

Call it df4plot

    date         kcfs    Flowtype
    10/1/2010        50     Power
    10/1/2010        10     Spill_Overgen
    10/1/2010         8         Spill_Force
    10/2/2010        52     Power
    10/2/2010         7     Spill_Overgen
    10/2/2010        10     Spill_Force     

(there are 3x365 rows in the data frame)

So what I want to do is make an aggregated area graph that shows each of these flows

p <- ggplot(data = df4plot, aes(date,kcfs)) +
geom_area(aes(colour = Flowtype, fill=Flowtype), position = “stack”) 

I want to control the colors used, so I added

plot_colors_aggregate <- c("forestgreen","lightsalmon","dodgerblue")
p <- p +
scale_color_manual(values = plot_colors_aggregate) +
scale_fill_manual(values = plot_colors_aggregate)

Now I want to add a dashed line, showing the maximum turbine capacity—the flow limits for power generation—that vary by month. I have a separate dataframe for this (365 rows long), df4FGline

    Date    FGlimit
    10/1/2010   52
    10/2/2010   52
     …
    11/1/2010   60
    11/2/2010   60
    ...
    Etc

So now I have

p <- p + 
geom_line(data = df4FGline, aes(x=date,y=FGlimit), colour = “darkblue”, linetype = “dashed”)

p

The legend is currently just the three blocks for the three types of Flowtype. I’d like to add the dashed line for the flow gate limits to the bottom, but I can’t get it to show up there.

It is probably related to my incomplete understanding of aes (help(aes) is AMAZINGLY unhelpful).

I’ve tried something similar to this and something similar to this, but since I’m only trying to add 1 line to a pre-existing legend, maybe?, this is not working for me.

I tried adding “legend = TRUE” inside the parentheses for the geom_line, but it put a dashed line inside each color box in the legend, AND created a 4th entry for the legend, but offset from the rest of the legend (below and to the right)... ARG!

I swear I have the book on order... any help you can share so that I understand this aesthetic thing and how it relates to the legend a little better, I'd be extremely grateful.

edited for typo

Community
  • 1
  • 1
  • 2
    Here's a hint: ggplot will automatically create a legend if you _map_ an aesthetic (inside of `aes()`), but not if you _set_ an aesthetic. – joran Aug 15 '12 at 22:33
  • But aren't both of my geoms doing this mapping thing, using aes()? Can you point me to anywhere I can read about this difference between _mapping_ and _setting_ I'd be grateful. – user1156870 Aug 15 '12 at 23:29
  • Linetype is set outside of aes() in geom_line. – joran Aug 16 '12 at 01:02
  • Thank you so much for your help on this I appreciate it. So _set_ is outside of aes() but inside geom_line(); _map_ is inside of aes(). when I move "linetype = 'dashed'" into aes()--when I _map_ it--my line is no longer dashed, but now I have a legend entry. If I have linetype = "dashed" in both locations, it goes back to my first version, with no legend entry. Is there a way to get _both_ a legend entry _and_ a dashed line? – user1156870 Aug 17 '12 at 14:37

1 Answers1

1

This should help:

df <- data.frame(x = 1:10,y = 1:10)
ggplot(df,aes(x = x,y = y)) + 
   geom_line(aes(linetype = "dashed")) + 
   scale_linetype_manual(name = "Linetype",values = "dashed")
joran
  • 169,992
  • 32
  • 429
  • 468
  • @user156870 Just so you know, the reason I didn't put more time into your question is that your example code wasn't terribly reproducible. Notice that my example code above can simply be copy+pasted and run in an R session. Not so with your code, and that usually makes it much more difficult to help. – joran Aug 17 '12 at 14:44