我在R中是很新的,我正在努力拼凑一个填充的直方图,该直方图被划分为6个类,KDE基于整个分布(而不是6个类的单个分布)。我有4列(data1
、data2
、data3
、origin
)的数据集,所有数据都是连续的,原点是我的分类(地理位置)。我可以用6个类来绘制data1的直方图,但是当我添加KDE曲线时,它也被划分为6个曲线(每个类一个)。我想我理解我必须重写第一个aes
参数,并在调用geom_density
时创建一个新的参数,但是我找不到如何这样做。
翻译我的问题与虹膜数据集,我想要KDE曲线为Sepal.Length
,而不是一个KDE曲线Sepal.Length
为每个物种。这是我的代码和虹膜数据的结果。
ggplot(data=iris, aes(x=Sepal.Length, fill=Species)) +
geom_histogram() +
theme_minimal() +
geom_density(kernel="gaussian", bw= 0.1, alpha=.3)
发布于 2022-04-22 13:09:41
我还找到了一个很好的教程,介绍如何将geom_hist()和geom_density()与sthda.com上的匹配标度结合起来
来自那里的Reprex是:
set.seed(1234)
df <- data.frame(
sex=factor(rep(c("F", "M"), each=200)),
weight=round(c(rnorm(200, mean=55, sd=5),
rnorm(200, mean=65, sd=5)))
)
library(ggplot2)
ggplot(df, aes(x=weight, color=sex, fill=sex)) +
geom_histogram(aes(y=..density..), alpha=0.5,position="identity") +
geom_density(alpha=.2)
https://stackoverflow.com/questions/68088769
复制