R:shapefile上的渐变图 [英] R: Gradient plot on a shapefile

查看:78
本文介绍了R:shapefile上的渐变图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I currently have a shapefile of the UK and have plot the population of species in different regions of the UK. So far I have just plotted 3 levels of species population and coloured them red=high, orange=med, green=low. But what I would like to do would be to have a gradient plot instead of being bounded by just 3 colours. So far I have a table called Count that has the regions as the column names and then the count of species for each region below. My lowest count being 0 and my highest being around 2500 and the regions in Count match with the regions in my shapefile. I have a function that determines what is high, med, low based on levels you input yourself

    High<-colnames(Count)[which(Count>'input value here')]

然后将它们绘制到shapefile上,例如

and then these are plotted onto the shapefile like this:

    plot(ukmap[(ukmap$Region %in% High),],col='red',add=T)

不幸的是,我无法真正安装任何软件包,我当时正在考虑使用colorRamp,但是我

Unfortunately I can't really install any packages, I was thinking of using colorRamp, but I'm not really sure what to do?

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

my data looks something like this

      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

第一列只是代表物种的数字,目前我有一个函数可以绘制计数到UK shapefile中,但基于高,中和低边界。上面的数据未附加到我的shapefile中。然后,我遍历数据集的每一行(物种),并为每一行(物种)绘制一个新地图。

and the first column is just a number that represents the species and currently I have a function that plots the count onto a UK shapefile but based on boundaries of high, med and low. The data above is not attached to my shapefile. I then loop through for each line (species) of my data set and plot a new map for each line (species).

推荐答案

确定,这是不使用 ggplot 的替代解决方案(我将把 ggplot 解决方案留作参考) 。这段代码很简单,但足以让您了解如何将其适应自己的数据。

OK, here is an alternative solution that doesn't use ggplot (I will leave the ggplot solution for reference). This code is simple but it should be enough to give you some ideas as to how you can adapt it to your own data.

# 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))

代码的结果如下ng图。

The result of the code is the following plot.

在请求添加图例后进行编辑,我稍稍调整了代码,但老实说我不理解R的传说功能足以获得某种生产质量,我不希望进一步研究它。 (顺便提一下此问题的提示以获取想法。)查看代码下方的图表明我们需要对图例颜色等进行重新排序,但我将其作为练习保留在原始海报中,或作为另一个问题发布。

EDIT following a request to include a legend, I have tweaked the code a little but in all honesty I don't understand base R's legend function well enough to get something of production quality and I have no wish to research it further. (Incidentally hat tip to this question for ideas.) A look at the plot beneath the code suggests that we need to reorder the legend colours etc but I will leave that to the original poster as an exercise or to post as another question.

# 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)

这篇关于R:shapefile上的渐变图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆