如何从密度图中的峰值将数据分成 2 个分区?

how can split data into 2 partition from the peak in the density graph?

我可以得到图表上的峰值并可以在上面画线 但现在我需要将我的数据分成两部分(峰值前)和(峰值后)

怎么做到的?

这是我用来获取峰值并在其上画线的代码:

library(data.table)
library(ggplot2)

ggplot(DS, aes(DS$loan_amount_ink)) + geom_density()


Ymax <- which.max(density( DS$loan_amount_ink)$y)  

Xmax <- density(DS$loan_amount_ink)$x[Ymax]       


ggplot(DS, aes(DS$loan_amount_ink)) + geom_density() + 
       geom_vline(xintercept = density( DS$loan_amount_ink)$x[Ymax])
library(ggplot2)
library(dplyr)
View(faithful)
ggplot(faithful, aes(waiting)) + geom_density()

which.max(density(faithful$waiting)$y) # Peak
peak <- density(faithful$waiting)$x[326]
peak # 79.96245
ggplot(faithful, aes(waiting)) + 
  geom_density() +
  geom_vline(xintercept = density(faithful$waiting)$x[326])

根据峰密度划分数据集。

df1 <- faithful %>% 
  dplyr::filter(waiting <= peak)

df2 <- faithful %>% 
  dplyr::filter(waiting > peak)

来源 - 求峰值密度:http://ianmadd.github.io/pages/PeakDensityDistribution.html