-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathggplot2.Rmd
More file actions
196 lines (146 loc) · 5.41 KB
/
ggplot2.Rmd
File metadata and controls
196 lines (146 loc) · 5.41 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
---
title: "GGplot2 Tutorial"
output:
md_document:
variant: gfm
---
#----------------------------------------------------------
# ggplot - Introduction to Graphical Visualization
#----------------------------------------------------------
#
# 1. Basic Structure and syntax in ggplot2
# 2. Exploration in data structure (dimentsion and variables properties)
# - Continous vs Discrete ?
# 3. Aesthetics definition
# 4. Primitive Plots (Horiontal line vs Vertical line)
# 5. One Varible plot (geom_histogram,geom_bar)
# 5.1 Two varible plot (text, point, jitter, box etc)
# 6. Cases study on Covid19 Data
#
```{r}
library(ggplot2)
data <- mtcars
#Henderson and Velleman (1981), Building multiple regression models interactively. Biometrics, 37, 391â411.
dim(data)
#32 11
str(data)
#[, 1] mpg Miles/(US) gallon
#[, 2] cyl Number of cylinders
#[, 3] disp Displacement (cu.in.)
#[, 4] hp Gross horsepower
#[, 5] drat Rear axle ratio
#[, 6] wt Weight (1000 lbs)
#[, 7] qsec 1/4 mile time
#[, 8] vs Engine (0 = V-shaped, 1 = straight)
#[, 9] am Transmission (0 = automatic, 1 = manual)
#[,10] gear Number of forward gears
#[,11] carb Number of carburetors
#How ggplot works
# 1. Define data
# 2. Define aesthetic aes()
# 3. Define variables, x= ; y =
```
#Example
```{r}
a <- ggplot(data, aes(x=gear,y=mpg))
a <- a + geom_blank() + ylim(0,35) + xlim(0,5)
a <- a + geom_hline(yintercept = 25)
a <- a + geom_vline(xintercept = 4)
a <- a + geom_abline(intercept=10,slope=5)
a
```
```{r}
#----------------------------------------------------------
# One Variable
#----------------------------------------------------------
mpg <- mpg
dim(mpg)
#234 11
str(mpg)
#http://archive.ics.uci.edu/ml/datasets/Auto+MPG
#tibble [234 x 11] (S3: tbl_df/tbl/data.frame)
#$ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ...
#$ model : chr [1:234] "a4" "a4" "a4" "a4" ...
#$ displ : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
#$ year : int [1:234] 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
#$ cyl : int [1:234] 4 4 4 4 6 6 6 4 4 4 ...
#$ trans : chr [1:234] "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
#$ drv : chr [1:234] "f" "f" "f" "f" ...
#$ cty : int [1:234] 18 21 20 21 16 18 18 18 16 20 ...
#$ hwy : int [1:234] 29 29 31 30 26 26 27 26 25 28 ...
#$ fl : chr [1:234] "p" "p" "p" "p" ...
#$ class : chr [1:234] "compact" "compact" "compact" "compact" ...
#One Discreate Parameters
b <- ggplot(mpg, aes(fl))
b <- b + geom_bar()
b
#One Continous Parameters
c <- ggplot(mpg, aes(cyl))
c <- c + geom_histogram()
c
d <- ggplot(mpg, aes(cty))
d <- d + geom_histogram()
d
```
```{r}
#----------------------------------------------------------
# Two Variables
#----------------------------------------------------------
#continuous x , continuous y
e <- ggplot(mpg, aes(cty, hwy))
e + geom_point()
e + geom_jitter()
e + geom_label(aes(label = cty))
e + geom_label(aes(label = hwy))
e + geom_text(aes(label = cty))
#discrete x , continuous y
f <- ggplot(mpg, aes(class, hwy))
f + geom_boxplot()
f + geom_violin(scale = "area")
#discrete x , discrete y
g <- ggplot(mpg, aes(class, drv))
g + geom_count()
```
```{r}
#----------------------------------------------------------
# Two Variables (x and y), more variebles as gropuing
#----------------------------------------------------------
e <- ggplot(mpg, aes(cty, hwy))
e + geom_text(aes(label = cty))
e + geom_text(aes(label = cty,colour = factor(manufacturer)))
e + geom_point(aes(colour = factor(cyl)))
e + geom_point(aes(colour = factor(manufacturer)))
e + geom_point(aes(colour = factor(manufacturer),shape = factor(cyl)))
e + geom_jitter(aes(colour = factor(manufacturer), shape = factor(cyl), size = displ))
```
#----------------------------------------------------------
# Practical Data Plotting - Covid 19
#----------------------------------------------------------
#https://www.ecdc.europa.eu/en/publications-data/download-todays-data-geographic-distribution-covid-19-cases-worldwide
```{r}
library(utils)
COVID_19 <- read.csv("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv", na.strings = "", fileEncoding = "UTF-8-BOM")
#1. Comparison between different countries, using a line plot (x = time, y=number of cases in log10).
#2. Total new cases in the world in a stack column plot, with each country as its own color (x = MOnth, y = number of cases in log10)
dim(COVID_19)
#11768 10
str(COVID_19)
library(dplyr)
Totalcases_in_All_Countries <- COVID_19 %>% group_by(countriesAndTerritories) %>% summarise(Cases = sum(cases_weekly))
```
#1. Comparison between different countries, using a line plot (x = time, y=number of cases in log10).
```{r}
cv <- ggplot(COVID_19, aes(x = dateRep, y= cases_weekly))
cv + geom_jitter()
cv + geom_jitter() + scale_y_log10()
```
#2. Total cases in the world in a stack column plot, with each country as its own color (x = MOnth, y = number of cases in log10)
```{r}
cv + geom_col(aes(color=countryterritoryCode))
cv + geom_col(aes(color=countryterritoryCode)) + theme(legend.position = "none")
cv + geom_col(aes(color=countryterritoryCode)) + scale_y_log10() + theme(legend.position = "none")
COVID_19s <- COVID_19 %>% group_by(countryterritoryCode) %>% filter(sum(cases_weekly)>300000)
cv2 <- ggplot(COVID_19s, aes(x = dateRep, y= cases_weekly))
cv2 + geom_col(aes(fill=countryterritoryCode))
cv2 + geom_col(aes(fill=countryterritoryCode)) + scale_y_log10()
```