R Plotly overlay density & histogram

Hi all - is it possible to overlay different types of plots in R, eg. density plots over histograms as in https://plot.ly/224/~riddhiman (this is exactly what I need in the first instance).

Hi, See https://plot.ly/ggplot2/geom_density/#density-and-histogram-overlay-using-geom_density. Its the R-Code that’ll get you that chart :thumbsup:

1 Like

Cheers - yes I’m replacing a ggplot, but would like to have a native plotly implementation (ie. using plot_ly and add_trace)

Sure - here’s an example:

library(plotly)

x <- rnorm(1000)
fit <- density(x)

plot_ly(x = x, type = "histogram", name = "Histogram") %>% 
  add_trace(x = fit$x, y = fit$y, mode = "lines", fill = "tozeroy", yaxis = "y2", name = "Density") %>% 
  layout(yaxis2 = list(overlaying = "y", side = "right"))

1 Like

Fantastic - thanks @royr2!

I don’t think this method works anymore after the plotly update. I ran the same code as above and got something different.

library(plotly)

x <- rnorm(1000)
fit <- density(x)

plot_ly(x = x, type = "histogram", name = "Histogram") %>% 
  add_trace(x = fit$x, y = fit$y, mode = "lines", fill = "tozeroy", yaxis = "y2", name = "Density") %>% 
  layout(yaxis2 = list(overlaying = "y", side = "right"))

I figured it out. You have to modify the code to specify “scatter”. See below:

library(plotly)

x <- rnorm(1000)
fit <- density(x)

plot_ly(x = x, type = "histogram", name = "Histogram") %>% 
  add_trace(x = fit$x, y = fit$y, type = "scatter", mode = "lines", fill = "tozeroy", yaxis = "y2", name = "Density") %>% 
  layout(yaxis2 = list(overlaying = "y", side = "right"))

Here’s a slightly simpler way of doing this (with plotly 4.0)

library(plotly)

x <- rnorm(1000)
fit <- density(x)

plot_ly(x = x) %>% 
  add_histogram() %>% 
  add_lines(x = fit$x, y = fit$y, fill = "tozeroy", yaxis = "y2") %>% 
  layout(yaxis2 = list(overlaying = "y", side = "right"))

1 Like

As an extension to this, I am trying to plot two graphs of this combination using subplot. Please see code below:

x <- rnorm(1000)
fit <- density(x)

ax <- list(
title = “”,
zeroline = TRUE,
showline = TRUE,
showticklabels = FALSE,
showgrid =FALSE,
autorange=TRUE,
overlaying=“y”
)

p1<-plot_ly(x = x) %>%
add_histogram() %>%
add_lines(x = fit$x, y = fit$y, fill = “tozeroy”, yaxis = “y2”) %>%
layout(yaxis2 = ax)

p2<-plot_ly(x = x) %>%
add_histogram() %>%
add_lines(x = fit$x, y = fit$y, fill = “tozeroy”, yaxis = “y2”) %>%
layout(yaxis2 = ax)

subplot(p1,p2)

The output of this is

Can somebody help me here? Where am I missing the “plot”?

I fixed this by fixing my axes right!

x <- rnorm(1000)
fit <- density(x)

ay <- list(
title = “”,
zeroline = TRUE,
showline = TRUE,
showticklabels = FALSE,
showgrid =FALSE,
autorange=TRUE,
overlaying=“y2”
)

ay1 <- list(
title = “”,
zeroline = TRUE,
showline = TRUE,
showticklabels = FALSE,
showgrid =FALSE,
autorange=TRUE,
overlaying=“y4”
)

p1<-plot_ly(x = x) %>%
add_histogram() %>%
add_lines(x = fit$x, y = fit$y, fill = “tozeroy”,mode = “markers”,marker = list(color = ‘gray’), type=“scatter”, yaxis = “y2”) %>%
layout(yaxis2 = ay)

p2<-plot_ly(x = x) %>%
add_histogram() %>%
add_lines(x = fit$x, y = fit$y, fill = “tozeroy”,mode = “markers”,marker = list(color = ‘gray’), type=“scatter”, yaxis = “y4”) %>%
layout(yaxis4 = ay1)

subplot(p1,p2)