R:shapefile上的渐变图

use*_*erk 5 plot gradient r colors

我目前有一个英国的shapefile,并绘制了英国不同地区的物种种群.到目前为止,我刚刚绘制了3个物种种群,并将它们染成红色=高,橙色= med,绿色=低.但我想做的是拥有一个渐变图而不是仅受3种颜色的限制.到目前为止,我有一个名为Count的表,其中区域为列名,然后是下面每个区域的物种数.我的最低计数为0,最高计数为2500左右,计数区域与我的shapefile中的区域匹配.我有一个函数可以根据你自己输入的级别来确定什么是高,中,低

    High<-colnames(Count)[which(Count>'input value here')]
Run Code Online (Sandbox Code Playgroud)

然后将这些绘制到shapefile上,如下所示:

    plot(ukmap[(ukmap$Region %in% High),],col='red',add=T)
Run Code Online (Sandbox Code Playgroud)

不幸的是我无法安装任何软件包,我正在考虑使用colorRamp,但我不确定该怎么办?

编辑:我的数据看起来像这样

      Wales   Midlands North Scotland South East South West
 1        551       32      124        1         49         28
 3         23       99      291      152        164        107
 4          1        7       17       11         21         14
 7        192       32       12        0          1          9
 9         98       97        5        1         21          0
Run Code Online (Sandbox Code Playgroud)

并且第一列只是一个代表物种的数字,目前我有一个函数可以将计数绘制到英国shapefile上,但是基于high,med和low的边界.上面的数据没有附加到我的shapefile.然后我遍历我的数据集的每一行(种类)并为每一行(种类)绘制一个新的地图.

Slo*_*ner 6

好吧,我会咬人的.我不会使用基础R因为plot我太难理解,所以相反我们会使用ggplot2.

# UK shapefile found via http://www.gadm.org/download
uk.url <- "http://www.filefactory.com/file/s3dz3jt3vr/n/GBR_adm_zip"

# replace following with your working directory - no trailing slash
work.dir <- "C:/Temp/r.temp/gb_map"

# the full file path for storing file
file.loc <- paste0(work.dir, "/uk.zip")

download.file (uk.url, destfile = file.loc, mode = "wb")
unzip(file.loc, exdir = work.dir)

# open the shapefile
require(rgdal)
require(ggplot2)
uk <- readOGR(work.dir, layer = "GBR_adm2")

# use the NAME_2 field (representing counties) to create data frame
uk.map <- fortify(uk, region = "NAME_2")

# create fake count data...
uk.map$count <- round(runif(nrow(uk.map), 0, 2500), 0)

# quick visual check
ggplot(uk.map, aes(x = long, y = lat, group = group, fill = count)) +
    geom_polygon(colour = "black", size = 0.5, aes(group = group)) +
    theme()
Run Code Online (Sandbox Code Playgroud)

这会生成下面的输出,这可能与您需要的类似.

截图

请注意,在这种情况下我们没有明确指定渐变 - 我们只是将其保留ggplot.如果您希望指定这些细节,可能会更多参与.如果沿着那条路走下去,你应该创建另一列,uk.map使用该cut函数将每个计数分配到(例如)10个箱中的一个.该uk.map数据帧是这样的:

> str(uk.map)
'data.frame':   427339 obs. of  8 variables:
 $ long : num  -2.05 -2.05 -2.05 -2.05 -2.05 ...
 $ lat  : num  57.2 57.2 57.2 57.2 57.2 ...
 $ order: int  1 2 3 4 5 6 7 8 9 10 ...
 $ hole : logi  FALSE FALSE FALSE FALSE FALSE FALSE ...
 $ piece: Factor w/ 234 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ group: Factor w/ 1136 levels "Aberdeen.1","Aberdeenshire.1",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ id   : chr  "Aberdeen" "Aberdeen" "Aberdeen" "Aberdeen" ...
 $ count: num  1549 1375 433 427 1282 ...
> 
Run Code Online (Sandbox Code Playgroud)


Slo*_*ner 4

好的,这里有一个不使用的替代解决方案ggplot(我将留下该ggplot解决方案供参考)。此代码很简单,但它应该足以为您提供一些关于如何使其适应您自己的数据的想法。

# UK shapefile found via http://www.gadm.org/download
uk.url <- "http://www.filefactory.com/file/s3dz3jt3vr/n/GBR_adm_zip"

# replace following with your working directory - no trailing slash
work.dir <- "C:/Temp/r.temp/gb_map"

# the full file path for storing file
file.loc <- paste0(work.dir, "/uk.zip")

download.file (uk.url, destfile = file.loc, mode = "wb")
unzip(file.loc, exdir = work.dir)

# open the shapefile
require(rgdal)
uk <- readOGR(work.dir, layer = "GBR_adm2")

# make some fake data to plot
uk@data$count <- round(runif(nrow(uk@data), 0, 2500), 0)
uk@data$count <- as.numeric(uk@data$count)

# and plot it
plot(uk, col = gray(uk@data$count/2500))
Run Code Online (Sandbox Code Playgroud)

代码的结果如下图所示。

截屏

根据包含图例的请求进行编辑,我对代码进行了一些调整,但老实说,我对基础 R 的legend功能了解不够好,无法获得生产质量的东西,而且我不想进一步研究它。(顺便提一下,向这个问题提出建议。)查看代码下方的图表明我们需要重新排序图例颜色等,但我将把它留给原始海报作为练习或作为另一个问题发布。

# UK shapefile found via http://www.gadm.org/download
uk.url <- "http://www.filefactory.com/file/s3dz3jt3vr/n/GBR_adm_zip"

# replace following with your working directory - no trailing slash
work.dir <- "C:/Temp/r.temp/gb_map"

# the full file path for storing file
file.loc <- paste0(work.dir, "/uk.zip")

download.file (uk.url, destfile = file.loc, mode = "wb")
unzip(file.loc, exdir = work.dir)

# open the shapefile
require(rgdal)
uk <- readOGR(work.dir, layer = "GBR_adm2")

# make some fake data to plot
uk@data$count <- as.numeric(round(runif(nrow(uk@data), 0, 2500), 0))
uk@data$bin <- cut(uk@data$count, seq(0, 2500, by = 250), 
      include.lowest = TRUE, dig.lab = 4)

# labels for the legend
lev = levels(uk@data$bin)
lev2 <- gsub("\\,", " to ", lev)
lev3 <- gsub("\\]$", "", lev2)
lev4 <- gsub("\\(|\\)", " ", lev3)
lev5 <- gsub("^\\[", " ", lev4)
my.levels <- lev5

# Create a function to generate a continuous color palette
rbPal <- colorRampPalette(c('red','blue'))
uk@data$Col <- rbPal(10)[as.numeric(cut(uk@data$count, seq(0, 2500, by = 250)))]

# Plot
plot(uk, col = uk@data$Col)
legend("topleft", fill = uk@data$Col, legend = my.levels, col = uk@data$Col)
Run Code Online (Sandbox Code Playgroud)

截屏