Python / R Check for density peaks in ggplot2

1

I have two sets of data formed as follows:

A = {id1: 0.3, id2: 0.1, id3: 0.3 ... idn: 0.2}

B = {id1: 0.01, id2: 0.04, id3: 0.75 ... idn: 0.9}

I used the ggplot function of R to plot the density values in the same graph, thus:

Iwonderwhattheid'sthatareonthepeaksofeachdensityare.Forexample,whataretheid'sthatareinthepeak(inred)ofeachdensity?

I would like to know if the id's are different or equal in the peaks, ie in the high density values.

    
asked by anonymous 26.05.2017 / 23:11

1 answer

3

You can get the data from the graph generated by ggplot2 by explicitly requesting print .

For example, let's generate a histogram:

rm(list = ls())
library(ggplot2)
set.seed(10)
df <- data.frame(x = rnorm(10000))
grafico <- ggplot(df, aes(x = x)) + geom_density()
grafico

Togetthedataofthegraphandthustoknowwhatthemaximumvalueaskstheprintexplicitly,savethedataandseethemaximumvalueofthedensity:

dados_grafico<-print(grafico)$data[[1]]dados_grafico[which.max(dados_grafico$density),c("x","density")]
             x   density
238 -0.1253751 0.3963933

In this case the maximum occurs at x = -0.1253751 with density of 0.3963933.

    
28.05.2017 / 09:18