Grace_G
Grace_G

Reputation: 53

R language, how can I extract the corresponding x value of the second highest peak seen from the density curve?

After I used a numeric vector to draw a density plot, I need to know the peak's position on x-axis.

plot(density(my_vector)  #From this plot, there are two peaks, right one is more higher
y_peak <- which.max(density(my_vector)$y)  #find the maximum y value
x_peak <- density(my_vector)$x[y_peak]  #find corresponding x value  

But how can I find the lower peak's x value? Any idea will be grateful.

Upvotes: 1

Views: 639

Answers (1)

missuse
missuse

Reputation: 19716

You wish to find the points larger than surrounding points or local maximums. One way to check if a point is a local maximum is to see if it is higher than the points around it

Here is an example of a density with 2 maximums

data:

set.seed(10)
b = rnorm(100)
v = rnorm(100)+2
b = c(b, v)

plot(density(b))

enter image description here

library(dplyr) #for lag and lead functions can also do it manually if you wish
dens = density(b)$y
z = data.frame(dens = dens, lag = lag(dens), lead = lead(dens))
which(with(z, dens>lag&dens>lead))
[1] 244 348

Here are the maxima:

density(b)$x[which(with(z, dens>lag&dens>lead))]
[1] 0.7244697 2.5298402

plot(density(b))
abline(v = density(b)$x[which(with(z, dens>lag&dens>lead))])

enter image description here

EDIT: dplyr lag and lead explained:

dplyr::lag(b)

basically changes the indexes of the vector + 1:

c(NA, b)[-length(c(NA, b))]

all(c(NA, b)[-length(c(NA, b))] == dplyr::lag(b),  na.rm = T)
[1] TRUE

dplyr::lead(b)

basically changes the indexes of the vector - 1:

all(c(b[-1], NA) == dplyr::lead(b),  na.rm = T)
[1] TRUE

Upvotes: 3

Related Questions