🏥 🏭 Working on the COVID-19 response in Canada? Plotly & the Canadian government can help you and your organization. Learn more and get in touch.

Histograms in R

How to make a histogram in R.


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Basic Histogram

library(plotly)
fig <- plot_ly(x = ~rnorm(50), type = "histogram")

fig

Normalized Histogram

library(plotly)
fig <- plot_ly(x = ~rnorm(50),
             type = "histogram",
             histnorm = "probability")

fig

Specify Binning Function

library(plotly)

x = c("Apples","Apples","Apples","Organges", "Bananas")
y = c("5","10","3","10","5")

fig <- plot_ly(y=y, x=x, histfunc='sum', type = "histogram")
fig <- fig %>% layout(yaxis=list(type='linear'))

fig

Horizontal Histogram

library(plotly)
fig <- plot_ly(y = ~rnorm(50), type = "histogram")

fig

Overlaid Histograms

fig <- plot_ly(alpha = 0.6)
fig <- fig %>% add_histogram(x = ~rnorm(500))
fig <- fig %>% add_histogram(x = ~rnorm(500) + 1)
fig <- fig %>% layout(barmode = "overlay")

fig

Stacked Histograms

fig <- plot_ly(alpha = 0.6)
fig <- fig %>% add_histogram(x = ~rnorm(500))
fig <- fig %>% add_histogram(x = ~rnorm(500) + 1)
fig <- fig %>% layout(barmode = "overlay")

fig

Cumulative Histogram

library(plotly)
fig <- plot_ly(x = ~rnorm(50),
             type = "histogram",
             cumulative = list(enabled=TRUE))

fig

Share bins between histograms

In this example both histograms have a compatible bin settings using bingroup attribute.

library(plotly)

fig <- plot_ly(
  type='histogram',
  x=~rnorm(100, 5),
  bingroup=1)

fig <- fig %>% add_trace(
  type='histogram',
  x=~rnorm(20, 5),
  bingroup=1)

fig <- fig %>% layout(
  barmode="overlay",
  bargap=0.1)

fig

Note that traces on the same subplot, and with the same barmode ("stack", "relative", "group") are forced into the same bingroup, however traces with barmode = "overlay" and on different axes (of the same axis type) can have compatible bin settings. Histogram and histogram2d trace can share the same bingroup.

library(plotly)

fig <- plot_ly(
  type='histogram',
  x=~rnorm(100, 5))

fig <- fig %>% add_trace(
  type='histogram',
  x=~rnorm(20, 5))

fig <- fig %>% layout(
  barmode="stack",
  bargap=0.1)

fig

Reference

See https://plot.ly/r/reference/#histogram for more information and chart attribute options!

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)