I have the following data set:
yr v1 v2 v3
1 18 17 34
2 23 36 31
3 29 34 30
4 37 16 25
5 31 22 19
yr= year/time variable. I want to plot three line diagrams (for v1,v2 and v3). Then introduce three horizontal bands on the plot area to show high/medium,low based on this range:
>= 20 : LOW
<20 and >= 30 : Medium
>30: High
I used ggplot to proceed like this:
first I stacked all the values in one column and created an "id" column of "var1",var2", "var3" to get three line diagrams:
test1:
yr values id
1 18 var1
2 23 var1
3 29 var1
4 37 var1
5 31 var1
1 17 var2
2 36 var2
3 34 var2
4 16 var2
5 22 var2
1 34 var3
2 31 var3
3 30 var3
4 25 var3
5 19 var3
ggplot(data = test1, aes(x = year, y = values, color = id)) + geom_line(aes(group = id)) + geom_point()
Next: I created a stacked plot for the range.
I assigned a value 40 to "high", combined all the 3 values in the range (20,30,40). Then I added a new column "name" showing high/medium/low.
test2:
yr values name
1 20 low
2 20 low
3 20 low
4 20 low
5 20 low
1 30 med
2 30 med
3 30 med
4 30 med
5 30 med
1 40 high
2 40 high
3 40 high
4 40 high
5 40 high
ggplot(data = test2, aes(x = year, y = values, fill = name)) + geom_area(position = 'stack')
While these two ggplots work fine individually, I don't know if it's possible to combine them in one plot to get: The three lines with three horizontal bands in the back ground.
Thanks a lot in advance.

structure(list(yr = 1:5, low = c(20, 20, 20, 20, 20), medium = c(30, 30, 30, 30, 30), high = c(40, 40, 40, 40, 40)), .Names = c("yr", "low", "medium", "high"), row.names = c(NA, -5L), class = "data.frame")position = "stack"(the default), you don't really want30and40formedandhigh; that would be the numbers forposition = "identity"(which works, but makes the colors overlap unpleasantly). To use"stack", you want20forlowand10for the other two, because that's how much higher you want them than the last bar. You'll also probably want to turnnameinto a factor with the levels in the appropriate order to get the stacking right.ggplot(data= test1, aes(year, values, color = id)) + geom_line() + geom_point() + geom_area(data = expand.grid (yr = test2$year,y = test2$values, band = as.factor(test2$name) ), aes(yr, y, color = NULL, fill = band), alpha = 0.2)