28 February 1919

Plan-A

setwd("C:\\Users\\Schrodi Lab\\Documents\\GitHub\\rheumatoidarthritis\\RA\\ASA\\MIR\\manuscript\\map")
install.packages("maptools")
install.packages("rgdal")
install.packages("maps")
install.packages("usmap")
install.packages("data.table")
install.packages("ggsn")
install.packages("ggrepel")
install.packages("rmarkdown")
library(ggplot2)
library(maps)
library(usmap)
library(data.table)
library(ggsn) 
library(ggrepel) 
library("maptools")
library("rgdal")
library("rmarkdown")
china_map <- rgdal::readOGR("bou2_4p.shp")
china_province = setDT(china_map@data)
setnames(china_province, "NAME", "province")
china_province[, province:=iconv(province, from = "GBK", to = "UTF-8")] 
china_province[, id:= .I-1] 
china_province[, table(province)]
china_province[, province:= as.factor(province)]
dt_china = setDT(fortify(china_map))
dt_china[, id:= as.numeric(id)]
setkey(china_province, id); setkey(dt_china, id)
dt_china <- china_province[dt_china]
province_CH <- china_province[, levels(province)] # the CH are in UTF-8 code
province_EN <- c("Shanghai", "Yunnan", "Inner Mongolia", "Beijing", "Taiwan",
                 "Jilin", "Sichuan", "Tianjin City", "Ningxia", "Anhui",
                 "Shandong", "Shanxi", "Guangdong", "Guangxi ", "Xinjiang",
                 "Jiangsu", "Jiangxi", "Hebei", "Henan", "Zhejiang",
                 "Hainan", "Hubei", "Hunan", "Gansu", "Fujian",
                 "Tibet", "Guizhou", "Liaoning", "Chongqing", "Shaanxi",
                 "Qinghai", "Hong Kong", "Heilongjiang"
)
value <- c(8893483, 12695396,  8470472,  7355291, 23193638,  9162183, 26383458,  3963604,  1945064, 19322432, 30794664, 10654162, 32222752, 13467663,  6902850, 25635291, 11847841, 20813492, 26404973, 20060115, 2451819, 17253385, 19029894,  7113833, 11971873,   689521, 10745630, 15334912, 10272559, 11084516, 1586635,  7026400, 13192935)
input_data <- data.table(province_CH, province_EN, value)
setkey(input_data, province_CH)
setkey(dt_china, province)
china_map_pop <- input_data[dt_china[AREA>0.1,]]
label_dt <- china_map_pop[, .(x = mean(range(long)), y = mean(range(lat)), province_EN, province_CH), by = id]
label_dt <- unique(label_dt)
setkey(label_dt, province_EN)
# I have fine-tuned the label position of some provinces
label_dt['Inner Mongolia', `:=` (x = 110, y = 42)]
label_dt['Gansu', `:=` (x = 96.3, y = 40)]
label_dt['Hebei', `:=` (x = 115.5, y = 38.5)]
label_dt['Liaoning', `:=` (x = 123, y = 41.5)]
rmarkdown::paged_table(china_map_pop[!is.na(province_CH),])

ggplot(china_map_pop, aes(x = long, y = lat, group = group, fill="blank")) +
  labs(fill = "Population (outdated)")+
  geom_polygon()+
  geom_path()+
  blank() 

  scale_fill_gradientn(colours=rev(heat.colors(10)),na.value="grey90",
  guide = guide_colourbar(barwidth = 0.8, barheight = 10)) + 
  + 
  geom_text(data = label_dt, aes(x=x, y=y, label = ""),inherit.aes = F) +
  scalebar(data = china_map_pop, dist = 500, dist_unit = "km",
  transform = T, model = "WGS84",
  border.size = 0.4, st.size = 2
  ) 

Plan-B

install.packages("maptools")
install.packages("rgdal")
install.packages("maps")
install.packages("usmap")
install.packages("data.table")
install.packages("ggsn")
install.packages("ggrepel")
install.packages("rmarkdown")
install.packages("mapdata")

library(ggplot2)
library(maps)
library(usmap)
library(data.table)
library(ggsn) 
library(ggrepel) 
library("maptools")
library("rgdal")
library("rmarkdown")
library(mapdata)
library(maptools)

map("china", col = "gray40", ylim = c(18,54))
china_map <- readShapePoly("bou2_4p.shp")
Shanghai = china_map[china_map$ADCODE99 == 310000,]
plot(Shanghai)

mydat = readShapePoly("bou2_4p.shp")
Shanghai = mydat[substr(as.character(mydat$ADCODE99), 1, 2) == '31',]
mysh = fortify(Shanghai, region = 'NAME99')

head(fortify(Shanghai))

mysh = transform(mysh, id = iconv(id, from = 'GBK'), group = iconv(group, from = 'GBK'))
head(mysh)


Shanghai = mydat[substr(as.character(mydat$ADCODE99), 1, 2) == '31',]
mysh = fortify(Shanghai, region = 'NAME99')
mysh = transform(mysh, id = iconv(id, from = 'GBK'), group = iconv(group, from = 'GBK'))
head(mysh)


install.packages("mapproj")
library(maptools)
library(ggplot2)
library(mapproj)
mydat = readShapePoly("bou2_4p.shp")
mymap<-ggplot(data = fortify(mydat)) +
  geom_polygon(aes(x = long, y = lat, group = id), colour = "black",
  fill = NA) +
  blank()
print(mymap + coord_map())
Shanghai = mydat[mydat$ADCODE99 == 310000,]
plot(Shanghai)
mysh=fortify(Shanghai)
mysh$id=1:nrow(mysh)
head(mysh)

sample = data.frame(id = unique(sort(mysh$id)))
sample$num = runif(length(mysh$id))
sample

csmap = ggplot(sample) +
  geom_map(aes(map_id = id, fill = num), color = "white", map = mysh) +
  scale_fill_gradient(high = "darkgreen",low = "lightgreen") +
  expand_limits(mysh) + coord_map() + blank()
print(csmap)

head(mysh)


blog comments powered by Disqus