How to generate histograms in R using ggpubr package

In [1]:
# -------------------------------------------------------
# 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)
Loading required package: ggplot2
Loading required package: magrittr
'/Users/nilimesh/Desktop/Data Science Recipes/Kickstarter-Examples-1000/KE-259'
ChurnStatusrevenue
Churn 53.79293
Churn 55.27743
Churn 56.08444
Churn 52.65430
Churn 55.42912
Churn 55.50606
Warning message:
“Using `bins = 30` by default. Pick better value with the argument `bins`.”
Warning message:
“Using `bins = 30` by default. Pick better value with the argument `bins`.”
Warning message:
“Using `bins = 30` by default. Pick better value with the argument `bins`.”