简体   繁体   中英

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

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

在此处输入图片说明

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

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)

在此处输入图片说明

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)

在此处输入图片说明

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)

在此处输入图片说明


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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM