Histograms and density plots

By Data Tricks, 12 June 2018

First run the following code to set-up the data and set some theme options.

library(ggplot2)
library(plotly)
set.seed(555)
data <- data.frame(
Value = round(rnorm(n=1000, mean=50, sd=10),0),
Category = sample(c(1:2),1000,replace=TRUE)
)
data$Category <- as.character(data$Category)
theme <- theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(color="#919191", size = 0.1),
axis.line.y = element_line(color="#919191", size = 0.1))

Basic histogram

#Basic histogram
gg <- ggplot(data, aes(x=Value))
gg <- gg + geom_histogram(binwidth = 1)
gg <- gg + theme
print(gg)

Density plot

#Density plot
gg <- ggplot(data, aes(x=Value, fill=Category))
gg <- gg + geom_density(alpha=0.3)
gg <- gg + theme
print(gg)

Density plots – filled or stacked

#Density plot filled
gg <- ggplot(data, aes(x=Value, fill=Category))
gg <- gg + geom_density(alpha=0.3, position="fill")
gg <- gg + theme
print(gg)

To create a stacked density plot, change position=”fill” to position=”stack”.

Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Please note that your first comment on this site will be moderated, after which you will be able to comment freely.