-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek_3.Rmd
More file actions
164 lines (128 loc) · 4.76 KB
/
Copy pathweek_3.Rmd
File metadata and controls
164 lines (128 loc) · 4.76 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
---
title: "Week 3 (Data Visualisation Course)"
author: "Kane"
date: "11/11/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Let's create a bar chart and then improve it based on the Data Visualisation Checklist (http://stephanieevergreen.com/wp-content/uploads/2016/10/DataVizChecklist_May2016.pdf)
```{r}
library(ggplot2)
library(dplyr)
#youtube <- read.csv('data/Youtube.csv')
youtube <- read.csv('https://raw.githubusercontent.com/KaneSec/ggplot2/main/data/Youtube.csv')
head(youtube)
# A basic bar chart
ggplot(youtube, aes(x =category)) +
geom_bar()
# Readable?
ggplot(youtube, aes(x =category)) +
geom_bar() +
theme(axis.text.x = element_text(angle = -90, hjust=0))
# 6-12 word descriptive title is left-justified in upper left corner
ggplot(youtube, aes(x =category)) +
geom_bar() +
theme(axis.text.x = element_text(angle = -90, hjust=0)) +
labs(title = "Count of the Most Popular Youtube Videos by Category",
x = "Category",
y = "count")
# Text is horizontal
ggplot(youtube, aes(x =category)) +
geom_bar() +
theme(axis.text.x = element_text(angle = -90, hjust=0)) +
labs(title = "Count of the Most Popular Youtube Videos by Category",
x = "Category",
y = "count") +
coord_flip()
# Text is horizontal (x axis)
ggplot(youtube, aes(x =category)) +
geom_bar() +
labs(title = "Number of Top Youtube authors in different categories",
x = "Category",
y = "count") +
coord_flip()
# Data are labeled directly
youtube_cat <- youtube %>% group_by(category) %>% summarize(count=n(), .groups = 'drop')
ggplot(youtube_cat, aes(x =category, y = count)) +
geom_bar(stat = "identity") +
geom_text(aes(label = count), hjust = -0.2, size = 3.5) +
labs(title = "Count of the Most Popular Youtube Videos by Category",
x = "Category",
y = "count") +
coord_flip()
# Fix the axis limit
ggplot(youtube_cat, aes(x =category, y = count)) +
geom_bar(stat = "identity") +
geom_text(aes(label = count), hjust = -0.2, size = 3.5) +
labs(title = "Count of the Most Popular Youtube Videos by Category",
x = "Category",
y = "count") +
ylim(0, 6000) +
coord_flip()
# Data are intentionally ordered
ggplot(youtube_cat, aes(x = reorder(category, count), y = count)) +
geom_bar(stat = "identity") +
geom_text(aes(label = count), hjust = -0.2, size = 3.5) +
labs(title = "Count of the Most Popular Youtube Videos by Category",
x = "Category",
y = "count") +
ylim(0, 6000) +
coord_flip()
# gridline
ggplot(youtube_cat, aes(x = reorder(category, count), y = count)) +
geom_bar(stat = "identity") +
geom_text(aes(label = count), hjust = -0.2, size = 3.5) +
labs(title = "Count of the Most Popular Youtube Videos by Category",
x = "Category",
y = "count") +
ylim(0, 6000) +
coord_flip() +
theme_classic()
```
```{r}
covid <- read.csv("http://raw.githubusercontent.com/owid/covid-19-data/master/public/data/ecdc/full_data.csv")
location <- read.csv('https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/ecdc/locations.csv')
covid_yesterday <- filter(covid, date == '2020-11-08' & location != "World")
covid_yesterday <- merge(covid_yesterday, location, all.x = TRUE)
head(covid_yesterday)
```
```{r}
ggplot(covid_yesterday, aes(x = total_cases, y = total_deaths)) +
geom_point() +
labs(title = "Covid-19 Total Cases & Deaths by Country",
x = "Total Cases",
y = "Total Deaths") +
scale_x_continuous(trans='log10', labels = scales::comma) +
scale_y_continuous(trans='log10', labels = scales::comma)
# Colour by continent
ggplot(covid_yesterday, aes(x = total_cases, y = total_deaths, color = continent)) +
geom_point() +
labs(title = "Covid-19 Total Cases & Deaths by Country",
x = "Total Cases",
y = "Total Deaths") +
scale_x_continuous(trans='log10', labels = scales::comma) +
scale_y_continuous(trans='log10', labels = scales::comma)
cols <- c("North America" = "#e41a1c",
"South America" = "#984ea3",
"Europe" = "#ff7f00",
"Africa" = "#a6cee3",
"Asia" = "#377eb8",
"Oceania" = "#a65628")
ggplot(covid_yesterday, aes(x = total_cases, y = total_deaths, color = continent, na.rm=TRUE)) +
geom_point() +
labs(title = "Covid-19 Total Cases & Deaths by Country",
x = "Total Cases",
y = "Total Deaths",
caption = "Data source: Our World in Data (https://github.com/owidbot)") +
scale_x_continuous(trans='log10', labels = scales::comma) +
scale_y_continuous(trans='log10', labels = scales::comma) +
scale_colour_manual(
values = cols
) +
annotate(geom = "text", x = 100000, y = 30, label = "Singapore", hjust = "left") +
theme(
plot.caption = element_text(hjust = 0)
)
```