0

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.

7
  • Please improve the question. 1) better to use dput to provide the data 2) Its' not clear clear what test2 is. If it is the result of the of the paragraph before it "Next: I created ..." then please provide the code. As written you leave too much work just to try and recreate the data Commented Jan 8, 2017 at 4:33
  • @epi99 Sorry for not being clear earlier. I will try the answer below. But hope the question now makes more sense. Commented Jan 8, 2017 at 4:52
  • its much clearer but still would be better with dput. dput outputs executable code that recreates the data structure. Here is an example of the output from dput 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") Commented Jan 8, 2017 at 5:09
  • 1
    If you're using position = "stack" (the default), you don't really want 30 and 40 for med and high; that would be the numbers for position = "identity" (which works, but makes the colors overlap unpleasantly). To use "stack", you want 20 for low and 10 for the other two, because that's how much higher you want them than the last bar. You'll also probably want to turn name into a factor with the levels in the appropriate order to get the stacking right. Commented Jan 8, 2017 at 5:16
  • Hi @alistaire followed your way to consider test1 for the line diagrams and test2 for my bands. Ran the following code. It does not give me the nice plot you generated. Could you please tell me where I am wrong : 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) Commented Jan 8, 2017 at 20:57

1 Answer 1

2

To plot all the lines at once, you need to gather to long form so you can pass all the variables to the y aesthetic as one. To plot the bands from a separate data.frame, you'll need to pass geom_area a new data parameter and aesthetic mapping instead of relying on the inherited one.

Without test2 this is an approximation, but all together, roughly,

library(tidyr)    # for `gather`
library(ggplot2)

test1 %>% gather(variable, value, -yr) %>%    # gather to long form
    ggplot(aes(yr, value, color = variable)) + 
    geom_area(data = expand.grid(yr = test1$yr,    # new data.frame for bands
                                 y = 10, 
                                 band = c('very low', 'low', 'medium', 'high')), 
              aes(yr, y, color = NULL, fill = band),    # new aesthetic for bands
              alpha = 0.2) + 
    geom_line() + geom_point()    # these can inherit aes from ggplot

multi-line plot with bands

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.