How to Add Shaded Confidence Intervals to Line Plot with Specified Values

How to add shaded confidence intervals to line plot with specified values

You need the following lines:

p<-ggplot(data=data, aes(x=interval, y=OR, colour=Drug)) + geom_point() + geom_line()
p<-p+geom_ribbon(aes(ymin=data$lower, ymax=data$upper), linetype=2, alpha=0.1)

Sample Image

I have a line plot describing forecast of historical data, I want to add the confidence interval as a shaded region

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:

Sample Image

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))

Ploting 95% confidence interval line plot with shaded area in python

Answer

You can use seaborn.lineplot to do that, since seaborn uses 95% CI by default, but firstly you need to reshape your data through pandas.melt.

If you start from data in a dataframe df like the one you provided, you can reshape it with:

df = pd.melt(frame = df,
var_name = 'column',
value_name = 'value')

output:

   column     value
0 Ph1 -0.152511
1 Ph1 -0.068273
2 Ph1 0.425363
3 Ph1 -0.019332
4 Ph1 0.077907
5 Ph1 0.145185
6 Ph2 -0.039428
7 Ph2 0.152013
8 Ph2 -0.043105
9 Ph2 0.139712
10 Ph2 0.341410
11 Ph2 0.112060
12 Ph3 0.131173
13 Ph3 -0.315244
14 Ph3 0.071670
15 Ph3 -0.026001
16 Ph3 -0.113731
17 Ph3 0.093898
18 ph4 -0.002039
19 ph4 0.005247
20 ph4 -0.045124
21 ph4 -0.021844
22 ph4 -0.065799
23 ph4 0.028815
24 Ph5 0.008101
25 Ph5 0.014775
26 Ph5 -0.036135
27 Ph5 -0.040854
28 Ph5 -0.027229
29 Ph5 -0.032327
30 Ph6 -0.002039
31 Ph6 -0.045268
32 Ph6 -0.037250
33 Ph6 -0.050648
34 Ph6 -0.077948
35 Ph6 0.004239

Then you can plot this df with:

fig, ax = plt.subplots()

sns.lineplot(ax = ax,
data = df,
x = 'column',
y = 'value',
sort = False)

plt.show()


Whole code

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

df = pd.read_csv('data.csv')

df = pd.melt(frame = df,
var_name = 'column',
value_name = 'value')

fig, ax = plt.subplots()

sns.lineplot(ax = ax,
data = df,
x = 'column',
y = 'value')

plt.show()


Plot

Sample Image

average line plot with shaded confidence interval in ggplot2

From your description, the following should be what you want:

StoredCO215mln <- ggplot(i3) +
aes(x = tick, y = total_co2_emissions_captured, color = factor(subsidy_to_port)) +
geom_smooth(method = loess) +
labs(title = "Total CO2 captured in 32 years (subsidy = 15mln)", y = "Amount CO2 captured") +
scale_color_discrete(name = "Subsidy to port (%)")

Since you mentioned “average line” I’ve used LOESS regression but you might want to play around with this and try others, e.g. simple linear regression.

How to add shaded area to line plot with the ggh4x package?

Following the post you referenced you could achieve your desired result by making separate columns with the values for each team using e.g. pivot_wider, add the lines via two geom_line and then apply stat_difference:

library(tidyverse)
library(ggh4x)
library(reshape2)

plot_dat <- pivot_wider(plot_dat, names_from = team, values_from = value)

ggplot(plot_dat, aes(x = week)) +
geom_line(aes(y = `Team A`, color = "Team A")) +
geom_line(aes(y = `Team B`, color = "Team B")) +
facet_wrap(~variable, scales = "free_y") +
stat_difference(aes(ymin = `Team B`, ymax = `Team A`), alpha = 0.3)

Sample Image

Draw three lines with CIs according to a column

The first step, to plot 3 lines, one for each w_mean is straightforward. If the aesthetic defining the color (among others) is a factor, ggplot will automatically separate the lines.

library(ggplot2)

g <- ggplot(df1, aes(x_mean, m_mean, color = factor(w_mean))) +
geom_line()
g

Sample Image

Now add the confidence bands and make the legend title prettier.

g + geom_ribbon(aes(ymin = LLCI, ymax = ULCI, 
fill = factor(w_mean)),
alpha = 0.25,
color = NA,
show.legend = FALSE) +
labs(color = "w_mean")

Sample Image

Data

df1 <-
structure(list(x_mean = c(1.6667, 2.6667, 4, 1.6667, 2.6667,
4, 1.6667, 2.6667, 4), w_mean = c(3.1, 3.1, 3.1, 4, 4, 4, 4.556,
4.556, 4.556), m_mean = c(3.1565, 3.2456, 3.3644, 2.9842, 3.2712,
3.6539, 2.8777, 3.287, 3.8328), se = c(0.1164, 0.0683, 0.107,
0.0727, 0.049, 0.0774, 0.0933, 0.0666, 0.1136), LLCI = c(2.9273,
3.111, 3.1539, 2.841, 3.1747, 3.5015, 2.6941, 3.1559, 3.6092),
ULCI = c(3.3856, 3.3801, 3.575, 3.1273, 3.3677, 3.8064, 3.0614,
3.4182, 4.0563)), class = "data.frame", row.names = c(NA,
-9L))

gradient shaded confidence interval

The densregion() command in the denstrip package seems to do what you want. A little adaptation from the example in its help page:

require(denstrip)
x <- 1:10
nx <- length(x)
est <- seq(0, 1, length=nx)^3
se <- seq(.7,1.3,length.out=nx)/qnorm(0.975)
y <- seq(-3, 3, length=100)
z <- matrix(nrow=nx, ncol=length(y))
for(i in 1:nx) z[i,] <- dnorm(y, est[i], se[i])
plot(x, type="n", ylim=c(-3, 3),xlab="")
densregion(x, y, z)
lines(x,est,col="white")

Sample Image



Related Topics



Leave a reply



Submit