-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter 2 data visualisation.R
More file actions
133 lines (87 loc) · 3.53 KB
/
chapter 2 data visualisation.R
File metadata and controls
133 lines (87 loc) · 3.53 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
library(nycflights13)
library(ggplot2)
library(moderndive)
#grammer of graphics
# matching data variable to aesthetic (aes for eg x,y position, size, colour )
# of geometric objects (geoms for example point, line, histogram etc)
#faceting breaks plot into several plots
#position - adjustment for barplots
# 5 named grahics. scatterplots, linegraphs, histogram, boxplot, barplots
# 5NG1. scatterplots
View(flights)
View(alaska_flights)
ggplot(data = alaska_flights) +
geom_point(aes(x = dep_delay, y = arr_delay))
#Overplotting
#adjusting transperency of points by adding alpha = 0.2
ggplot(data = alaska_flights) +
geom_point(aes(x = dep_delay, y = arr_delay), alpha = 0.2)
#Jittering
#use geom_jitter() instead of geom_point()
ggplot(data = alaska_flights) +
geom_jitter(aes(x = dep_delay, y = arr_delay), width = 30, height =30)
#using jitter and alpha both
ggplot(data = alaska_flights) +
geom_jitter(aes(x = dep_delay, y = arr_delay), width = 30, height =30, alpha = 0.4)
# 5NG2 Linegraphs via geomline. Used when variable on x axis has an inherent ordering such as time.
weather
?weather
early_january_weather
glimpse(early_january_weather)
ggplot(data = early_january_weather)+
geom_line(aes(x = time_hour, y = temp))
# using different linetypes by using linetype = 0 to 6, size of line by size = ....
# and colour of line by colour = ......
ggplot(data = early_january_weather)+
geom_line(aes(x = time_hour, y = temp), linetype = 1, size = 1, colour = "blue")
# 5NG3. Histograms. via geom_histogram
# bins, observations falling into bins
ggplot(weather)+
geom_histogram(aes(x = temp))
#changing border colour to white by colour = "........"
#and inside colour by fill = "........"
ggplot(weather)+
geom_histogram(aes(x = temp), colour = "white", fill = "steelblue")
# specify how many bins we want to cut x axis in by "bins = .........."
#default bins are 30
ggplot(weather)+
geom_histogram(aes(x = temp), bins = 40, colour = "white", fill = "steelblue")
# Or specify width of the bins by "binwidth = ........."
ggplot(weather)+
geom_histogram(aes(x = temp), binwidth = 10, colour = "white", fill = "steelblue")
#facet wrapping
#splitting the histogram on the basis of month by adding facet_wrap(~month)
ggplot(weather)+
geom_histogram(aes(x = temp), binwidth = 10, colour = "white", fill = "steelblue")+
facet_wrap(~month)
# specify number of row in facet wrap by facet_wrap(~month, nrow = .....)
#or number of coloumns by ncol=......
ggplot(weather)+
geom_histogram(aes(x = temp), binwidth = 10, colour = "white", fill = "steelblue")+
facet_wrap(~month, nrow = 4)
ggplot(weather)+
geom_histogram(aes(x = temp), binwidth = 10, colour = "white", fill = "steelblue")+
facet_wrap(~month, ncol = 4)
# 5NG4 Boxplots. via geom_boxplot
ggplot(weather)+
geom_boxplot(aes(x = factor(month), y = temp)).
#numerical variable month converted to categorical variable by using factor()
#5NG5 barplots via geom_bar or geom_col
# geom_bar() when not pre counted and geom_col() when pre counted
ggplot(flights)+
geom_bar(aes(x = carrier))
# Two categorical variables
View(flights)
ggplot(flights)+
geom_bar(aes(x = carrier, fill = origin)). # this is called stacked barplots
#dodged barplots
ggplot(flights)+
geom_bar(aes(x = carrier, fill = origin), position = "dodge")
# making width of every bar equal
ggplot(flights)+
geom_bar(aes(x = carrier, fill = origin),
position = position_dodge(preserve = "single"))
#faceting geom bar
ggplot(flights)+
geom_bar(aes(x = carrier))+
facet_wrap(~origin, ncol = 1)