Skip to content

Instantly share code, notes, and snippets.

@aravindhebbali
Last active September 25, 2017 10:09
Show Gist options
  • Save aravindhebbali/674bc30cc1539d735bdc4e6210982d1d to your computer and use it in GitHub Desktop.
Save aravindhebbali/674bc30cc1539d735bdc4e6210982d1d to your computer and use it in GitHub Desktop.
ggplot2: Histogram
# install
install.packages('ggplot2')
install.packages('readr')
# library
library(ggplot2)
library(readr)
# import data
ecom <- readr::read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/web.csv')
ecom
# Histogram
ggplot(ecom) +
geom_histogram(aes(n_visit))
# Specify Bins
ggplot(ecom) +
geom_histogram(aes(n_visit), bins = 7)
# Fill
ggplot(ecom) +
geom_histogram(aes(n_visit), bins = 7, fill = 'blue')
# Alpha
ggplot(ecom) +
geom_histogram(aes(n_visit), bins = 7, fill = 'blue', alpha = 0.3)
# Color
ggplot(ecom) +
geom_histogram(aes(n_visit), bins = 7, fill = 'white', color = 'blue')
# Bins, Color & Fill
ggplot(ecom) +
geom_histogram(aes(n_visit), bins = 7, fill = 'blue', color = 'white')
# Bin Width
ggplot(ecom) +
geom_histogram(aes(n_visit), binwidth = 2, fill = 'blue', color = 'black')
# Line Type
ggplot(ecom) +
geom_histogram(aes(n_visit), bins = 5, fill = 'white',
color = 'blue', linetype = 3)
# Line Size
ggplot(ecom) +
geom_histogram(aes(n_visit), bins = 5, fill = 'white',
color = 'blue', size = 1.25)
# Map Fill to Variable
ggplot(ecom) +
geom_histogram(aes(n_visit, fill = device), bins = 7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment