0

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.

missy morrow
  • 337
  • 3
  • 16
  • 1
    I wonder if one way might be to `scale` density so that it is more clear on the plot. `test = data.frame(y = rgamma(1000, shape = 0.25, rate = 0.0054)) ; fit = density(test$y) ; scale = 500/max(fit$y) ; plot_ly() %>% add_histogram(x = ~test$y, name = "Histogram") %>% add_lines(x = fit$x, y = scale*fit$y, name = "Density")` – d.b Dec 28 '16 at 22:22
  • @DarshanBaral this is interesting – missy morrow Dec 29 '16 at 03:35

2 Answers2

2

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))
royr2
  • 2,239
  • 15
  • 20
0

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

Peter Ellis
  • 5,694
  • 30
  • 46
  • I have tried ggplot but I cannot suppress the y axis values produced by the density plot and show the Frequency Count from histogram. – missy morrow Dec 29 '16 at 00:08