1

I am trying to plot two flows and one rainfall data in one graph. I have broke it up into top and bottom parts as shown in the following pic. Here I have two issues with this plots and spent ages but cannot solve it.

  1. Why the observed flow always in black, even I have set it up as blue? Did I accidentally used some other arguments to overwrite it?
  2. The most importantly is, how do I able to add a legend for the bottom plot? I tried many different codes but they don't seem to work for me.

    x = data.frame(date = Date, rain = Obs_rain, obsflow = Obs_flow,simflow=Sim_flow)
    
    g.top <- ggplot(x, aes(x = date, y = rain, ymin=0, ymax=rain)) +
             geom_linerange() +
             scale_y_continuous(trans = "reverse") +
             theme_bw() +
             theme(plot.margin = unit(c(1,5,-30,6),units="points"),
             axis.title.y = element_text(vjust =0.3)) +
             labs(x = "Date",y = "Rain(mm)")
    
    g.bottom <- ggplot(x, aes(x = date, y = obsflow, ymin=0, ymax=obsflow), colour = "blue",size=0.5) +
             geom_linerange() +  #plot flow
             geom_linerange(aes(y = simflow, ymin=0, ymax=simflow), colour = "red", size =0.5)+ 
             labs(x = "Date", y = "River flow (ML/day)") +
             theme_classic() +
             theme(plot.background = element_rect(fill = "transparent"),
             plot.margin = unit(c(2,0,1,1),units="lines"))
    
    grid.arrange(g.top,g.bottom, heights = c(1/5, 4/5)) 
    

    enter image description here

Update:

I have resolved the issue with blue line colour. I accidently put arguments in the wrong place. But I'm still struggling with the legend.

    g.bottom <- ggplot(x, aes(x = date, y = obsflow, ymin=0, ymax=obsflow)) +
                geom_linerange(colour = "blue",size=0.5) +  #plot flow
Yu Deng
  • 1,051
  • 4
  • 18
  • 35
  • 1
    for the bottom plot. Melt your data, i.e. you should have 3 columns: 1=date, 2=flow (obs vs sim), 3=value . Then use aes(x=data,y=value,color=flow) – Pierre Jul 01 '14 at 07:04
  • Hi, Pierre, I am not sure when you said 3 columns :1=date, 2=flow (obs vs sim), 3=value . Could you kindly explain what do you mean by the second or the third column? – Yu Deng Jul 02 '14 at 08:00

1 Answers1

2

As an explanation of what @pierre means... turn your data from "wide" to "long" format using reshape2::melt, so that the flow type for each date is in one column flow_type, and the value is another (flow_val). Then you specify flow_type as the grouping variable with which to assign colour:

require(reshape2)

x.melted <- melt(x, id.vars = c("date", "rain"), variable.name="flow_type",
                 value.name="flow_val")

g.bottom <- ggplot(x.melted, aes(x = date),size=0.5) +
  geom_linerange(aes(ymin=0, ymax=flow_val, colour=flow_type)) +  #plot flow
  labs(x = "Date", y = "River flow (ML/day)") +
  theme_classic() +
  theme(plot.background = element_rect(fill = "transparent"),
        plot.margin = unit(c(2,0,1,1),units="lines"), 
        legend.position="bottom") + 
  scale_colour_manual(guide = guide_legend(title = "Flow Type"), 
                      values = c("obsflow"="blue", "simflow"="red"))
andyteucher
  • 1,393
  • 14
  • 21
  • Thanks a lot, I really appreciate it. But I am also curious if I want to use different line type, I just add `linetype=flow_type` after `colour=flow_type` but how can I plot legend in one slot? Right now it's one legend for color and one for line type. – Yu Deng Jul 03 '14 at 05:32
  • Try this answer here: http://stackoverflow.com/questions/12410908/creating-a-ggplot-legend-with-both-color-and-shape – andyteucher Jul 03 '14 at 16:26