Histograms in R

How to make a histogram in R.


Plotly is known for its graphing libraries, but it's also a broader ecosystem for building interactive data apps, AI data analysis tools, and deploying and hosting Dash apps. Explore the full set of tools and platforms designed to help turn data into action. Discover Plotly.

Note: We are retiring documentation for R, MATLAB, Julia, and F#. Learn more about this change here.

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

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://plotly.com/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)