Mat*_*ers 3 r ggplot2 probability-density
如何提取每个密度图的值矩阵?
例如,我感兴趣的是,当重量= 71时,橙子、苹果、梨、香蕉的密度是多少?
下面是最小的例子:
library(ggplot2)
set.seed(1234)
df = data.frame(
fruits = factor(rep(c("Orange", "Apple", "Pears", "Banana"), each = 200)),
weight = round(c(rnorm(200, mean = 55, sd=5),
rnorm(200, mean=65, sd=5),
rnorm(200, mean=70, sd=5),
rnorm(200, mean=75, sd=5)))
)
dim(df) [1] 800 2
ggplot(df, aes(x = weight)) +
geom_density() +
facet_grid(fruits ~ ., scales = "free", space = "free")
Run Code Online (Sandbox Code Playgroud)
将绘图保存在变量中,构建数据结构并按ggplot_build面板分割数据。然后用 进行插值approx以获得新值。
g <- ggplot(df, aes(x = weight)) +
geom_density() +
facet_grid(fruits ~ ., scales = "free", space = "free")
p <- ggplot_build(g)
# These are the columns of interest
p$data[[1]]$x
p$data[[1]]$density
p$data[[1]]$PANEL
Run Code Online (Sandbox Code Playgroud)
按面板拆分列表成员p$data[[1]],但仅保留x和density值。然后循环遍历分割数据以按水果组进行插值。
sp <- split(p$data[[1]][c("x", "density")], p$data[[1]]$PANEL)
new_weight <- 71
sapply(sp, \(DF){
with(DF, approx(x, density, xout = new_weight))
})
# 1 2 3 4
#x 71 71 71 71
#y 0.04066888 0.05716947 0.001319164 0.07467761
Run Code Online (Sandbox Code Playgroud)
或者,在不预先拆分数据的情况下,使用by.
b <- by(p$data[[1]][c("x", "density")], p$data[[1]]$PANEL, \(DF){
with(DF, approx(x, density, xout = new_weight))
})
do.call(rbind, lapply(b, as.data.frame))
# x y
#1 71 0.040668880
#2 71 0.057169474
#3 71 0.001319164
#4 71 0.074677607
Run Code Online (Sandbox Code Playgroud)