R Markdown
setwd("D:/Hoc R/Cac bai viet tong hop")
pan<-read.csv("seagame.csv")
pkg <- c("rvest", "tidyverse", "ggmap", "magrittr")
lapply(pkg, require, character.only = TRUE)
## Loading required package: rvest
## Warning: package 'rvest' was built under R version 3.3.3
## Loading required package: xml2
## Warning: package 'xml2' was built under R version 3.3.3
## Loading required package: tidyverse
## Warning: package 'tidyverse' was built under R version 3.3.3
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Warning: package 'ggplot2' was built under R version 3.3.3
## Warning: package 'tibble' was built under R version 3.3.3
## Warning: package 'tidyr' was built under R version 3.3.3
## Warning: package 'readr' was built under R version 3.3.3
## Warning: package 'purrr' was built under R version 3.3.3
## Warning: package 'dplyr' was built under R version 3.3.3
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag(): dplyr, stats
## Loading required package: ggmap
## Warning: package 'ggmap' was built under R version 3.3.3
## Loading required package: magrittr
##
## Attaching package: 'magrittr'
## The following object is masked from 'package:ggmap':
##
## inset
## The following object is masked from 'package:purrr':
##
## set_names
## The following object is masked from 'package:tidyr':
##
## extract
## [[1]]
## [1] TRUE
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] TRUE
##
## [[4]]
## [1] TRUE
library(tidyverse)
library(ggplot2)
library(ggmap)
library(maps)
## Warning: package 'maps' was built under R version 3.3.3
##
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
##
## map
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 3.3.3
pan %<>% mutate(Country= as.character(Country))
## Warning: package 'bindrcpp' was built under R version 3.3.3
pan %<>% mutate(city= as.character(city))# chuyển về chr
# Lấy thông tin về kinh độ và vĩ độ của các thành phố
# với hàm geocode() của gói ggmap:
geo_data<-pan$city %>% geocode()
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Kuala%20Lumpur&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bangkok&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Hanoi&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Singapore&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Jakarta&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Manila&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Naypyidaw&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Phnom%20Penh&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Vientiane&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Bandar%20Seri%20Begawan&sensor=false
pan<-cbind(pan,geo_data) # Hợp nhất dữ liệu địa lí
map_data <- map_data("world")
Seagame29 <- ggplot() +
geom_polygon(data = map_data,
aes(long, lat, group = group),
fill = "white", colour = "pink") +
geom_point(data = pan,
aes(lon, lat, size = Gold),
color = "yellow", alpha =.8) +
coord_cartesian(xlim = c(90, 125), ylim = c(-10,30)) +
labs(x = NULL, y = NULL) +
scale_size_continuous(range = c(2, 15),
breaks = c(1000000, 5000000, 10000000),
name = "Gold", labels = scales::comma_format())+
geom_text_repel(data=pan,aes(lon,lat,label=Country),size=3,colour="black",force=0.1)+
labs(title="Xếp hạng Seagame 29",subtitle="Vàng",caption="nguồn: nghiencuudinhluong.com")
Seagame29
## Warning: Removed 1 rows containing missing values (geom_point).
## Warning: Removed 1 rows containing missing values (geom_text_repel).
