Git Product home page Git Product logo

mapchina's Introduction

mapchina

An R package storing the geospatial shapefile (vector data) of China administrative divisions to the county/district-level.

中华人民共和国区县级行政区划矢量地图数据

Build Status Build status Version Download

Installation

To install CRAN version 安装CRAN正式版本:

install.packages("mapchina")

To install the most updated version of the 'dev' branch 安装开发中的最新版本:

if (!require(devtools)) {
  install.packages("devtools")
}

devtools::install_github("xmc811/mapchina", ref = "dev")

Examples 使用示例

1. Browsing the dataframe of the shapefile

查看矢量地图数据

library(mapchina)
head(china)
# Output 输出
Simple feature collection with 6 features and 13 fields
geometry type:  MULTIPOLYGON
dimension:      XY
bbox:           xmin: 115.4248 ymin: 39.44473 xmax: 116.8805 ymax: 41.05936
geographic CRS: WGS 84
# A tibble: 6 x 14
  Code_County Code_Perfecture Code_Province Name_Province Name_Perfecture Name_County Pinyin Pop_2000 Pop_2010 Pop_2017 Pop_2018   Area Density
  <chr>       <chr>           <chr>         <chr>         <chr>           <chr>       <chr>     <dbl>    <dbl>    <dbl>    <dbl>  <dbl>   <dbl>
1 110101      1101            11            北京市        NA              东城区      Dōngc881763   919253       NA   822000   41.8  19670.
2 110102      1101            11            北京市        NA              西城区      Xīché…  1232823  1243315       NA  1179000   50.5  23360.
3 110114      1101            11            北京市        NA              昌平区      Chāng614821  1660501       NA  2108000 1342     1571.
4 110115      1101            11            北京市        NA              大兴区      Dàxīn671444  1365112       NA  1796000 1053     1706.
5 110111      1101            11            北京市        NA              房山区      Fángs814367   944832       NA  1188000 1995      595.
6 110116      1101            11            北京市        NA              怀柔区      Huáir296002   372887       NA   414000 2123      195.
# … with 1 more variable: geometry <MULTIPOLYGON [°]>

The main data object china is a dataframe, with each row as one county/district level administrative division of China. To plot the map of a particular region, you can filter() codes or names to subset the dataframe. The codes are stored in variables Code_Province, Code_Perfecture, and Code_County, and the names are stored in Name_Province, Name_Perfecture, and Name_County. The codes and names follow the 3-level hierarchy: Province (2-digit), Perfecture (4-digit), and County (6-digit).

在矢量数据china中,行政区划的中文名与代码均分为三级:省级、地级与县级,可使用filter()进行任意筛选。

注意:直辖市、特别行政区、**地区、省直管市以及其他特殊行政区域的地级区域名称Name_PerfectureNA。用户可根据作图需要进行修改。


2. Export the data to shapefile

将数据导出为矢量文件

library(sf)
st_write(china, "your/path/to/china.shp", layer_options = "ENCODING=UTF-8")

3. Plotting the population density rank of Beijing, Tianjin, and Hebei

京津冀县级人口密度排名作图

Since the shapefile data is also a dataframe, it can be plotted by ggplot grammer of graphics. The geometric object is geom_sf().

library(tidyverse)
library(sf)

df <- china %>%
        filter(Code_Province %in% c("11","12","13"))

ggplot(data = df) +
        geom_sf(aes(fill = rank(Density))) +
        scale_fill_distiller(palette = "BuPu", direction = 1) +
        theme_bw() +
        theme(legend.position = "none")


4. Plotting the map with customized data

使用新加入的数据作图

New data can be added to the shapefile dataframe as new variables

df$Var <- runif(nrow(df))

ggplot(data = df) +
        geom_sf(aes(fill = Var)) +
        scale_fill_distiller(palette = "YlOrRd") +
        theme_bw() +
        theme(legend.position = "none")


5. Plotting the map with random color, but no two adjacent regions have the same color.

随机颜色作图并使得相邻区域颜色不一样

We use greedy coloring algorithm to solve the problem. The function generate_map_colors() takes a shapefile dataframe as input and outputs a list of index for filling colors.

df2 <- china %>%
        filter(Code_Province %in% c("32"))

ggplot(data = df2) +
        geom_sf(aes(fill = factor(generate_map_colors(df2)))) +
        scale_fill_brewer(palette = "Set3") +
        theme_bw() +
        theme(legend.position = "none")


6. Plotting the administrative divisions at higher levels (province or perfecture level)

对省地级行政区作图

The geometry of county-level divisions can be merged to higher level divisions by functions group_by(), summarise(), and sf::st_union().

县级行政区可被合并为地级或省级行政区进行作图。

df3 <- china %>%
        filter(Code_Province %in% as.character(31:36))

df3 <- df3 %>%
        group_by(Name_Province) %>%
        summarise(geometry = st_union(geometry))

ggplot(data = df3) +
        geom_sf(aes(fill = Name_Province)) +
        scale_fill_brewer(palette = "Set3") +
        theme_bw() +
        theme(legend.position = "none")


7. Adding Chinese characters to the map

在地图中加入汉字标记

To add Chinese characters for the map, R package showtext is required.

if (!require("showtext")) {
install.packages("showtext")

showtext::showtext_auto()

ggplot(data = df3) +
        geom_sf(aes(fill = Name_Province)) +
        scale_fill_brewer(palette = "Set3") +
        theme_bw() +
        theme(legend.position = "none") +
        geom_sf_label(aes(label = Name_Province))


8. A comprehensive example

综合示例:多省地图,按地级行政区划随机着色并加汉字标记,各级区划分界线不同

showtext::showtext_auto()

df4 <- china %>%
        filter(Code_Province %in% c("32","34"))

df4_prov <- df4 %>%
        group_by(Name_Province) %>%
        summarise(geometry = st_union(geometry))

df4_perf <- df4 %>%
        group_by(Name_Perfecture) %>%
        summarise(geometry = st_union(geometry))

ggplot() +
        geom_sf(data = df4_perf,
                aes(fill = factor(generate_map_colors(df4_perf))),
                linetype = "solid", size = 0.5) +
        scale_fill_brewer(palette = "Pastel1") +
        geom_sf(data = df4, alpha = 0, linetype = "dashed", size = 0.2) +
        geom_sf(data = df4_prov, alpha = 0, linetype = "solid", size = 1.2) +
        geom_sf_label(data = df4_perf, aes(label = Name_Perfecture)) +
        theme_bw() +
        theme(legend.position = "none")

mapchina's People

Contributors

xmc811 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mapchina's Issues

cannot adjust line thickness

Dear developers,

I am using your package, I have a problem in using it. I failed to adjust the thickness of line. The codes are as follows.

df4 <- china %>%
    filter(Code_Province %in% c("51"))

df4_prov <- df4 %>%
    group_by(Name_Province) %>%
    summarise(geometry = st_union(geometry))

df4_perf <- df4 %>%
    group_by(Name_Prefecture) %>%
    summarise(geometry = st_union(geometry))

ggplot() +
    geom_sf(data = df4_perf,
            #aes(fill = factor(generate_map_colors(df4_perf))),
            linetype = "solid", color='black', size = 5) +#the size = 5 same with size =1. I did not know why.
    #scale_fill_brewer(palette = "Pastel1") +
    geom_sf(data = df4, alpha = 0.3, linetype = "dotted", size = 0.2, color='gray50') +
    geom_sf(data = df4_prov, alpha = 0, linetype = "solid", size = 1.5) +
    geom_sf_label(data = df4_perf, aes(label = Name_Prefecture)) +
    #theme_minimal() +
    theme(legend.position = "none")

Thank you very much. The code linetype = "solid", color='black', size = 5, size=5 did not woek.

sincerely,
yanpeng

Geometry error when `summarise()`ing Sichuan data

When trying Example 6 with Sichuan (province code 51), like so:

library(mapchina)
library(tidyverse)
library(sf)

df3 <- china %>%
  filter(Code_Province %in% "51")

df3 <- df3 %>%
  group_by(Name_Province) %>%
  summarise(geometry = st_union(geometry))

I get this error:

Error: Problem with `summarise()` column `geometry`.
ℹ `geometry = st_union(geometry)`.
x Evaluation error: Found 1 feature with invalid spherical geometry.
[159] Loop 0 is not valid: Edge 4 has duplicate vertex with edge 8.
ℹ The error occurred in group 1: Name_Province = "四川省".

My setup is as follows:

> sessionInfo()
R Under development (unstable) (2021-07-05 r80597)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.utf8  LC_CTYPE=German_Germany.utf8    LC_MONETARY=German_Germany.utf8 LC_NUMERIC=C                    LC_TIME=German_Germany.utf8    
system code page: 1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] sf_1.0-1        forcats_0.5.1   stringr_1.4.0   dplyr_1.0.7     purrr_0.3.4     readr_1.4.0     tidyr_1.1.3     tibble_3.1.2    ggplot2_3.3.5   tidyverse_1.3.1
[11] mapchina_0.1.0 

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.1   haven_2.4.1        colorspace_2.0-2   vctrs_0.3.8        generics_0.1.0     s2_1.0.6           utf8_1.2.1         rlang_0.4.11      
 [9] e1071_1.7-7        pillar_1.6.1       glue_1.4.2         withr_2.4.2        DBI_1.1.1          dbplyr_2.1.1       wk_0.4.1           modelr_0.1.8      
[17] readxl_1.3.1       lifecycle_1.0.0    munsell_0.5.0      gtable_0.3.0       cellranger_1.1.0   rvest_1.0.0        class_7.3-19       fansi_0.5.0       
[25] broom_0.7.8        Rcpp_1.0.6         KernSmooth_2.23-20 scales_1.1.1       backports_1.2.1    classInt_0.4-3     jsonlite_1.7.2     fs_1.5.0          
[33] hms_1.1.0          stringi_1.6.2      grid_4.2.0         cli_3.0.0          tools_4.2.0        magrittr_2.0.1     proxy_0.4-26       crayon_1.4.1      
[41] pkgconfig_2.0.3    ellipsis_0.3.2     xml2_1.3.2         reprex_2.0.0       lubridate_1.7.10   assertthat_0.2.1   httr_1.4.2         rstudioapi_0.13   

error with testing code

Hi, @xmc811
I installed mapchina, and test the code in README. However, I got the errors:

> library(mapchina)
> library(tidyverse)
> library(sf)
> 
> df <- china %>%
+   filter(Code_Province %in% c("11","12","13"))
> 
> ggplot(data = df) +
+   geom_sf(aes(fill = rank(Density))) +
+   scale_fill_distiller(palette = "BuPu", direction = 1) +
+   theme_bw() +
+   theme(legend.position = "none")
Error in CPL_transform(x, crs, aoi, pipeline, reverse) : 
  OGRCreateCoordinateTransformation() returned NULL: PROJ available?
In addition: Warning message:
In CPL_transform(x, crs, aoi, pipeline, reverse) :
  GDAL Error 1: No PROJ.4 translation for source SRS, coordinate transformation initialization has failed.

Here is the sessionInfo , for you information:

> sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] sf_0.9-7        forcats_0.5.0   stringr_1.4.0   dplyr_1.0.4     purrr_0.3.3     readr_1.3.1     tidyr_1.1.2    
 [8] tibble_3.0.1    ggplot2_3.3.3   tidyverse_1.3.0 mapchina_0.1.0 

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.0   haven_2.3.0        lattice_0.20-38    colorspace_1.4-1   vctrs_0.3.6        generics_0.0.2    
 [7] rlang_0.4.9        e1071_1.7-3        pillar_1.4.3       glue_1.4.2         withr_2.3.0        DBI_1.1.0         
[13] dbplyr_1.4.3       RColorBrewer_1.1-2 modelr_0.1.8       readxl_1.3.1       lifecycle_0.2.0    munsell_0.5.0     
[19] gtable_0.3.0       cellranger_1.1.0   rvest_0.3.5        class_7.3-15       fansi_0.4.1        broom_0.5.6       
[25] Rcpp_1.0.3         KernSmooth_2.23-16 scales_1.1.0       backports_1.1.5    classInt_0.4-2     jsonlite_1.7.2    
[31] farver_2.0.3       fs_1.4.1           digest_0.6.27      hms_0.5.3          stringi_1.4.5      grid_3.6.2        
[37] cli_2.0.2          tools_3.6.2        magrittr_2.0.1     crayon_1.3.4       pkgconfig_2.0.3    ellipsis_0.3.1    
[43] xml2_1.3.1         reprex_0.3.0       lubridate_1.7.8    assertthat_0.2.1   httr_1.4.2         rstudioapi_0.11   
[49] R6_2.4.1           units_0.6-7        nlme_3.1-142       compiler_3.6.2    

Would you mind give me any suggestions?

Best regards,
sandy

在Code_Province level上union数据会导致R crash

以下是我的sessionInfo:

R version 4.0.2 (2020-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.1 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8       
 [4] LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5         class_7.3-17       crayon_1.3.4       sf_0.9-6          
 [5] dplyr_1.0.2        grid_4.0.2         R6_2.4.1           lifecycle_0.2.0   
 [9] DBI_1.1.0          magrittr_1.5       e1071_1.7-4        units_0.6-7       
[13] pillar_1.4.6       KernSmooth_2.23-17 rlang_0.4.7        rstudioapi_0.11   
[17] ellipsis_0.3.1     vctrs_0.3.2        generics_0.0.2     tools_4.0.2       
[21] glue_1.4.1         purrr_0.3.4        compiler_4.0.2     pkgconfig_2.0.3   
[25] classInt_0.4-3     tidyselect_1.1.0   tibble_3.0.3

我用的是data.table来处理数据,code如下

library(mapchina)
library(data.table)
library(ggplot2)
library(sf)
dt_china <- as.data.table(china)
dt_prov <- dt_china[, list(geometry = st_union(geometry)), by=.(Code_Province)]

但是在Code_Prefecture level上面做union没问题(我怀疑_Code_Perfecture_是个typo,地区的英文为_Prefecture_)

dt_pref <- dt_china[, list(geometry = st_union(geometry)), by=.(Code_Perfecture)]
setnames(dt_pref, 'Code_Perfecture', 'Code_Prefecture')

Export data to shapefile

To export data to shapefile, try the following to see if it works:

library(sf)
st_write(china, "your/path/to/china.shp", layer_options = "ENCODING=UTF-8")

Issues with converting sf to dataframe and plotting

Hi,
I just wanted to let you know about an issue that I found and couldn't resolve. For consistency with the rest of my code, I was attempting to convert your "mapchina::china" object to a dataframe using the following code. However, when I plot the results, the polygon lines are not all correct (some lines are connecting to the wrong places).

china_df <- mapchina::china %>%
group_by(Code_Province) %>%
summarise(geometry = st_union(geometry)) %>%
sf_to_df(fill = TRUE) %>%
rename("long" = x, "lat" = y, "group" = sfg_id)

ggplot(china_df, aes(x=long, y=lat)) +
geom_path(aes(group=group), color="gray20", lwd = 0.35)

Best,
Brittany

示例中分组后进行summarise会报错

你好,偶然看到有人推荐这个地图包,准备试一下,然后跟着示例做一下发现分组汇总会报错;

df4_prov <- df4 %>%

  • group_by(Name_Province) %>%
    
  • summarise(geometry = st_union(geometry))
    

错误: Problem with summarise() input geometry.
x Input geometry must return compatible vectors across groups
i Input geometry is st_union(geometry).
i Result type for group 1 (Name_Province = "安徽省"): <sfc_POLYGON>.
i Result type for group 2 (Name_Province = "江苏省"): <sfc_MULTIPOLYGON>.

R版本3.6.3

plotly compatibility

@xmc811 xu 你好,再想问一个问题. 就是根据的的示例里, df2生成的地图可以和 plotly进行配合, 生成一些交互动作,df4生成的地图就不行. 请问一下这二者可以配合使用吗

Originally posted by @antonio-yu in #1 (comment)

plotly我没用过。如果df2可以但是df4不行的话,我猜一个可能的原因是:df2只是用ggplot2画了一层,而df4的例子画了三层(代码中有三次geom_sf())。所以也许可以试试调整这几层之间的上下顺序来得到plotly的交互性。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.