【发布时间】:2018-09-23 06:46:12
【问题描述】:
对于只有 x 变量的概率分布,我很难在 geom_density 曲线下着色。我想在 x > 0.05 的区域下对图进行着色。 R 上的其他线程只有在包含 y 变量时才能工作。
使用这些随机生成的分布值:
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)
【问题讨论】: