Day 2. Data visualisation
crime = read.csv("C:\\Thach\\VN trips\\VN trip 5 (Apr 2023)\\Datasets\\Crime dataset reduced.csv", header = T)
Việc 1: Đánh giá phân bố của police
hist(crime$police, col = "blue", border = "white")
library(ggplot2)

p = ggplot(data = crime, aes(x = police))
p + geom_histogram(fill = "blue", col = "white") + labs(x = "Police presence", y = "Number of people", title = "Distribution of police presence")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Việc 2: So sánh phân bố cảnh sát giữa 2 TP Fresno và Los
Angeles
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.1 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ lubridate 1.9.2 ✔ tibble 3.2.1
## ✔ purrr 1.0.1 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
set.1 = crime %>% filter(city %in% c("fresno", "losangel"))
p = ggplot(data = set.1, aes(x = police, fill = city))
p + geom_density() + labs(x = "Police presence", y = "Number of observations", title = "Distrbution of police presence between Fresno and Los Angeles")

Việc 3: So sánh phân bố tỉ lệ tội phạm giữa 2 TP Fresno và Los
Angeles bằng biểu đồ hộp
p = ggplot(data = set.1, aes(x = city, y = police, col = city))
p + geom_boxplot() + geom_jitter(alpha = 0.2) +
labs(x = "City", y = "Police presence", title = "Distribution of police presence by cities")

Việc 4: Tỉ lệ tội phạm liên quan như thế nào đến hiện diện của cảnh
sát
4.1: Chung cho cả 5 thành phố
p = ggplot(data = crime, aes(x = police, y = robbery))
p + geom_point() + stat_smooth(aes(group = 1)) +
labs(x = "Police presence", y = "Criminal proportion", title = "Correlation between police presence and criminal proportion")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Chỉ ở TP Los Angeles
set.la = subset(crime, city == "losangel")
p = ggplot(data = set.la, aes(x = police, y = robbery))
p + geom_point() + stat_smooth(aes(group = 1)) +
labs(x = "Police presence", y = "Criminal proportion", title = "Correlation between police presence and criminal proportion in Los Angeles")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Chỉ trong năm 1992
set.92 = subset(crime, year == "92")
library(ggrepel)
p = ggplot(data = set.92, aes(x = police, y = robbery, label = city))
p + geom_point() + geom_smooth(method = "lm") + geom_text_repel(aes(col = city)) +
labs(x = "Police presence", y = "Criminal proportion", title = "Correlation between police presence and criminal proportion in 1992")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: The following aesthetics were dropped during statistical transformation: label
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
## the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
## variable into a factor?

Việc 5: Vẽ mối liên quan giữa hiện diện của cảnh sát và tỉ lệ tội
phạm cho từng TP trong 1 biểu đồ
p = ggplot(data = crime, aes(x = police, y = robbery, col = city))
p + geom_point() + stat_smooth(method = "lm") +
labs(x = "Police presence", y = "Criminal proportion", title = "Correlation between police presence and criminal proportion by cities")
## `geom_smooth()` using formula = 'y ~ x'

p + geom_point() + facet_grid(. ~ city) + stat_smooth(aes(group = 1)) +
labs(x = "Police presence", y = "Criminal proportion", title = "Correlation between police presence and criminal proportion by cities")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

Việc 6: Đánh giá tiến bộ của học sinh
wil = read.csv("C:\\Thach\\VN trips\\VN trip 5 (Apr 2023)\\Datasets\\willett.csv", header = T)
6.1 Biểu đồ bánh tằm mô tả tiến bộ của từng học sinh
ggplot(data=wil, aes(x=time, y=y, group=id, col=id)) + geom_line() + theme(legend.position="none")

6.2 Biểu đồ mô tả tiến bộ cho từng học sinh
ggplot(data=wil, aes(x=time, y=y, col=factor(id))) + geom_point() + facet_wrap(~id) + theme(legend.position="none")

Việc 7: Ghi lại các lệnh và chia sẻ trên tài khoản RPubs của
bạn