0

The line plot is given as such -

Line plot

Now, I have a dataframe which has both the mean and confidence interval as columns. I wanted to add the confidence interval as a shaded region around the line plot.

I tried to add the confidence interval using polygon but it gave me separate lines including the confidence interval as a curve and shaded region. Using Polygon

The code for the same is given below -

plot(row.names(df),df$`Point Forecast`,xlab='30 days of forecast',ylab='Cases',type='l',lwd=2,col='blue')
polygon(x=row.names(df),y=df$`Hi 95`,col=rgb(0.5,0.5,0.5,0.25))
polygon(x=row.names(df),y=df$`Lo 95`,col=rgb(0.5,0.5,0.5,0.25))

The dataframe is as such -

    Forecast       Low      High Days
1   93380.13  88617.45  98142.81  229
2   98406.60  93269.62 103543.58  230
3  101944.17  96715.20 107173.14  231
4  104451.55  99062.35 109840.74  232
5  104548.60  98916.06 110181.15  233
6  101948.87  96160.04 107737.70  234
7  100730.13  93877.35 107582.91  235
8  102627.38  94483.51 110771.26  236
9  105946.54  96958.65 114934.44  237
10 109369.26  99772.87 118965.65  238
11 111689.54 101513.19 121865.88  239
12 111629.45 100986.24 122272.65  240
13 110281.26  98925.13 121637.39  241
14 109906.05  97426.23 122385.88  242
15 111292.69  97583.75 125001.63  243
16 113973.91  99178.07 128769.75  244
17 116971.84 101210.18 132733.51  245
18 118885.46 102302.11 135468.81  246
19 119120.52 101724.31 136516.73  247
20 118590.55 100197.20 136983.90  248
21 118600.11  98993.22 138207.00  249
22 119812.74  98914.34 140711.14  250
23 122108.28  99949.76 144266.81  251
24 124617.50 101305.57 147929.43  252
25 126289.40 101920.54 150658.26  253
26 126815.90 101377.10 152254.71  254
27 126784.58 100156.77 153412.39  255
28 127066.83  99119.44 155014.21  256
29 128235.71  98896.34 157575.08  257
30 130220.61  99501.05 160940.17  258

1 Answers1

0

One option would be using ggplot2. You can add a ribbon for the high and low confidence limits with geom_ribbon(). Here the code.

library(ggplot2)
#Code
ggplot(df,aes(x = Point, y = Forecast)) +
  geom_line(color = "#14243e") +
  geom_ribbon(
    aes(ymin = Low, ymax = High),
    fill = 'midnightblue',
    alpha = 0.2)

Output:

enter image description here

Some data used:

#Data
df <- structure(list(Point = 229:258, Forecast = c(93380.13, 98406.6, 
101944.17, 104451.55, 104548.6, 101948.87, 100730.13, 102627.38, 
105946.54, 109369.26, 111689.54, 111629.45, 110281.26, 109906.05, 
111292.69, 113973.91, 116971.84, 118885.46, 119120.52, 118590.55, 
118600.11, 119812.74, 122108.28, 124617.5, 126289.4, 126815.9, 
126784.58, 127066.83, 128235.71, 130220.61), Low = c(88617.45, 
93269.62, 96715.2, 99062.35, 98916.06, 96160.04, 93877.35, 94483.51, 
96958.65, 99772.87, 101513.19, 100986.24, 98925.13, 97426.23, 
97583.75, 99178.07, 101210.18, 102302.11, 101724.31, 100197.2, 
98993.22, 98914.34, 99949.76, 101305.57, 101920.54, 101377.1, 
100156.77, 99119.44, 98896.34, 99501.05), High = c(98142.81, 
103543.58, 107173.14, 109840.74, 110181.15, 107737.7, 107582.91, 
110771.26, 114934.44, 118965.65, 121865.88, 122272.65, 121637.39, 
122385.88, 125001.63, 128769.75, 132733.51, 135468.81, 136516.73, 
136983.9, 138207, 140711.14, 144266.81, 147929.43, 150658.26, 
152254.71, 153412.39, 155014.21, 157575.08, 160940.17)), class = "data.frame", row.names = c(NA, 
-30L))
Duck
  • 39,058
  • 13
  • 42
  • 84
  • I tried doing as your code but it's giving me an empty plot and an error saying - `geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?` – SAIF UL MEHDI Sep 14 '20 at 13:05
  • @SAIFULMEHDI First try with the data I shared `df` and reproduce the code. I suspect you have to load `tidyverse` library. – Duck Sep 14 '20 at 13:08
  • I did it with your data and it gave as you have shown but for my data it isn't showing as such – SAIF UL MEHDI Sep 14 '20 at 13:12
  • @SAIFULMEHDI Nice, so there must be an issue with your variable names. Do you have the same names as in the data you provided? Please check `names(yourdf)` and let me know the output or move to chat with this https://meta.stackexchange.com/questions/187426/how-do-i-chat-with-another-user-on-stackoverflow – Duck Sep 14 '20 at 13:16
  • The name of the second column is Point Forecast and the first column is the index – SAIF UL MEHDI Sep 14 '20 at 13:24
  • You have to enquote the names when they have spaces with `` – Duck Sep 14 '20 at 13:28
  • I have modified the dataframe. But still it's giving me an empty plot – SAIF UL MEHDI Sep 14 '20 at 13:30
  • @SAIFULMEHDI It is strange I think the names are the issue, try leaving the same names as `df` for your real data, or move to chat in order to help you! – Duck Sep 14 '20 at 13:32
  • I have low reputation, so I cannot move to chat – SAIF UL MEHDI Sep 14 '20 at 13:36
  • @SAIFULMEHDI All people can do, just check the link I shared with you! – Duck Sep 14 '20 at 13:38
  • @SAIFULMEHDI Wait, you shared new data, right? – Duck Sep 14 '20 at 13:39
  • Data is the same but the names of the column have been modified – SAIF UL MEHDI Sep 14 '20 at 13:40
  • @SAIFULMEHDI try `ggplot(df,aes(x=Days,y=Forecast))+geom_line(color = "#14243e") +geom_ribbon( aes(ymin = Low, ymax = High), fill = 'midnightblue', alpha = 0.2)` – Duck Sep 14 '20 at 13:40
  • @SAIFULMEHDI Added a new option in comments! Please check if that works! – Duck Sep 14 '20 at 13:40
  • Nope, it's still giving an empty plot – SAIF UL MEHDI Sep 14 '20 at 13:42
  • @SAIFULMEHDI The issue must be the names, please check about the chat. Please use `names(yourdf)` and share the output in a comment! – Duck Sep 14 '20 at 13:43
  • names(df) `[1] "Forecast" "Low" "High" "Days"` – SAIF UL MEHDI Sep 14 '20 at 13:44
  • @SAIFULMEHDI Try re starting `R` and only load your data and `ggplot2`. It might be an issue with other package, alaso adjust the names for each variable in the `ggplot` design! – Duck Sep 14 '20 at 13:46
  • I did it and I tried plotting a histogram using `geom_histogram`, it did plot but when I am trying to plot a line using `geom_line` I'm getting the error `geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?` – SAIF UL MEHDI Sep 14 '20 at 13:59
  • @SAIFULMEHDI Try adding `group=1` in the first `aes()` or `group=Days` – Duck Sep 14 '20 at 14:02