-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathExploratory Data Analysis (EDA) with R
132 lines (115 loc) · 3.52 KB
/
Exploratory Data Analysis (EDA) with 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
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
# Data Manipulation & Visualization
# 7 Package
library(dplyr)
library(ggplot2)
library(choroplethr)
library(choroplethrMaps)
library(openintro)
library(fiftystater)
library(colorplaner)
# Data
vehicle <- read.csv('https://raw.githubusercontent.com/bkrai/R-files-from-YouTube/main/vehicle.csv')
car <- as_data_frame(vehicle)
car
# Filter
car %>%
filter(State=='CA' | State == 'TX' | State=='FL')
car %>%
filter(State=='CA', ---)
# Arrange
car %>%
filter(State=='CA' | State == 'TX' | State=='FL') %>%
arrange(desc(Mileage))
# Summarise
car %>%
summarise(Avg_lc = mean(lc),
sd_lc = sd(lc),
max_lc = max(lc),
min_lc = ---,
sum_lc = ---,
median_lc = ---,
total = n())
# Group by
car %>%
group_by(State) %>%
summarise(Avg_lc = mean(lc),
sd_lc = sd(lc),
max_lc = max(lc),
min_lc = ---,
sum_lc = ---,
median_lc = ---,
total = n()) %>%
arrange(desc(Avg_lc))
# Mutate
car %>%
group_by(State) %>%
mutate(cph = sum(lc)/sum(lh)) %>%
summarise(Avg_cph = ---,
Avg_mileage = ---) %>%
arrange(desc(Avg_cph))
# Visualization
# Histogram
car %>%
filter(State=='CA' | State == 'TX' | State=='FL') %>%
ggplot(aes(x=lc, fill = ---)) +
geom_histogram(alpha=0.8, color='darkblue') +
ggtitle('Labor cost in Top 3 states') +
facet_wrap(~State)
# Density
car %>%
filter(State=='CA' | State == 'TX' | State=='FL') %>%
ggplot(aes(x=lc, fill = State)) +
---(alpha=0.5, color='darkblue') +
ggtitle('Labor cost in Top 3 states')
# Scatter
car %>%
filter(State=='CA' | State == 'TX' | State=='FL') %>%
ggplot(aes(x=lh, y = lc, col=State, size=mc)) +
geom_point(alpha=0.5, color='darkblue') +
geom_smooth(se=0) +
facet_wrap(---)
# bar plot
new <- car %>%
group_by(State) %>%
mutate(cph = ---/---) %>%
summarise(Avg_cph = mean(cph),
Avg_mileage = mean(Mileage)) %>%
arrange(desc(Avg_cph))
ggplot(new, aes(x=State, y = Avg_cph, fill = State)) +
geom_col() +
coord_flip() +
ggtitle('Cost per hour in 50 states')
# Box plot
car %>%
group_by(State) %>%
filter(n() >40) %>%
ggplot(aes(x=---, y=---, col = State)) +
geom_boxplot()
# Map-1
# Data preparation
new <- car %>%
group_by(State) %>%
summarise(total = n(),
Avg_mileage = mean(Mileage))
colnames(new) <- c('region', 'value', 'mileage')
new
new$region <- abbr2state(new$region)
new$region <- tolower(new$region)
new <- new[-1,]
state_choropleth(new,
title = "Car Failures in US",
legend = 'Number of Failures')
# Map-2
p <- ggplot(new, aes(map_id = ---)) +
geom_map(aes(fill=---), map = fifty_states) +
expand_limits(x=fifty_states$long, y=fifty_states$lat) +
coord_map() +
scale_x_continuous(breaks=NULL) +
scale_y_continuous(breaks=NULL) +
labs(x="", y="") +
theme(legend.position = "bottom",
panel.background = element_blank())
p + fifty_states_inset_boxes()
p + aes(fill2 = ---) + scale_fill_colourplane() +
theme(legend.position = 'right') +
ggtitle('Geo Map for Failures and Average Mileage')