Histograms in R
How to make a histogram in R.
Plotly Studio: Transform any dataset into an interactive data application in minutes with AI. Sign up for early access now.
Basic Histogram
library(plotly)
fig <- plot_ly(x = ~rnorm(50), type = "histogram")
fig
Click to copy
Normalized Histogram
library(plotly)
fig <- plot_ly(x = ~rnorm(50),
type = "histogram",
histnorm = "probability")
fig
Click to copy
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
Click to copy
Horizontal Histogram
library(plotly)
fig <- plot_ly(y = ~rnorm(50), type = "histogram")
fig
Click to copy
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
Click to copy
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
Click to copy
Cumulative Histogram
library(plotly)
fig <- plot_ly(x = ~rnorm(50),
type = "histogram",
cumulative = list(enabled=TRUE))
fig
Click to copy
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
Click to copy
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
Click to copy
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)
Click to copy