[英]In R how to plot the tail area of a normal distribution using ggplot?
我找到了一種通過組合兩個 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))
是否有使用 ggplot 創建正態分布並像上面一樣突出顯示尾部區域的“不那么hackey”的方法?
首先,我喜歡你的方法; 不確定這是否不那么“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)
據我了解, gghighlight
需要一個data
參數,因此它本身無法與geom_area
使用(意思是:沒有data
但有stat = "function"
)或stat_function
。 這就是我首先生成數據df
的原因。
回應您關於如何“突出顯示 1 和 -1 之間的區域”的評論; 您可以執行以下操作
ggplot(df, aes(x, y)) + geom_area(fill = "sky blue") + gghighlight(abs(x) < 1)
要突出顯示區域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)
搶占潛在的后續問題:此方法僅適用於連續區域。 意思是,我還沒有找到在單個gghighlight
語句中突出顯示x < -2.5 & x > 2.5
的gghighlight
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.