gpt4 book ai didi

r - 如果没有y变量,则在x轴上的间隔上遮盖geom_density

转载 作者:行者123 更新时间:2023-12-04 03:35:50 25 4
gpt4 key购买 nike

我在geom_density曲线下难以阴影处理仅具有x变量的概率分布。我想在x> 0.05的区域下对图进行阴影处理。仅当包含y变量时,R上的其他线程才起作用。

使用这些随机生成的分布值:

a <- c(-0.1125, -0.1405, -0.1038, -0.1246, -0.1381, -0.1281, -0.144, 
-0.1377, -0.1287, -0.1119, -0.1553, -0.1578, -0.154, -0.1379,
-0.1506, -0.1166, -0.09943, -0.1689, -0.1794, -0.1632, -0.175,
-0.1561, -0.1143, -0.1952, -0.1865, -0.1478, -0.1556, -0.1175,
-0.1098, -0.1224, -0.09501, -0.1164, -0.2199, -0.1501, -0.1461,
-0.08725, -0.1158, -0.1917, -0.1405, -0.1081, -0.1013, -0.07569,
-0.121, -0.1811, -0.1248, -0.1255, -0.09941, -0.1829, -0.212,
-0.1053, -0.1311, -0.1057, -0.1344, -0.09613, -0.1535, -0.1362,
-0.1477, -0.1196, -0.13, -0.1721, -0.1419, -0.1344, -0.08684,
-0.1137, -0.1054, -0.179, -0.1314, -0.122, -0.14, -0.1453, -0.1063,
-0.1382, -0.143, -0.1278, -0.1114, -0.1008, -0.1237, -0.08701,
-0.08896, -0.1261, -0.1674, -0.1116, -0.1192, -0.156, -0.1738,
-0.1137, -0.1405, -0.1663, -0.1393, -0.1259, -0.07659, -0.1176,
-0.1325, -0.1432, -0.1373, -0.1153, -0.1173, -0.1683, -0.1485,
-0.1222)
b <- c(0.02765, 0.0003655, 0.01315, 0.03996, 0.009496, 0.0006978,
0.01546, 0.006651, 0.03626, -0.02307, 0.01906, 0.006012, -0.03311,
0.03919, 0.001477, 0.005686, -0.01026, -0.02559, -0.01881, -0.02306,
-0.00751, -0.002696, 0.008015, -0.01801, -0.04651, 0.001755,
-0.02369, 0.03002, 0.01155, 0.04294, 0.01012, 0.05339, -0.007262,
0.0272, 0.02658, -0.04211, -0.01421, 0.008791, -0.0005405, 0.02552,
0.004705, 0.03458, 0.02617, 0.007282, -0.007129, 0.004159, 0.01888,
0.01341, -0.02492, 0.01837, 0.024, 0.02048, 0.00438, -0.006591,
0.02295, 0.008665, 0.02429, 0.006213, -0.04526, -0.01066, -0.003409,
-0.007527, 0.008865, 0.03149, 0.03217, -0.004714, 0.009994, -0.009908,
-0.01366, -0.0108, -0.003148, 0.006765, -0.04191, 0.04184, 0.01474,
-0.0099, 0.001694, 0.00889, 0.01091, 0.001035, -0.01351, 0.00369,
-0.05145, 0.01338, 0.004623, -0.007436, -0.007046, 0.01927, 0.0005834,
0.01277, 0.02874, -0.01633, 0.006894, 0.02411, 0.0292, 0.05691,
0.02347, 0.02901, 0.02329, 0.00198)

并用函数绘制它们的图形:
library(ggplot2)
library(gridExtra)

proportion.distribution.fn <- function(a, b) {
# Generating data frames
a1 <- as.data.frame(a)
b1 <- as.data.frame(b)

# Generating graphs
a1g <- ggplot(a1, aes(x = a1[,1])) +
geom_density(fill = "skyblue1") +
labs(title = "a distribution", x = "Proportion", y = "Density")
b1g <- ggplot(b1, aes(x = b1[,1])) +
geom_density(fill = "skyblue1") +
labs(title = "b distribution", x = "Proportion", y = "Density")

return(grid.arrange(a1g, b1g))
}

proportion.distribution.fn(a, b)

Two probability distributions. Would like to shade anywhere from the right of x=0.05

最佳答案

我很惊讶地发现ggplot2(显然)没有简单的方法来做到这一点。这是一个使用ggplot_build提取所需x值(应在发生阴影的地方),然后使用geom_area手动绘制分布的解决方案:

proportion.distribution.fn <- function(a, b) {
# Posteriors (delta)
a1 <- as.data.frame(a)
b1 <- as.data.frame(b)

# Plotting delta
a1g <- ggplot(a1, aes(x = a1[,1])) +
geom_density() + # Note the lack of fill here
labs(title = "a distribution", x = "Proportion", y = "Density")

a1g_df <- ggplot_build(a1g)$data[[1]]

a1g <- a1g + geom_area(data = subset(a1g_df, x > 0.05),
aes(x=x,y=y),
fill = "skyblue1",
color = "black") # gives a nice border

b1g <- ggplot(b1, aes(x = b1[,1])) +
geom_density() + # Note the lack of fill here
labs(title = "b distribution", x = "Proportion", y = "Density")

b1g_df <- ggplot_build(b1g)$data[[1]]

b1g <- b1g + geom_area(data = subset(b1g_df, x > 0.05),
aes(x=x,y=y),
fill = "skyblue1",
color = "black") # gives a nice border

return(grid.arrange(a1g, b1g))
}

proportion.distribution.fn(a, b)

enter image description here

关于r - 如果没有y变量,则在x轴上的间隔上遮盖geom_density,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49807993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com