试图在 R 中制作一个基本的 Choropleth 地图,但进展不顺利强化似乎不起作用

问题描述

我曾尝试遵循 R 的 Rob (https://rforjournalists.com/2019/10/20/how-to-make-a-uk-local-authority-choropleth-map-in-r/) 为记者所做的相同代码,但我正在努力。我对 R 很陌生,因此非常感谢任何帮助。

我想由地方当局制作英国的等值线地图。我已经从 ONS 网站 https://geoportal.statistics.gov.uk/datasets/local-authority-districts-december-2020-uk-buc 下载了一个 shapefile,并且有关于托儿服务提供者进入率的数据。 https://docs.google.com/spreadsheets/d/1YzOg_wKgO4vNMqr8TchPtJcqqYU6JFDGjmdDeQPWCLo/edit#gid=0

然而,我什至无法制作地图。

这是我的代码

setwd('/Users/jtbbl/Library/Mobile 
Documents/com~apple~CloudDocs/Map/Local_Authority_districts_(December_2020)_UK_BUC')
read_sf('Local_Authority_districts_(December_2020)_UK_BUC.shp')
shp <- readOGR('Local_Authority_districts_(December_2020)_UK_BUC.shp')
shp <- fortify(shp,region = 'LAD20CD')

setwd('/Users/jtbbl/Library/Mobile Documents/com~apple~CloudDocs/Map')
Entry_Rate <- read_csv('Choropleth map ready2go.csv')

shp <- merge(shp,Entry_Rate,by.x = 'id',by.y = 'order')
shp <- arrange(shp,order)


p <- ggplot(data = shp,aes(x = long,y = lat,group = group,fill = id)) + 
     geom_polygon(colour = 'blue') + coord_equal() + theme_void() +
     ggtitle('Percentage of births where one of both parents were born outside the UK',subtitle = 'England and Wales,2018')

这是输出

setwd('/Users/jtbbl/Library/Mobile 
Documents/com~apple~CloudDocs/Map/Local_Authority_districts_(December_2020)_UK_BUC')
> read_sf('Local_Authority_districts_(December_2020)_UK_BUC.shp')
Simple feature collection with 379 features and 10 fields
geometry type:  MULTIpolyGON
dimension:      XY
bBox:           xmin: -116.1928 ymin: 7054.1 xmax: 655644.8 ymax: 1218616
projected CRS:  OSGB 1936 / British National Grid
# A tibble: 379 x 11
     OBJECTID LAD20CD  LAD20NM        LAD20NMW  BNG_E  BNG_N   LONG   LAT Shape__Are
    <int> <chr>    <chr>          <chr>     <int>  <int>  <dbl> <dbl>      <dbl>
1        1 E060000… Hartlepool     NA       447160 531474 -1.27   54.7  96029871.
2        2 E060000… Middlesbrough  NA       451141 516887 -1.21   54.5  55231394.
3        3 E060000… Redcar and Cl… NA       464361 519597 -1.01   54.6 248342848.
4        4 E060000… Stockton-on-T… NA       444940 518183 -1.31   54.6 205231414.
5        5 E060000… Darlington     NA       428029 515648 -1.57   54.5 198812771.
6        6 E060000… Halton         NA       354246 382146 -2.69   53.3  82890887.
7        7 E060000… Warrington     NA       362744 388456 -2.56   53.4 178742993.
8        8 E060000… Blackburn wit… NA       369490 422806 -2.46   53.7 139386373.
9        9 E060000… Blackpool      NA       332819 436635 -3.02   53.8  33544769.
10       10 E060000… Kingston upon… NA       511894 431650 -0.304  53.8  70878198.
  # … with 369 more rows,and 2 more variables: Shape__Len <dbl>,#   geometry <MULTIpolyGON [m]>
> shp <- readOGR('Local_Authority_districts_(December_2020)_UK_BUC.shp')
  OGR data source with driver: ESRI Shapefile

Source:/Users/Map/Local_Authority_districts_(December_2020)_UK_BUC/Local_Authori 
ty_districts_(December_2020)_UK_BUC.shp",layer: 
"Local_Authority_districts_(December_2020)_UK_BUC"
 with 379 features
 It has 10 fields
 Warning message:
 In OGRSpatialRef(dsn,layer,morphFromESRI = morphFromESRI,dumpSRS = dumpSRS,:
     discarded datum OSGB_1936 in Proj4 deFinition: +proj=tmerc +lat_0=49 +lon_0=-2 
  +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs
 > shp <- fortify(shp,region = 'LAD20CD')
 SpP is invalid
 Warning messages:
 1: In RGEOSUnaryPredFunc(spgeom,byid,"rgeos_isvalid") :
    Ring Self-intersection at or near point 486959.84970000002 134505.30050000001
 2: In rgeos::gUnaryUnion(spgeom = SpP,id = IDs) :
  Invalid objects found; consider using set_RGEOS_CheckValidity(2L)
> 
> setwd('/Users/jtbbl/Library/Mobile Documents/com~apple~CloudDocs/Map')
> Entry_Rate <- read_csv('Choropleth map ready2go.csv')

── Column specification ──────────────────────────────────────────────────────────
cols(
  order = col_double(),LAD20NM = col_character(),id = col_character(),`Entry Rate` = col_double()
   )

> 
> shp <- merge(shp,by.y = 'order')
> shp <- arrange(shp,order)
> 
> 
> p <- ggplot(data = shp,fill = id)) + 
+   geom_polygon(colour = 'blue') + coord_equal() + theme_void() +
+   ggtitle('Percentage of births where one of both parents were born outside the UK',+           subtitle = 'England and Wales,2018')here

不知道为什么 fortify 不起作用,任何其他建议都会非常有帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)