In R how to plot the tail area of a normal distribution using ggplot?

Viewed 933

I found a way to "hack" ggplot by combining two geom_area plots to create a normal distribution with a tail area:

library(ggplot2)
mean <-  0
standard_deviation <- 1
Zscore <- -1.35

observation = (Zscore*standard_deviation) + mean
(tail_area <- round(pnorm(observation),2))

ggplot(NULL, aes(c(-5,5))) +
    geom_area(stat = "function", fun = dnorm, fill="sky blue", xlim = c(-5, -1.35)) +
    geom_area(stat = "function", fun = dnorm,  xlim = c(-1.35, 5))

enter image description here

Is there "not so hackey" approach using ggplot to create normal distributions and highlighting tail areas like above?

1 Answers

First off, I like your approach; not sure whether this is less "hackey", but here's another option using gghighlight

# Generate data (see comment below)
library(dplyr)
df <- data.frame(x = seq(-5, 5, length.out = 100)) %>% mutate(y = dnorm(x))

# (gg)plot and (gg)highlight
library(ggplot2)
library(gghighlight)
ggplot(df, aes(x, y)) + geom_area(fill = "sky blue") + gghighlight(x < -1.35)

enter image description here

From what I understand, gghighlight needs a data argument, so it won't work with geom_area by itself (meaning: without data but with stat = "function"), or with stat_function. That's why I'm generating data df first.


Update

In response to your comment about how to "highlight the area between 1 and -1"; you can do the following

ggplot(df, aes(x, y)) + geom_area(fill = "sky blue") + gghighlight(abs(x) < 1)

enter image description here

Update 2

To highlight the region 1.5 < x < 2.5 simply use the conditional statement x > 1.5 & x < 2.5

ggplot(df, aes(x, y)) + geom_area(fill = "sky blue") + gghighlight(x > 1.5 & x < 2.5)

enter image description here


To pre-empt potential follow questions: This method will only work for contiguous regions. Meaning, I haven't found a way to highlight x < -2.5 & x > 2.5 in a single gghighlight statement.

Related