# -------------------------------------------------------
# How to generate histograms in R using ggpubr package
# -------------------------------------------------------
# load libraries
library(datasets)
library(gridExtra)
library(ggpubr)
# Get current working directory
getwd()
# create simulated data
set.seed(1234)
wdata = data.frame(ChurnStatus = factor(rep(c("Churn", "Not_Churn"), each=500)),
revenue = c(rnorm(500, 55), rnorm(500, 58)))
head(wdata)
# Density plot with mean lines and marginal rug
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change outline and fill colors by groups
# Use custom palette
ggdensity(wdata, x = "revenue", add = "mean", rug = TRUE, color = "ChurnStatus",
fill = "ChurnStatus", palette = c("#00AFBB", "#E7B800"))
# Histogram plot with mean lines and marginal rug
# :::::::::::::::::::::::::::::::::::::::::::::::::::
# Change outline and fill colors by groups
# Use custom color palette
gghistogram(wdata, x = "revenue", add = "mean", rug = TRUE, color = "ChurnStatus",
fill = "ChurnStatus", palette = c("#00AFBB", "#E7B800"))
# Using IRIS dataset
data("iris")
df <- iris
# Sepal.Length: Density plot & Histogram with mean lines and marginal rug
g1 <- ggdensity(df, x = "Sepal.Length", add = "mean", rug = TRUE, color = "Species",
fill = "Species", palette = c("#00AFBB", "#E7B800" , "#B3F200"))
g2 <- gghistogram(df, x = "Sepal.Length", add = "mean", rug = TRUE, color = "Species",
fill = "Species", palette = c("#00AFBB", "#E7B800" , "#B3F200"))
grid.arrange(g1, g2, nrow = 1)
# Petal.Width: Density plot & Histogram with mean lines and marginal rug
g1 <- ggdensity(df, x = "Petal.Width", add = "mean", rug = TRUE, color = "Species",
fill = "Species", palette = c("#00AFBB", "#E7B800" , "#B3F200"))
g2 <- gghistogram(df, x = "Petal.Width", add = "mean", rug = TRUE, color = "Species",
fill = "Species", palette = c("#00AFBB", "#E7B800" , "#B3F200"))
grid.arrange(g1, g2, nrow = 1)