density curve over plotly histogram

4.2k views Asked by At

I am using plotly to plot a histogram for a dataset I am working with

test  <- data.frame(y = rgamma(1000, shape = 0.25, rate = 0.0054))
plot_ly(x = ~test$y, type = "histogram", nbinsx = "23")

The plot as such is fine but I am unclear how to draw a smooth density curve flowing through the contours of the histogram.

Plotly reference manual suggests,

A histogram trace is initialized with plot_ly or add_trace:

plot_ly(df, type="histogram"[, ...])
add_trace(p, type="histogram"[, ...]) 

and there is a histnorm (enumerated: "" | "percent" | "probability" | "density" | "probability density" ) histonorm function which i assume will allow users to draw a density curve but I am not sure how to use this function.

Interested to find out how others have approached this problem. Any tips or suggestions are much appreciated.

2

There are 2 answers

1
Peter Ellis On

One option, easiest if you're more familiar with ggplot's API than plotly's (as I am) is to make the plot with ggplot2 first.

library(plotly)
library(ggplot2
test  <- data.frame(y = rgamma(1000, shape = 0.25, rate = 0.0054))

p <- ggplot(test, aes(x = y, y = ..density..)) +
   geom_histogram(fill = "steelblue", bins  = 23) +
   geom_density()

ggplotly(p)

enter image description here

1
royr2 On

While not ideal - here's one way to do it.

EDIT: updated for y-axis limits

library(plotly)

y <- rgamma(1000, shape = 0.25, rate = 0.0054)
dens <- data.frame(x = density(y)$x,
                   y = density(y)$y)

miny <- 0
maxy <- max(dens$y)

plot_ly() %>% 
  add_histogram(x = y) %>% 
  add_lines(data = dens, x = ~x, y = ~y, yaxis = "y2", 
            line = list(width = 3)) %>% 
  layout(yaxis2 = list(overlaying = "y", 
                       side = "right", 
                       range = c(miny, maxy),
                       showgrid = F, 
                       zeroline = F))