简体   繁体   English

使用 ggplot2 的直方图上的密度

[英]Density over histogram using ggplot2

I have "long" format data frame which contains two columns: first col - values, second col- sex [Male - 1/Female - 2].我有“长”格式数据框,其中包含两列:第一列 - 值,第二列 - 性别 [男 - 1/女 - 2]。 I wrote some code to make a histogram of entire dataset (code below).我写了一些代码来制作整个数据集的直方图(下面的代码)。

ggplot(kz6, aes(x = values)) + 
  geom_histogram()

However, I want also add a density over histogram to emphasize the difference between sexes ie I want to combine 3 plots: histogram for entire dataset, and 2 density plots for each sex.但是,我还想在直方图上添加一个密度以强调性别之间的差异,即我想组合 3 个图:整个数据集的直方图,以及每个性别的 2 个密度图。 I tried to use some examples ( one , two , three , four ), but it still does not work.我尝试使用一些示例( ),但它仍然不起作用。 Code for density only works, while the combinations of hist + density does not.密度代码仅有效,而 hist + 密度的组合无效。

density <- ggplot(kz6, aes(x = x, fill = factor(sex))) + 
  geom_density()

both <- ggplot(kz6, aes(x = values)) + 
  geom_histogram() +
  geom_density()

both_2 <- ggplot(kz6, aes(x = values)) + 
  geom_histogram() +
  geom_density(aes(x = kz6[kz6$sex == 1,]))

PS some examples contains y=..density.. what does it mean? PS 一些例子包含y=..density..是什么意思? How to interpret this?这个怎么解释?

To plot a histogram and superimpose two densities, defined by a categorical variable, use appropriate aesthetics in the call to geom_density , like group or colour .要绘制直方图并叠加由分类变量定义的两个密度,请在对geom_density的调用中使用适当的美学,例如groupcolour

ggplot(kz6, aes(x = values)) +
  geom_histogram(aes(y = ..density..), bins = 20) +
  geom_density(aes(group = sex, colour = sex), adjust = 2)

在此处输入图片说明

Data creation code.数据创建代码。

I will create a test data set from built-in data set iris .我将从内置数据集iris创建一个测试数据集。

kz6 <- iris[iris$Species != "virginica", 4:5]
kz6$sex <- "M"
kz6$sex[kz6$Species == "versicolor"] <- "F"
kz6$Species <- NULL
names(kz6)[1] <- "values"
head(kz6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM