# R Plotly overlay density & histogram

**URL:** <https://community.plotly.com/t/r-plotly-overlay-density-histogram/640>\
**Category:** Plotly R\
**Created:** [March 14, 2016, 12:20pm UTC](https://community.plotly.com/t/r-plotly-overlay-density-histogram/640 "2016-03-14T12:20:55Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nite](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nite/32/156_2.png) [@nite](https://community.plotly.com/u/nite)\
**Post date:** [March 14, 2016, 12:20pm UTC](https://community.plotly.com/t/r-plotly-overlay-density-histogram/640/1 "2016-03-14T12:20:55Z")

</div>

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](https://plot.ly/224/~riddhiman) (this is exactly what I need in the first instance).

---

<div class="post-metadata">

**Author:** ![royr2](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@royr2](https://community.plotly.com/u/royr2)\
**Post date:** [March 16, 2016, 6:28am UTC](https://community.plotly.com/t/r-plotly-overlay-density-histogram/640/2 "2016-03-16T06:28:42Z")

</div>

Hi, See [https://plot.ly/ggplot2/geom\_density/#density-and-histogram-overlay-using-geom\_density](https://plot.ly/ggplot2/geom_density/#density-and-histogram-overlay-using-geom_density). Its the R-Code that’ll get you that chart 👍

---

<div class="post-metadata">

**Author:** ![nite](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nite/32/156_2.png) [@nite](https://community.plotly.com/u/nite)\
**Post date:** [March 16, 2016, 12:31pm UTC](https://community.plotly.com/t/r-plotly-overlay-density-histogram/640/3 "2016-03-16T12:31:43Z")

</div>

Cheers - yes I’m replacing a ggplot, but would like to have a native plotly implementation (ie. using plot\_ly and add\_trace)

---

<div class="post-metadata">

**Author:** ![royr2](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@royr2](https://community.plotly.com/u/royr2)\
**Post date:** [March 16, 2016, 1:17pm UTC](https://community.plotly.com/t/r-plotly-overlay-density-histogram/640/4 "2016-03-16T13:17:03Z")

</div>

Sure - here’s an example:

```auto
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"))

```

![](https://us1.discourse-cdn.com/flex024/uploads/plot/original/1X/fb3714216733fc972478c4fff7ce373ccd40ac11.png)

---

<div class="post-metadata">

**Author:** ![nite](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/nite/32/156_2.png) [@nite](https://community.plotly.com/u/nite)\
**Post date:** [March 16, 2016, 1:22pm UTC](https://community.plotly.com/t/r-plotly-overlay-density-histogram/640/5 "2016-03-16T13:22:40Z")

</div>

Fantastic - thanks @royr2!

---

<div class="post-metadata">

**Author:** ![ehossain](https://avatars.discourse-cdn.com/v4/letter/e/ee7513/32.png) [@ehossain](https://community.plotly.com/u/ehossain)\
**Post date:** [November 18, 2016, 4:37pm UTC](https://community.plotly.com/t/r-plotly-overlay-density-histogram/640/6 "2016-11-18T16:37:56Z")

</div>

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"))

```

![](https://us1.discourse-cdn.com/flex024/uploads/plot/original/1X/4510e6b4f37794ccdde99067ad21f366d4925166.png)

---

<div class="post-metadata">

**Author:** ![ehossain](https://avatars.discourse-cdn.com/v4/letter/e/ee7513/32.png) [@ehossain](https://community.plotly.com/u/ehossain)\
**Post date:** [November 18, 2016, 4:47pm UTC](https://community.plotly.com/t/r-plotly-overlay-density-histogram/640/7 "2016-11-18T16:47:35Z")

</div>

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"))

```

![](https://us1.discourse-cdn.com/flex024/uploads/plot/original/1X/1b5219a28072eb49de486b6cfbab47f1d8eb2189.png)

---

<div class="post-metadata">

**Author:** ![royr2](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@royr2](https://community.plotly.com/u/royr2)\
**Post date:** [November 21, 2016, 12:10pm UTC](https://community.plotly.com/t/r-plotly-overlay-density-histogram/640/8 "2016-11-21T12:10:55Z")

</div>

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

```auto
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"))

```

---

<div class="post-metadata">

**Author:** ![nashank79](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@nashank79](https://community.plotly.com/u/nashank79)\
**Post date:** [October 11, 2018, 5:47pm UTC](https://community.plotly.com/t/r-plotly-overlay-density-histogram/640/10 "2018-10-11T17:47:20Z")

</div>

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

 ![newplot](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/c/c6439319aaa3210aae2f2b8e3f9c85e3c572dc17.png)

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

---

<div class="post-metadata">

**Author:** ![nashank79](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@nashank79](https://community.plotly.com/u/nashank79)\
**Post date:** [October 16, 2018, 11:20pm UTC](https://community.plotly.com/t/r-plotly-overlay-density-histogram/640/11 "2018-10-16T23:20:38Z")

</div>

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)

 ![newplot](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/0/0cd6d930a7f2afd925ce43989f467e6378357041.png)
