-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.R
More file actions
121 lines (92 loc) · 3.96 KB
/
Copy pathmain.R
File metadata and controls
121 lines (92 loc) · 3.96 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
#install the packages
install.packages("dplyr")
install.packages("ggplot2")
install.packages("corrplot")
install.packages("ggcorrplot")
#load the packages
library(dplyr)
library(ggplot2)
library(corrplot)
library(ggcorrplot)
#load the dataset
store <- read.csv("department-store.csv")
#view the dataset
View(store)
#glimpse the dataset
glimpse(store)
#add a column to show the profit
store <- mutate(store, PROFIT = SELLING_PRICE-COST_PRICE)
#add a column to show the profit percent
store <- mutate(store, PROFIT_PERCENT = (PROFIT/COST_PRICE)*100)
#add a column to show the net profit
store <- mutate(store, NET_PROFIT = PROFIT*QUANTITY_DEMANDED)
#view the data after mutate
View(store)
#sort the data grouped by PRODUCT_TYPE
store_group <- group_by(store, PRODUCT_TYPE)
#calculate the average/mean
summarise(store_group, average = mean(NET_PROFIT))
#calculate the summation
summarise(store_group, summation = sum(NET_PROFIT))
#calculate the minimum and maximum
summarise(store_group, minimum = min(NET_PROFIT), mAXIMUM = max(NET_PROFIT))
#calculate the median
summarise(store_group, median = median(NET_PROFIT))
#calculate the variance
summarise(store_group, variance = var(NET_PROFIT))
#calculate the standard deviation
summarise(store_group, standard_deviation = sd(NET_PROFIT))
#calculate the range
summarise(store_group, range = range(NET_PROFIT))
#plot for AVERAGE_QUANTITY & PRODUCT_TYPE
store %>% group_by(PRODUCT_TYPE) %>%
summarise(AVERAGE = mean(QUANTITY_DEMANDED)) %>%
ggplot(aes(x = PRODUCT_TYPE, y = AVERAGE))+
geom_col(width = 0.6, fill = "#4169E1")+
theme(text = element_text(size = 9))
#plot for AVERAGE_NET_PROFIT & PRODUCT_TYPE
store %>% group_by(PRODUCT_TYPE) %>%
summarise(AVERAGE_NET_PROFIT = mean(NET_PROFIT)) %>%
ggplot(aes(x = PRODUCT_TYPE, y = AVERAGE_NET_PROFIT))+
geom_col(width = 0.6, fill = "#4169E1")+
theme(text = element_text(size = 9))
#plot for NET_PROFIT & COMPANY
store %>%
ggplot(aes(x = COMPANY, y = NET_PROFIT, color = PRODUCT_CATEGORY)) + geom_point()
#plot for PROFIT & QUANTITY_DEMANDED WHERE PRODUCT_TYPE == "beauty products"
store %>% filter(PRODUCT_TYPE == "beauty products") %>%
ggplot(aes(x = QUANTITY_DEMANDED, y = PROFIT, color = PRODUCT_CATEGORY)) + geom_point()
#plot for PRICE-DEMAND RELATIONSHIP (AVERAGE_SELLING_PRICE VS QUANTITY_DEMANDED)
store %>%
ggplot(aes(x = QUANTITY_DEMANDED, y = SELLING_PRICE)) + geom_line(color = "#006400")
#plot for AVERAGE_NET_PROFIT & COMPANY
store %>% group_by(PRODUCT_TYPE, COMPANY) %>%
summarise(AVERAGE_NET_PROFIT = mean(NET_PROFIT, na.rm = TRUE)) %>%
ggplot(aes(x = PRODUCT_TYPE, y = AVERAGE_NET_PROFIT, group = COMPANY, color = COMPANY))+
geom_line()+ theme(text = element_text(size = 9.5))
#histogram for PROFIT_PERCENT of PRODUCT_CATEGORY
store %>%
ggplot(aes(x = PROFIT_PERCENT, fill = PRODUCT_CATEGORY))+
geom_histogram(binwidth = 30)
#histogram for QUANTITY_DEMANDED of PRODUCT_CATEGORY where PRODUCT_TYPE is "snacks"
store %>%
filter(PRODUCT_TYPE == "snacks") %>%
ggplot(aes(x = QUANTITY_DEMANDED, fill = PRODUCT_CATEGORY))+
geom_histogram(binwidth = 30)
##PIE CHART FOR EACH HYGIENE PRODUCT'S QUANTITY DEMANDED.
#STEP1: PREPARE REQUIRED DATA
store1 <- filter(store, PRODUCT_TYPE == "hygiene")%>%
group_by(PRODUCT_CATEGORY) %>%
summarise(QUANTITY_DEMANDED = sum(QUANTITY_DEMANDED))
#STEP2: CALCULATE PERCENTAGE OF EACH PRODUCT
store2 <- store1 %>%
arrange(desc(PRODUCT_CATEGORY)) %>%
mutate(percentage = round(QUANTITY_DEMANDED*100/sum(QUANTITY_DEMANDED))) %>%
mutate(y_pos = cumsum(percentage)-0.5*percentage)
# STEP3: CREATE THE PIE CHART
store2 %>%
ggplot(aes(x = "", percentage, fill = PRODUCT_CATEGORY))+
geom_bar(width = 1, stat = "identity", color = "white", alpha = .5)+
coord_polar("y", start = 0)+
geom_text(aes(y = y_pos, label = paste0(percentage, "%")), color = "black")+
scale_fill_manual(values = rainbow(7))+ theme_void()