Search code examples
rggplot2histogramdensity-plot

Overlay histogram with multiple density curves for generalized pareto distribution


I want to draw an overlay histogram with multiple density curves for generalized Pareto distribution. As you can see that the density curves are not clearly visible. Are they any way to make it clear? Thanks.

enter image description here

library(eva)
data(lowestoft)
data = as.vector(lowestoft)

d1 = dgpd(data, 0,  31.38105, 10.15003)
d2 = dgpd(data, 0, 2.9431553, 0.6778055)
d3 = dgpd(data,0, 5.413916, 17.162103)
d4 = dgpd(data,0, 43.18705, 13.98005)


N = length(data)
allden = c(d1, d2, d3, d4)
settings = c(rep('d1', N), rep('d2', N), 
             rep('d3', N), rep('d4', N))


mydata = data.frame(x= rep(data, 4), allden = allden, Methods = settings)

ggplot(mydata, aes(x)) +
  geom_histogram(aes(y = stat(density)), binwidth = 1, fill = "grey", color = "black")+
  geom_line(aes(x = x, y=allden, color = Methods)) 

Solution

  • One thing you could do would be to add a separate scaled y-axis for the geom_line like this

    library(eva)
    data(lowestoft)
    data = as.vector(lowestoft)
    
    d1 = dgpd(data, 0,  31.38105, 10.15003)
    d2 = dgpd(data, 0, 2.9431553, 0.6778055)
    d3 = dgpd(data,0, 5.413916, 17.162103)
    d4 = dgpd(data,0, 43.18705, 13.98005)
    
    
    N = length(data)
    allden = c(d1, d2, d3, d4)
    settings = c(rep('d1', N), rep('d2', N), 
                 rep('d3', N), rep('d4', N))
    
    
    mydata = data.frame(x= rep(data, 4), allden = allden, Methods = settings)
    
    coef <- 8
    ggplot(mydata, aes(x,allden)) +
      geom_histogram(aes(y = stat(density)), binwidth = 1, fill = "grey", color = "black") +
      geom_line(aes(x = x, y=allden*coef, color = Methods)) +
      scale_y_continuous(name="Histogram Axis",
                         sec.axis = sec_axis(trans~./coef, name='Line Axis'))