[英]ggplot mixture model R
我有一个包含数值和分类变量的数据集。 每个类别的数值变量的分布都不同。 我想为每个分类变量绘制“密度图”,以便它们在视觉上低于整个密度图。
这类似于没有计算混合模型的混合模型的组件(因为我已经知道分割数据的分类变量)。
如果我根据分类变量将 ggplot 分组,则四个密度中的每一个都是真实密度并集成为一个。
library(ggplot2)
ggplot(iris, aes(x = Sepal.Width)) + geom_density() + geom_density(aes(x = Sepal.Width, group = Species, colour = 'Species'))
我想要的是将每个类别的密度作为子密度(不整合为 1)。 类似于下面的代码(我只为三种鸢尾中的两种实现了)
myIris <- as.data.table(iris)
# calculate density for entire dataset
dens_entire <- density(myIris[, Sepal.Width], cut = 0)
dens_e <- data.table(x = dens_entire[[1]], y = dens_entire[[2]])
# calculate density for dataset with setosa
dens_setosa <- density(myIris[Species == 'setosa', Sepal.Width], cut = 0)
dens_sa <- data.table(x = dens_setosa[[1]], y = dens_setosa[[2]])
# calculate density for dataset with versicolor
dens_versicolor <- density(myIris[Species == 'versicolor', Sepal.Width], cut = 0)
dens_v <- data.table(x = dens_versicolor[[1]], y = dens_versicolor[[2]])
# plot densities as mixture model
ggplot(dens_e, aes(x=x, y=y)) + geom_line() + geom_line(data = dens_sa, aes(x = x, y = y/2.5, colour = 'setosa')) +
geom_line(data = dens_v, aes(x = x, y = y/1.65, colour = 'versicolor'))
导致
上面我对数字进行了硬编码以减少 y 值。 有没有办法用 ggplot 做到这一点? 还是去计算?
谢谢你的想法。
你的意思是这样的吗? 不过,您需要更改比例。
ggplot(iris, aes(x = Sepal.Width)) +
geom_density(aes(y = ..count..)) +
geom_density(aes(x = Sepal.Width, y = ..count..,
group = Species, colour = Species))
另一种选择可能是
ggplot(iris, aes(x = Sepal.Width)) +
geom_density(aes(y = ..density..)) +
geom_density(aes(x = Sepal.Width, y = ..density../3,
group = Species, colour = Species))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.