-
Notifications
You must be signed in to change notification settings - Fork 0
/
Overlaying histograms.R
43 lines (37 loc) · 1.7 KB
/
Overlaying histograms.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#----------------------
# Overlaying histograms
#----------------------
library(tidyverse)
library(gridExtra)
# 1. Create dataset
x1 <- rnorm(n = 100, mean = 5, sd = 1)
x2 <- runif(n = 100, min = 0, max = 10) + rgamma(n = 100, shape = 10, rate = 1)
category <- sample(c('yes', 'no'), size = 100, replace = TRUE, prob = c(0.2, 0.8))
dataset <- data.frame(x1, x2, category)
# 1. Create first plot
p1 <- ggplot(dataset, aes(x = x1, fill = category)) + # Draw overlaying histogram
geom_histogram(position = "identity", alpha = 0.3, bins = 20) +
labs(title = 'Overlaying histogram 2',
subtitle = 'artificial dataset',
y="count of customers", x="churn") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=10, face="italic", color="darkred"),
panel.background = element_rect(fill = "white", colour = "grey50"),
panel.grid.major = element_line(colour = "grey90"))
# 2. Create second plot
p2 <- ggplot(dataset, aes(x = x2, fill = category)) + # Draw overlaying histogram
geom_histogram(position = "identity", alpha = 0.3, bins = 20) +
labs(title = 'Overlaying histogram 1',
subtitle = 'artificial dataset',
y="count of customers", x="churn") +
theme(axis.text=element_text(size=8),
axis.title=element_text(size=8),
plot.subtitle=element_text(size=10, face="italic", color="darkred"),
panel.background = element_rect(fill = "white", colour = "grey50"),
panel.grid.major = element_line(colour = "grey90"))
# 3. Create final plot
final.plot <- grid.arrange(p1, p2, nrow = 1)
#----
# end
#----