Show Sidebar Hide Sidebar

# Histograms in R

How to make a histogram in R.

### New to Plotly?

Plotly's R library is free and open source!
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

### Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

``````library(plotly)
packageVersion('plotly')
``````
``````## [1] '4.7.1.9000'
``````

#### Basic Histogram

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

# Set up API credentials: https://plot.ly/r/getting-started
``````

#### Normalized Histogram

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

# Set up API credentials: https://plot.ly/r/getting-started
``````

#### Specify Binning Function

``````library(plotly)

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

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

# Set up API credentials: https://plot.ly/r/getting-started
``````

#### Horizontal Histogram

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

# Set up API credentials: https://plot.ly/r/getting-started
``````

#### Overlaid Histograms

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

# Set up API credentials: https://plot.ly/r/getting-started
``````

#### Stacked Histograms

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

# Set up API credentials: https://plot.ly/r/getting-started
``````

#### Cumulative Histogram

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