【问题标题】:R: Plotting shapefilesR:绘制 shapefile
【发布时间】:2018-04-24 16:43:50
【问题描述】:

我对 R 非常陌生,目前正在尝试从 shapefile 创建地图。但是,我一直坚持如何根据数据集中的给定参数用颜色填充地图......我将不胜感激!谢谢!

我的代码如下:

#Read in data and set variables
parameter_results<- readRDS("param_results_2014.RDS")
NJ<- readOGR(dsn="V:/lum/WM&S/BEAR (Bureau of Environmental Analysis and Restoration)/Envpln/Hourly Employees/JohnDoe/Rwork/2014IR/Maps/shapefiles",layer="2014_NJ_Integrated_Report_AU")
#join common field
NJ_merge<- merge(NJ,parameter_results,by.x="AU_NUM",by.y="Waterbody")
colors <- c("#91D79E","#FFFF73","#FF7F7F","#FF7F7F")


#Following lines create plot with scale and arrow
plotexpression<-plot(NJ_merge, xlim=c(200000.732,905000.646), ylim=c(-5812.321,900000.543),main = "Figure 2.4A: Assessment Results for Trout Aquatic\nLife Use, Spatial Extent",col=NJ_merge$watsup)

prettymap(plotexpression,oma=c(3,3,4,3),drawbox = TRUE,scale.plotunit="mi",drawscale = TRUE,scale.pos = "bottomright",drawarrow = TRUE,arrow.scale = .5,box.lwd = 1, arrow.cols = c("black","black"),arrow.text.col = "black")


legend(640000, 400000, legend=c("Fully Supported", "Insufficient Data", "TMDL Waters (Not Supported)", "Not Supporting","Not Applicable"), 
       fill=c(colors), bty="n", title = "Aquatic Life - Trout\nDesignated Use 2014\nAssessment",text.font = 2, cex=0.6,title.adj=0.2,title.col=1)

数据集参数结果:

# A tibble: 958 x 89
   WMA   Waterbody  Name    `Biological (Cau~ `Biological Trou~ DO    `DO Trout` Temperature
   <chr> <chr>      <chr>   <chr>             <chr>             <chr> <chr>      <chr>      
 1 15    020403020~ Abseco~ Attaining         Not Applicable    Atta~ Not Appli~ Attaining  
 2 15    020403020~ Abseco~ Insufficient Inf~ Not Applicable    Non ~ Not Appli~ Attaining  
 3 15    020403020~ Abseco~ Attaining         Not Applicable    Insu~ Not Appli~ Insufficie~
 4 15    020403020~ Abseco~ Attaining         Not Applicable    Atta~ Not Appli~ Attaining  
 5 14    020403011~ Albert~ Non Attaining     Not Applicable    Atta~ Not Appli~ Attaining  
 6 11    020401052~ Alexau~ Attaining         Attaining         Insu~ Attaining  Insufficie~
 7 11    020401052~ Alexau~ Attaining         Attaining         Insu~ Attaining  Insufficie~
 8 17    020402060~ Allowa~ Non Attaining     Not Applicable    Atta~ Not Appli~ Attaining  
 9 17    020402060~ Allowa~ Insufficient Inf~ Not Applicable    Atta~ Not Appli~ Attaining  
10 17    020402060~ Allowa~ Insufficient Inf~ Not Applicable    Insu~ Not Appli~ Insufficie~

NJ_merge 如下:

str(NJ_merge,2)
Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
  ..@ data       :'data.frame': 958 obs. of  101 variables:
  ..@ polygons   :List of 958
  ..@ plotOrder  : int [1:958] 950 844 853 421 687 329 334 721 251 321 ...
  ..@ bbox       : num [1:2, 1:2] 190378 10574 659480 919549
  .. ..- attr(*, "dimnames")=List of 2
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot

我目前得到的情节:

用图例中的颜色填充地图上的区域我缺少什么???

【问题讨论】:

  • 对我正在尝试做的事情没有帮助@G5W
  • 屏幕截图中的文字真的很难阅读。如果您将str 的输出粘贴到您的帖子中,它将为我们提供相同的信息但清晰易读
  • 这样更好吗? @卡米尔

标签: r plot shapefile


【解决方案1】:

使用sf 包非常简单。使用包含在北卡罗来纳州的 shapefile 数据,我创建了一个组变量,并使用包含的 plot.sf 方法或 ggplot2 的开发版本中包含的 geom_sf 绘制它。

library(tidyverse)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3
set.seed(100)
nc <- system.file("shape/nc.shp", package="sf") %>%
  st_read() %>%
  mutate(
    group = sample.int(5, 100, replace = TRUE),
    group = parse_factor(group, levels = 1:5)
    )
#> Reading layer `nc' from data source `C:\Users\Calum You\Documents\R\win-library\3.4\sf\shape\nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs

plot(nc[, "group"])

ggplot(nc, aes(fill = group)) +
  theme_minimal() +
  geom_sf() +
  scale_fill_brewer(type = "qual")

reprex package (v0.2.0) 于 2018 年 4 月 24 日创建。

【讨论】:

  • 不太明白这与我的数据有什么关系以及我想要完成什么? @Calum 你
  • 您没有提供任何数据,因此很难排除故障。但是您说您想根据某个组在 shapefile 的每个元素内绘制一种颜色,所以我在sf 中向您展示了如何非常简单地完成此操作。我还认为在sf 中执行此操作比直接使用rgdal 更可取,这就是该软件包的用途。
猜你喜欢
  • 1970-01-01
  • 2020-10-07
  • 2017-05-05
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 2012-03-06
相关资源
最近更新 更多