Filling Area Under A Curve
0.1 Libraries
library(data.table)
library(ggplot2)
So this just shows a few ways to fill in the area under a curve using ggplot2.
1 Make a Distribution
First I’m going to make a normal distribution
normalDistribution <- data.frame(
x = seq(-4,4, by = 0.01),
y = dnorm(seq(-4,4, by = 0.01))
)
2 Two-tailed
Now define the critical values.
criticalValues <- qnorm(c(.025,.975))
shadeNormalTwoTailedLeft <- rbind(c(criticalValues[1],0), subset(normalDistribution, x < criticalValues[1]))
shadeNormalTwoTailedRight <- rbind(c(criticalValues[2],0), subset(normalDistribution, x > criticalValues[2]), c(3,0))
And make the plot.
ggplot(normalDistribution, aes(x,y)) +
geom_line() +
geom_polygon(data = shadeNormalTwoTailedLeft, aes(x=x, y=y, fill="red")) +
geom_polygon(data = shadeNormalTwoTailedRight, aes(x=x, y=y, fill="red")) +
guides(fill="none") +
geom_hline(yintercept = 0) +
geom_segment(aes(x = criticalValues[1], y = 0, xend = criticalValues[1], yend = dnorm(criticalValues[1]))) +
geom_segment(aes(x = criticalValues[2], y = 0, xend = criticalValues[2], yend = dnorm(criticalValues[2])))
3 Right-tailed
criticalValue <- qnorm(.95)
shadeNormal <- rbind(c(criticalValue,0), subset(normalDistribution, x > criticalValue), c(3,0))
ggplot(normalDistribution, aes(x,y)) +
geom_line() +
geom_polygon(data = shadeNormal, aes(x=x, y=y, fill="red")) +
guides(fill="none") +
geom_hline(yintercept = 0) +
geom_segment(aes(x = criticalValue, y = 0, xend = criticalValue, yend = dnorm(criticalValue)))
4 Left-tailed
criticalValue <- qnorm(.15)
shadeNormal <- rbind(c(criticalValue,0), subset(normalDistribution, x < criticalValue))
ggplot(normalDistribution, aes(x,y)) +
geom_polygon(data = shadeNormal, aes(x=x, y=y, fill="red")) +
geom_line() +
guides(fill="none") +
geom_hline(yintercept = 0) +
geom_segment(aes(x = criticalValue, y = 0, xend = criticalValue, yend = dnorm(criticalValue)))