简体   繁体   English

在R中如何使用ggplot绘制正态分布的尾部区域?

[英]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:我找到了一种通过组合两个 geom_area 图来创建带有尾部区域的正态分布来“破解”ggplot的方法:

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?是否有使用 ggplot 创建正态分布并像上面一样突出显示尾部区域的“不那么hackey”的方法?

First off, I like your approach;首先,我喜欢你的方法; not sure whether this is less "hackey", but here's another option using gghighlight不确定这是否不那么“hackey”,但这是使用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 .据我了解, gghighlight需要一个data参数,因此它本身无法与geom_area使用(意思是:没有data但有stat = "function" )或stat_function That's why I'm generating data df first.这就是我首先生成数据df的原因。


Update更新

In response to your comment about how to "highlight the area between 1 and -1" ;回应您关于如何“突出显示 1 和 -1 之间的区域”的评论 you can do the following您可以执行以下操作

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

在此处输入图片说明

Update 2更新 2

To highlight the region 1.5 < x < 2.5 simply use the conditional statement x > 1.5 & x < 2.5要突出显示区域1.5 < x < 2.5只需使用条件语句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.意思是,我还没有找到在单个gghighlight语句中突出显示x < -2.5 & x > 2.5gghighlight

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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