-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcustomer_descriptive_plots.R
More file actions
148 lines (119 loc) · 5.17 KB
/
customer_descriptive_plots.R
File metadata and controls
148 lines (119 loc) · 5.17 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
library(dplyr)
library(ggplot2)
library(ggpubr)
library(ggstats)
working_directory
ggtheme_descriptive_plot()
## Plots
### Simple plots
customer_descriptive_plot <- single_plot(df = df_loyalty_analysis %>%
dplyr::select(customer_id, gender, mean_agegroup, county_name) %>%
distinct(customer_id, .keep_all = TRUE),
variable = c("gender", "mean_agegroup", "county_name"),
rotate_axis = FALSE,
percent_yaxis=FALSE,
text_label_size = 2.5,
x_axis_label_wrap_width = 15
)
print(customer_descriptive_plot)
### stacked plots
customer_stack_plot <- sapply(c("gender", "mean_agegroup","county_name"), function(x) {
df_new <- df_loyalty_analysis %>%
dplyr::select(customer_id, gender, mean_agegroup, county_name) %>%
distinct(customer_id, .keep_all = TRUE)
out <- if (x == "mean_agegroup" | x == "county_name") {
stacked_plot(df = df_new,
variable = c("gender", "mean_agegroup", "county_name")[!(c("gender", "mean_agegroup", "county_name") %in% x)],
fill_vars = x,
title_label = NULL,
nrow_legend = 5
)
} else {
stacked_plot(df = df_new,
variable = c("gender", "mean_agegroup", "county_name")[!(c("gender", "mean_agegroup", "county_name") %in% x)],
fill_vars = x,
title_label = NULL,
nrow_legend = 2
)
}
out
}, simplify = FALSE
)
print(customer_stack_plot)
### stacked plots - panels
customer_stack_year_plot <- sapply(c("gender", "mean_agegroup","county_name"), function(x) {
df_new <- df_loyalty_analysis %>%
dplyr::select(customer_id, gender, mean_agegroup, county_name, year) %>%
distinct(customer_id, year, .keep_all = TRUE)
out <- if (x == "mean_agegroup"| x == "county_name") {
stacked_plot(df = df_new,
variable = c("gender", "mean_agegroup","county_name")[!(c("gender", "mean_agegroup","county_name") %in% x)],
fill_vars = x,
facet_vars = c("year"),
facet_wrap=TRUE,
nrow_legend = 5
)
} else {
stacked_plot(df = df_new,
variable = c("gender", "mean_agegroup","county_name")[!(c("gender", "mean_agegroup","county_name") %in% x)],
fill_vars = x,
facet_vars = c("year"),
facet_wrap=TRUE,
nrow_legend = 2
)
}
out
}, simplify = FALSE
)
print(customer_stack_year_plot)
## Combined plots
### simple plots
customer_descriptive_plot_grid <- ggpubr::ggarrange(plotlist = customer_descriptive_plot,
ncol = length(customer_descriptive_plot),
nrow = NULL,
legend = "bottom",
common.legend = FALSE)
customer_descriptive_plot_grid
### stacked plots
customer_stack_plot_grid <- sapply(names(customer_stack_plot), function(x) {
ggpubr::ggarrange(plotlist = customer_stack_plot[[x]],
ncol = length(customer_stack_plot[[x]]),
nrow = NULL,
legend = "right",
common.legend = TRUE)
}, simplify = FALSE
)
customer_stack_plot_all_grid <- ggpubr::ggarrange(plotlist = customer_stack_plot_grid,
nrow = length(customer_stack_plot_grid),
ncol = NULL,
legend = NULL,
common.legend = FALSE)
customer_stack_plot_all_grid
### stacked plots - panels
customer_stack_year_plot_grid <- sapply(names(customer_stack_year_plot), function(x) {
ggpubr::ggarrange(plotlist = customer_stack_year_plot[[x]],
ncol = length(customer_stack_year_plot[[x]]),
nrow = NULL,
legend = "right",
common.legend = TRUE)
}, simplify = FALSE
)
customer_stack_year_plot_all_grid <- ggpubr::ggarrange(plotlist = customer_stack_year_plot_grid,
nrow = length(customer_stack_year_plot_grid),
ncol = NULL,
legend = NULL,
common.legend = FALSE)
customer_stack_year_plot_all_grid
## Saving the grid plots
### Simple plots
ggsave(plot=customer_descriptive_plot_grid, height = 6, width = 11.5,
filename = paste0("customer_descriptive_plot",".png"),
path = output_plots_Dir, bg='white')
### stacked plots
ggsave(plot=customer_stack_plot_all_grid, height = 7, width = 11.5,
filename = paste0("customer_stack_plot",".png"),
path = output_plots_Dir, bg='white')
### stacked plots - panels
ggsave(plot=customer_stack_year_plot_all_grid, height = 7, width = 14,
filename = paste0("customer_stack_year_plot",".png"),
path = output_plots_Dir, bg='white')