【问题标题】:Create a map of country in R在R中创建国家地图
【发布时间】:2021-06-09 13:10:31
【问题描述】:

我需要根据形状文件(最好是彩色的)创建一个国家(泰国)的地图,并添加省份的代码(从 10 到 96,77 代码)和来自 h.сsv 的相应系数(也是 77 values) 文件在地图上。

我正在尝试展示我的两个代码(也许其中一个更适合地图): 第一个:

library(raster)
library(rasterVis)
library(rgdal)
library(rgeos)
library(dismo)
library(sp)
library(maptools)
library(maps)
library(mapdata)
library(XML)
library(foreign)
library(latticeExtra)
library(shapefiles)
library(RColorBrewer)
library(GISTools)
#library(SDMTools)

library(dplyr)
library(tidyr)
library(tidyverse)
library(lubridate)



## preparing shapefiles
thailand_district <- shapefile("C:/usa/archive/TH_Province2012.shp")
thailand_district
crs(thailand_district)
names(thailand_district)
thailand_district_lonlat<- spTransform(thailand_district, CRS("+proj=longlat +datum=WGS84"))
crs(thailand_district_lonlat)

thailand_district_lonlat_s<-gSimplify(thailand_district_lonlat, tol=0.02, topologyPreserve=TRUE)

district_id<-thailand_district_lonlat$A_CODE
province_id<-thailand_district_lonlat$P_CODE


thailand_prov <- shapefile("C:/usa/archive/TH_Province2012.shp")
thailand_prov
crs(thailand_prov)
thailand_prov_lonlat<- spTransform(thailand_prov, CRS("+proj=longlat +datum=WGS84"))
crs(thailand_prov_lonlat)

thailand_prov_lonlat_s<-gSimplify(thailand_prov_lonlat, tol=0.02, topologyPreserve=TRUE)


## preparing centroids
thailand_district_centroids <- getSpPPolygonsLabptSlots(thailand_district_lonlat)
head(thailand_district_centroids)

district_centroids<- data.frame(province_id,district_id, thailand_district_centroids[,1],thailand_district_centroids[,2])

district_centroids<-read.csv("data.scrub.district.csv")


names(district_centroids) <- c("province_id","district_id","longitude", "latitude")


ex<-district_centroids
coordinates(ex)<- cbind("longitude", "latitude")
plot(ex)


thailand_province_centroids <- getSpPPolygonsLabptSlots(thailand_prov_lonlat)
head(thailand_province_centroids)



## read cases
scrub1<-read_csv("C:/usa/archive/scrub_2003-07_180319.csv")
               
names(scrub1)
scrub1<-dplyr::select(scrub1,Address, The.day.began.to.get.sick..M.D.Y.)
scrub1 <- dplyr::rename(scrub1,date=The.day.began.to.get.sick..M.D.Y.)
scrub1 <-na.omit(scrub1)
scrub2<-read_csv("C:/usa/archive/scrub_2008-11_180319.csv")
scrub2<-dplyr::select(scrub2,Address, The.day.began.to.get.sick..M.D.Y.)
scrub2 <- dplyr::rename(scrub2,date=The.day.began.to.get.sick..M.D.Y.)
scrub3<-read_csv("C:/usa/archive/scrub_2012-18_180319.csv")
scrub3<-dplyr::select(scrub3,Address, The.day.began.to.get.sick..M.D.Y.)
scrub3 <- dplyr::rename(scrub3,date=The.day.began.to.get.sick..M.D.Y.)

scrub<-dplyr::union(scrub1, scrub2)
scrub<-dplyr::union(scrub, scrub3)

scrub$district_id<-(tamboon_id=substr(scrub$Address, 1,4))

scrub <- dplyr::rename(scrub,village_id=Address) 
scrub<-tidyr::drop_na(scrub,village_id)

scrub_district <- dplyr::select(scrub,district_id)


# preparation
scrub$date1 <- as.Date(scrub$date,
                       format = "%d/%m/%Y")

scrub$year<-lubridate::year(scrub$date1)
scrub$YearMonth<-format(scrub$date1, "%Y-%m")


scrubYear<-scrub %>%
  drop_na() %>%
  group_by(year) %>% 
  summarise(scrubcases= n())

scrub$district_id<-as.factor(scrub$district_id)
is.factor(scrub$district_id)

scrubDistrict<-scrub %>%
  drop_na() %>%
  group_by(district_id) %>% 
  summarise(scrubcases= n())

district_centroids

district_centroids2<-district_centroids %>%
  unite("district_id", province_id,district_id2)

district_centroids2$district_id<-gsub("_", "",district_centroids2$district_id )

scrubdistict_longlat<-dplyr::left_join(district_centroids2,scrubDistrict,
                                       by="district_id")


write_csv(scrubdistict_longlat,"data.scrub.district.csv")




scrubClean<-read_csv("data.scrub.district.csv")

mydata<-dplyr::filter(scrubClean, scrubcases > 0)

ex2<-mydata
coordinates(ex2)<-c("longitude","latitude")
bubble(ex2,"scrubcases")


# map
library(tmap)
library(tmaptools)

proj4string(ex2) <- proj4string(thailand_district_lonlat)

tmaptools::palette_explorer()

# thailand

tm1<-tm_shape(thailand_prov_lonlat_s) +
  tm_fill(NA) + tm_borders("black")+
  tm_borders("black")+
  tm_compass(type = "8star", position = c("right", "top"),size = 2)+
  tm_scale_bar(breaks = c(0, 100, 100), size = 0.5, position = c("right", "bottom"))+
  tm_style( "beaver")

tm1

tm2<-tm_shape(thailand_district_lonlat_s)+
  tm_polygons()+
  tm_shape(ex2) +
  tm_bubbles("scrubcases",col = "lightblue",scale = 2,
             border.col = "black", border.alpha = .5, 
             contrast=1, 
             title.size="cases / district")

tm2
library(dplyr)
library(tidyr)
library(tmap)

data(World)
names(World)
mygideon<-read_csv("data.gideon.iso.final.csv") %>%
  group_by(iso_a3) %>%
  summarise(total.outbreaks=n())

world2<-dplyr::left_join(World,mygideon2,by="iso_a3")

tm_shape(world2) +
  tm_polygons("total.outbreaks",
              style = "fixed",
              breaks = c(1,50,100,250,500,750, 1000, 1500, 2500),
              palette="Oranges",
              title = "Total outbreaks (1940-2018)", contrast = 1.2,
              border.col = "gray30", id = "name", n=6,
              legend.hist = TRUE,alpha = 1)+
  tm_layout(legend.outside = TRUE)

 **Error in data.frame(province_id, district_id, thailand_district_centroids[,  :
 arguments imply differing number of rows : 0, 77.use coordinates method**

2n 码。

library(raster)
library(rasterVis)
library(rgdal)
library(rgeos)
library(dismo)
library(sp)
library(maptools)
library(maps)
library(mapdata)
library(XML)
library(foreign)
library(latticeExtra)
library(shapefiles)
library(RColorBrewer)
library(GISTools)
#library(SDMTools)

library(dplyr)
library(tidyr)
library(tidyverse)
library(rgeos) # to fortify without needing gpclib
library(ggplot2)
library(scales) # for formatting ggplot scales with commas
thamap <-  readOGR("C:/usa/archive/TH_Province2012.shp")
thamap
crs(thamap)
thamap_lonlat<- spTransform(thamap, CRS("+proj=longlat +datum=WGS84"))
crs(thamap_lonlat)

thamap_lonlat_s<-gSimplify(thamap_lonlat, tol=0.02, topologyPreserve=TRUE)
thamap.fort <- fortify(thamap)
idList <-thamap@data$PROV_CODE

centroids.df <- as.data.frame(coordinates(thamap))
names(centroids.df) <- c("Longitude", "Latitude")
info <- read.csv("h.csv")
pop.df <- data.frame(idList,info,centroids.df)
ggplot(pop.df, aes(map_id = idList)) + #"id" is col in your df, not in the map object 
  geom_map(aes(fill = info), colour= "grey", map = thamap.fort) +
  expand_limits(x = thamap.fort$long, y = thamap.fort$lat) +
  scale_fill_gradient(high = "red", low = "white", guide = "colorbar", labels = comma) +
  geom_text(aes(label = id, x = Longitude, y = Latitude)) + #add labels at centroids
  coord_equal(xlim = c(-90,-30), ylim = c(-60, 20)) + 
  labs(x = "Longitude", y = "Latitude", title = "map Thailand") +
  theme_bw() 
Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
 Aesthetics must be valid data columns. Problematic aesthetic(s): label = id. 
Did you mistype the name of a data column or forget to add after_stat()?

如果您能帮我修改一下代码以创建地图,我将不胜感激。

你也可以告诉我,如何将csv文件中的数据(77个值)添加到省份代码附近的地图上?

非常感谢您的帮助

【问题讨论】:

    标签: r csv dictionary ggplot2 shapes


    【解决方案1】:

    这是使用tmap 的解决方案。包含泰国边界(国家和省份)的 shapefile 可从https://data.humdata.org/dataset/thailand-administrative-boundaries 获得。省份代码(10-96)也以字符格式包含在数据集中,可以轻松提取。

    library(tmap)
    library(sf)
    library(tidyverse)
    
    provinces <- st_read(dsn = "tha_adm_rtsd_itos_20190221_SHP_PART_1/tha_admbnda_adm1_rtsd_20190221.shp") %>% 
      as.tibble() %>% 
      separate(ADM1_PCODE, into = c("pcode_text", "pcode_num"), sep = "(?<=[A-Za-z])(?=[0-9])") %>% 
      select(geometry, pcode_num) %>% 
      st_as_sf()
      
    tm_shape(provinces) +
        tm_fill(col = "MAP_COLORS") +
        tm_text("pcode_num", size = .5) +
    tm_borders(lwd = .7, col = "black")
    

    【讨论】:

      猜你喜欢
      • 2012-06-28
      • 2021-07-18
      • 2020-01-17
      • 2021-11-13
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      相关资源
      最近更新 更多