如何获得重叠密度曲线下的面积?
我该如何解决 R 的问题?(这里有python的解决方案:计算两个函数的重叠面积)
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)))
)
Run Code Online (Sandbox Code Playgroud)
(来源:http : //www.sthda.com/english/wiki/ggplot2-density-plot-quick-start-guide-r-software-and-data-visualization)
ggplot(df, aes(x=weight, color=sex, fill=sex)) +
geom_density(aes(y=..density..), alpha=0.5)
Run Code Online (Sandbox Code Playgroud)
“图中使用的点由 ggplot_build() 返回,因此您可以访问它们。” 所以现在,我有了积分,我可以将它们提供给 approxfun,但我的问题是我不知道如何减去密度函数。
非常感谢任何帮助!(而且我相信需求量很大,没有现成的解决方案。)
set.seed(357)
x <- data.frame(name = sample(letters, 10), val = runif(10), stringsAsFactors = F)
x[c(2,6),"name"] <- c("k","k")
ggplot(x, aes(x = name, y = val)) + theme_bw() + geom_bar(stat = "identity")
Run Code Online (Sandbox Code Playgroud)
How can I plot the axis in the same order as x$name? (Yes, the k is duplicate, I want that to show up in the plot like this axis: c k g f o k s v t q)
In the past I used to do:
x$name <- factor(x$name, levels = …Run Code Online (Sandbox Code Playgroud)