首先,這是我的資料:
x <- structure(list(FIGURE = c("4", "4", "4", "4", "XXXIIIc", "XXXIIIc",
"XXXIIIc", "XXXVI", "XXXVI", "XXXVI", "XLIV", "XLIV", "XLIV",
"3", "3", "88", "88", "19", "19", "19", "19", "19", "18a", "18a",
"18a", "18b", "18c", "18c", "18c", "18c", "18c", "55", "Ensemble IX",
"2", "25", "25", "26", "26", "29", "54", "54", "130", "130"),
media_fecha = c(16382.5, 14759, 13776, 15941, 18189.5, 17546.5,
17422, 18084, 16898.5, 17987, 16422.5, 15729.5, 17615.5,
16016, 15864.5, 14643, 14132, 17146.5, 16619.5, 16424, 16317,
16328.5, 16317, 15679, 14834.5, 15467, 13197.5, 12691.5,
12683.5, 12384, 14545, 16053.5, 16669.5, 15234.5, 17036.5,
16608, 17428, 17439.5, 26785.5, 16813, 16651.5, 15432, 14655
), sd_fecha = c(266.25, 235.5, 131.5, 327.5, 210.25, 267.75,
267, 262, 239.25, 304, 283.75, 189.75, 266.75, 174, 294.75,
278.5, 331, 266.75, 204.75, 215.5, 196, 285.25, 196, 288.5,
250.25, 277, 63.75, 101.75, 100.75, 168, 337.5, 136.25, 212.75,
183.25, 199.25, 206.5, 193, 264.75, 312.25, 235.5, 299.25,
238.5, 312.5)), row.names = c(NA, -43L), class = c("tbl_df",
"tbl", "data.frame"))
該資料集由 3 列組成:
FIGURE:分類變數media_fecha:資料集的該元素/行的平均值。sd_fecha:資料集的該元素/行的標準偏差。
XLIV中的類別FIGURE由三行或元素組成。
我有興趣使用ggplot2.
- 例如,
XLIV用它們對應的均值和偏差來說明 的三個正態分布。并且,突出顯示重疊區域。
我該怎么做?
更新
考慮到它們相應的 2 西格瑪限制,我想給所有密度函式之間的重疊區域一個顏色。類似于 XXXIIIc 的示例:

uj5u.com熱心網友回復:
您可以按行對資料進行分組,用 100 個 x 和 y 值進行匯總,比方說,從平均值以下 4 sd 到平均值以上 4 sd,然后使用普通 old 進行繪圖geom_line。
如果要突出顯示所有曲線重疊的區域(在每個均值的 2 sd 以內),您可以找到所有曲線在該范圍內的 x 值,然后標記最高的 y 值,這適用于填充。
我們可以將所有這些包裝在一個函式中,以便于使用和保持一致性:
library(tidyverse)
plot_norms <- function(data, fig, ...) {
x %>%
filter(FIGURE == fig) %>%
mutate(min_range = min(media_fecha - 4 * sd_fecha),
max_range = max(media_fecha 4 * sd_fecha)) %>%
mutate(group = row_number()) %>%
rowwise() %>%
summarise(x = seq(min_range, max_range, length.out = 500),
y = dnorm(x, media_fecha, sd_fecha),
is_2d = x > media_fecha - 2 * sd_fecha &
x < media_fecha 2 * sd_fecha,
FIGURE = FIGURE, group = group) %>%
group_by(x) %>%
mutate(fill_me = all(is_2d) & rank(y) == max(rank(y))) %>%
ggplot(aes(x, y))
geom_area(data = . %>% filter(fill_me), ..., position = 'identity')
geom_line(aes(group = group))
ggtitle(paste("Normal distributions, figure", fig))
theme_minimal(base_size = 16)
}
所以現在你可以這樣做:
plot_norms(x, "19", fill = 'lightblue', alpha = 0.5)

plot_norms(x, "XXXIIIc", fill = 'red', alpha = 0.2)

創建于 2022-11-12,使用reprex v2.0.2
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/534938.html
標籤:r图表2意思是正态分布
上一篇:在ggplot中沿x軸添加空格
