install.packages(“tidyverse”)
library(“tidyverse”)
library(ggplot2) library(dplyr)
data()
library(help = “ggplot2”) library(help = “tidyverse”) library(help = “tibble”) library(help = “tidyr”)
ls(“package:datasets”)
##1.5 Đọc thử dữ liệu mẫu MPG
ggplot2::mpg
mpg
View(mpg)
##2.1 Đánh giá thống kê thông tin chung về bộ dữ liệu xe
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
summary(mpg)
## manufacturer model displ year
## Length:234 Length:234 Min. :1.600 Min. :1999
## Class :character Class :character 1st Qu.:2.400 1st Qu.:1999
## Mode :character Mode :character Median :3.300 Median :2004
## Mean :3.472 Mean :2004
## 3rd Qu.:4.600 3rd Qu.:2008
## Max. :7.000 Max. :2008
## cyl trans drv cty
## Min. :4.000 Length:234 Length:234 Min. : 9.00
## 1st Qu.:4.000 Class :character Class :character 1st Qu.:14.00
## Median :6.000 Mode :character Mode :character Median :17.00
## Mean :5.889 Mean :16.86
## 3rd Qu.:8.000 3rd Qu.:19.00
## Max. :8.000 Max. :35.00
## hwy fl class
## Min. :12.00 Length:234 Length:234
## 1st Qu.:18.00 Class :character Class :character
## Median :24.00 Mode :character Mode :character
## Mean :23.44
## 3rd Qu.:27.00
## Max. :44.00
## 2.2 Khối lượng bộ dữ liệu --> 243 rows * 11 Columns
dim(mpg)
## [1] 234 11
glimpse(mpg)
## Rows: 234
## Columns: 11
## $ manufacturer <chr> "audi", "audi", "audi", "audi", "audi", "audi", "audi", "…
## $ model <chr> "a4", "a4", "a4", "a4", "a4", "a4", "a4", "a4 quattro", "…
## $ displ <dbl> 1.8, 1.8, 2.0, 2.0, 2.8, 2.8, 3.1, 1.8, 1.8, 2.0, 2.0, 2.…
## $ year <int> 1999, 1999, 2008, 2008, 1999, 1999, 2008, 1999, 1999, 200…
## $ cyl <int> 4, 4, 4, 4, 6, 6, 6, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 8, 8, …
## $ trans <chr> "auto(l5)", "manual(m5)", "manual(m6)", "auto(av)", "auto…
## $ drv <chr> "f", "f", "f", "f", "f", "f", "f", "4", "4", "4", "4", "4…
## $ cty <int> 18, 21, 20, 21, 16, 18, 18, 18, 16, 20, 19, 15, 17, 17, 1…
## $ hwy <int> 29, 29, 31, 30, 26, 26, 27, 26, 25, 28, 27, 25, 25, 25, 2…
## $ fl <chr> "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p…
## $ class <chr> "compact", "compact", "compact", "compact", "compact", "c…
## 2.3 Tên các cột dữ liệu
names(mpg)
## [1] "manufacturer" "model" "displ" "year" "cyl"
## [6] "trans" "drv" "cty" "hwy" "fl"
## [11] "class"
str(mpg)
?mpg
library(ggplot2)
plot(mpg$displ, mpg$hwy)
## 3.1 chi tiết
plot(mpg$displ,mpg$hwy, main = " Biểu đồ đánh giá phân bổ hiệu suất và kích thước động cơ",
xlab = "hiệu suất",
ylab = "Hiệu suất nhiên liệu",
col = "red",
pch = 17,
cex = 1)
## vẽ với ggplot2
ggplot(data = mpg) +
geom_point(mapping = aes(x= displ, y = hwy))
## chi tiết màu sắc và nhãn
ggplot(mpg, aes(x= displ, y= hwy)) +
geom_point(color="green") +
labs(title = " Biểu đồ phân bổ hiệu suất và kích thước động cơ",
x= "kích thước động cơ",
y="mưc tiêu thụ nhiên liệu -lít" )
library(ggplot2)
hist(mpg$hwy)
## 3.2 chi tiết
hist(mpg$hwy,
main = "Biểu đồ phân bổ hiệu suất tiêu hao nhiên liệu",
xlab = "Số lượng lit tiêu hao",
ylab = " số lượng động cơ",
col = "blue")
hist(mpg$year,
main = " Biểu đồ phân bổ xe sản xuất theo năm")