Last active
February 2, 2023 10:16
-
-
Save Ray901/d0c736b4677a7400da2c to your computer and use it in GitHub Desktop.
plot Taiwan map with R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(maptools) | |
library(xlsx) | |
countyDat<-data.frame(Eng=c("Taipei","Taipei City","Taoyuan","Taichung","Taichung City","Tainan", | |
"Tainan City","Kaohsiung","Kaohsiung City","Ilan","Hsinchu","Miaoli", | |
"Changhwa","Nantou","Yunlin","Chiayi","Pingtung","Taitung","Hualien", | |
"Penghu","Keelung City","Hsinchu","Chiayi","Kinmen","Lienchiang" | |
), | |
Chn=c("新北市","臺北市","桃園市","臺中市","臺中市","臺南市", | |
"臺南市","高雄市","高雄市","宜蘭縣","新竹縣","苗栗縣", | |
"彰化縣","南投縣","雲林縣","嘉義縣","屏東縣","臺東縣","花蓮縣", | |
"澎湖縣","基隆市","新竹市","嘉義市","金門縣","連江縣" | |
), | |
stringsAsFactors = F | |
) | |
gpclibPermit() | |
load("/home/ray/R_code/graph/TWN_adm/TWN_adm2.RData") | |
map <- fortify(gadm,region='ID_2') | |
map$region <- gadm$NAME_2[match(map$id,gadm$ID_2)] | |
map <- map[which(map$region!=""),] | |
# Load Population Data | |
pdata <- read.xlsx('/home/ray/importData/m0s710405.xls',1) | |
pdata <- | |
data.frame( | |
region = as.character(pdata[grep("市|縣",pdata[,1]),1]), | |
population = as.numeric(as.character(pdata[grep("市|縣",pdata[,1])-1,3])), | |
stringsAsFactors = F) | |
pdata$region <- countyDat$Eng[match(pdata$region,countyDat$Chn)] | |
pdata <- pdata[which(pdata$region %in% map$region),] | |
ggplot(data = pdata) + | |
geom_map(map =map,aes(map_id = region, fill = population), | |
color='gray40' ) + | |
expand_limits(x = map$long, y = map$lat) + | |
scale_fill_continuous(high='red2',low='white') + | |
theme(axis.text=element_blank(), | |
axis.ticks=element_blank(), | |
axis.title=element_blank(), | |
panel.grid.major=element_blank(), | |
panel.background=element_blank()) + | |
labs(title="台灣人口統計") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment