1. Kết nối dữ liệu
dulieu <-read.csv("http://solieu.vip/csv/dothi2.csv")
head(dulieu)
## ï..STT Tinh Nam GDP Ploai Bap Lua Heo Ga
## 1 1 Long An 2020 -11 Tinh1 18.43 15.45 21.99 30.61
## 2 2 Dong Thap 2020 -13 Tinh2 9.24 27.78 27.55 25.69
## 3 3 An Giang 2020 -15 Tinh3 14.87 22.49 21.36 20.07
## 4 4 Kien Giang 2020 -17 Tinh4 17.26 24.18 25.61 26.65
## 5 5 Soc Trang 2020 -19 Tinh5 16.85 21.07 22.55 21.12
## 6 6 Tien Giang 2020 -21 Tinh6 15.98 24.75 31.25 29.61
2. Vẽ đồ thị mutil_line
library(ggplot2)
m <- ggplot(data=dulieu, aes(x=GDP)) +
geom_line(aes(y=Bap), col="red", size=1.5) +
geom_line(aes(y=Lua), col="green", size=1.5) +
geom_line(aes(y=Heo), col="blue", size=1.5) +
geom_line(aes(y=Ga), col="yellow", size=1.5)
m

3. Thêm text giới thiệu vào
dulieumoi <-dulieu[1,]
head(dulieumoi)
## ï..STT Tinh Nam GDP Ploai Bap Lua Heo Ga
## 1 1 Long An 2020 -11 Tinh1 18.43 15.45 21.99 30.61
m2 <- m +
geom_text(data=dulieumoi, aes(x=GDP, y=Bap, label="Bap ngo"), col="red", vjust=-1) +
geom_text(data=dulieumoi, aes(x=GDP, y=Lua, label=" Lua nuoc"), col="green", vjust=-1) +
geom_text(data=dulieumoi, aes(x=GDP, y=Heo), label="Thit heo", col="blue", vjust=-1) +
geom_text(data=dulieumoi, aes(x=GDP, y=Ga, label="Thit ga"), col="yellow", vjust=-1)
m2

4. Hiện thị giá trị theo yêu cầu
library(dplyr)
##
## 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
m3 <- m2 +
geom_text(data=filter(dulieu,Bap==max(Bap)), aes(y=Bap, label=Bap), col="red", vjust=-1) +
geom_text(data=filter(dulieu,Lua==max(Lua)), aes(y=Lua, label=Lua), col="green", vjust=-1) +
geom_text(data=filter(dulieu,Heo==max(Heo)), aes(y=Heo, label=Heo), col="blue", vjust=-1) +
geom_text(data=filter(dulieu,Ga==max(Ga)), aes(y=Ga, label=Ga), col="yellow", vjust=-1)
m3

5. Kéo dài đồ thị
m4 <- m3 + theme_bw() +
scale_y_discrete(expand = c(0.1,0)) +
#scale_x_discrete(expand = c(0.1,0.1))
geom_point(data=filter(dulieu,Bap==max(Bap)), aes(y=Bap), col="red", size=3) +
geom_point(data=filter(dulieu,Lua==max(Lua)), aes(y=Lua), col="green", size=3) +
geom_point(data=filter(dulieu,Heo==max(Heo)), aes(y=Heo), col="blue", size=3) +
geom_point(data=filter(dulieu,Ga==max(Ga)), aes(y=Ga), col="yellow", size=3)
m4
