【问题标题】:R: Gradient plot on a shapefileR:形状文件上的渐变图
【发布时间】:2013-08-03 16:58:49
【问题描述】:

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

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

然后将它们绘制到 shapefile 上,如下所示:

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

很遗憾,我无法真正安装任何软件包,我正在考虑使用 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

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

【问题讨论】:

    标签: r colors plot gradient


    【解决方案1】:

    你试过 colorRampPalette 吗?

    以下是您可以尝试构建渐变调色板的方法

           gradient_color <- colorRampPalette(c("blue", "red"))
           gradient_color(10)
    

    [1]“#0000FF”“#1C00E2”“#3800C6”“#5500AA”“#71008D”“#8D0071”“#AA0055” [8]“#C60038”“#E2001C”“#FF0000”

    示例图

         plot(rep(1,10),col=gradient_color(10))
    

    【讨论】:

    • 是的,我正在考虑使用 colorRampPalette,类似于 gradient_color
    • 好吧,你还没有给我们一个可重复的问题(see here 了解详情),所以很难提供帮助。话虽如此,将 shapefile 放入问题通常并不容易(尽管您可以提供下载链接),但至少向我们展示您的数据是什么样的,然后我们可能会提供帮助。
    【解决方案2】:

    好吧,我会咬人的。我不会使用base 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()
    

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

    请注意,在这种情况下,我们没有明确指定渐变 - 我们只是将其保留为 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 ...
    > 
    

    【讨论】:

    • 感谢您的帮助,不幸的是,正如我在问题中所说,我无法安装任何软件包,但我相信这对处于类似情况的其他人有用!
    • 您既没有ggplot2 也没有maps 软件包?你有没有打过library() 电话检查?
    • 我没有 ggplot2,我有 gplots 但是我没有安装 perl。为了绘制地图,我使用的是 maptools,但是是的,我也有地图包,并且为了从 Excel 电子表格中读取我的数据,我使用的是 XLConnect。
    • 我很惊讶你无法访问ggplot2 - 现在它几乎是 R 不可或缺的一部分。从长远来看,学习和使用它是有益的;我建议你询问控制你的 R 安装的人给你权限。详情请?install.packages
    【解决方案3】:

    好的,这是一个不使用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))
    

    代码的结果如下图。

    在请求包含图例后进行编辑,我对代码进行了一些调整,但老实说,我不太了解基本 R 的 legend 功能,无法获得生产质量的东西,我不想研究它进一步。 (顺便提一下this 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)
    

    【讨论】:

    • 太棒了,谢谢!你知道如何创建与此相对应的图例吗?
    猜你喜欢
    • 1970-01-01
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多