【问题标题】:r spatial mapping空间映射
【发布时间】:2012-08-14 03:47:45
【问题描述】:

我正在尝试在 R 中制作一些漂亮的空间地图。我正在尝试了解如何上传数据以防您想查看它们,但我还没有弄清楚(对此我很抱歉,但是成为新用户意味着寻找所有这些东西。

我的情况如何。我有一个整个美国的 shapefile,我只需要一些州,我可以在绘制它时选择我的网格(正如你从绘图部分的代码中看到的那样)。

我还有一些产量数据(点),它们有纬度、经度和产量。我有 4 个不同的产量数据,称为(“所有站”、“0.5”、“1.0”和“2.0”)。

我正在尝试在空间地图上绘制这 4 个产量数据,以获得 4 个不同的空间地图。完成了。

我已经通过在这里和那里阅读 stackoverflow 来做到这一点,我使用了一些零碎的东西来做到这一点,尽管在我惊讶于我能以多快的速度前进之前我从未这样做过(感谢 StackOverflow 上的人们!!!)。

谁能帮我理解我的代码是否正确?

另外,我怎样才能让 4 张地图的图例更规则?例如。从 4000 到 9000,4 张地图中的每一张都有 500 个间隔。我所做的是创建一个单独的文本文件(“Yield for Legend.txt”),我用它来生成地图和图例中的色标。那是对的吗?

再次,欢迎您的批评!

谢谢, 大卫

    rm(list=ls())
    setwd("C:\\Users\\.....\\Shape File")
    library(spatstat)
    library(rgdal)
    library(shapefiles)
    library(maptools)
    library(RColorBrewer)
    library(classInt)


    # read in shapefiles

    counties.rg <- readOGR("C:\\Users\\......\\Shape File", "tl_2011_us_county")


    Yields <- read.table("Yield.txt", skip=1, header = F)

    Yield.g <- as.ppp(Yields, owin( c(-89, -76), c(25, 37)))

    ## Reading Data for Legend and colouring breaks

     Y.LE <- read.table("Yield for Legend.txt", header=F)
     Y.L.I <- classIntervals(Y.LE$V1, n=9, style = "quantile")
     Y.L.I <- Y.L.I$brks

    #select color palette and the number colors (levels of income) to represent on the map
    #colors <- brewer.pal(9, "RdYlGn")
    colors <- brewer.pal(9, "Greys")

    ################################################
    ### Generating MAPS ############################
    ################################################

    #set breaks for the 9 colors
      #par(mfrow=c(2,2))

     pdf("13 August Spatial Maps.pdf") 
    # All Points 

    brks.all <-classIntervals(Yields$V3, n=9, style = "quantile")
    brks.all <- brks.all$brks

    plot(counties.rg, axes=TRUE, border="grey", xlim = c(-82, -80),
        ylim = c(24, 37))
    points(Yield.g, cex= 1.1, bg=colors[findInterval(Yields$V3, Y.L.I,all.inside=TRUE)], pch=21)

    #add a title
    title(paste ("Rainfed Yield (kg/ha)All Stations"))

    #add a legend
    legend("bottomright", legend=leglabs(round(Y.L.I)), fill=colors, bty="n", cex=0.7 ) #,x.intersp = .5, y.intersp = .5)



    # 0.5 Grid  
    brks.05 <-classIntervals(Yields$V4, n=9, style = "quantile")
    brks.05 <- brks.05$brks

    plot(counties.rg, axes=TRUE, border="grey", xlim = c(-82, -80),
        ylim = c(24, 37))
    points(Yield.g, cex= 1.1, bg=colors[findInterval(Yields$V4, Y.L.I,all.inside=TRUE)], pch=21)
    #abline(v=GF$V1, col="grey40")
    #abline(h=GF$V2, col="grey10", lty="dotted")
    #backup
    #points(Yield.g, cex= Yields$V4/9000, col=colors[findInterval(Yields$V4, brks.05,all.inside=TRUE)], pch=19)

    #add a title
    title(paste ("Rainfed Yield (kg/ha)0.5"))

    #add a legend
    legend("bottomright", legend=leglabs(round(Y.L.I)), fill=colors, bty="n", cex=0.7 ) #,x.intersp = .5, y.intersp = .5)


    # 1.0 Grid
    brks.1 <-classIntervals(Yields$V5, n=9, style = "quantile")
    brks.1 <- brks.1$brks

    plot(counties.rg, axes=TRUE, border="grey", xlim = c(-82, -80),
        ylim = c(24, 37))
    points(Yield.g, cex= 1.1, bg=colors[findInterval(Yields$V5, Y.L.I,all.inside=TRUE)], pch=21)
    #abline(v=GO$V1, col="grey40")
    #abline(h=GO$V2, col="grey10", lty="dotted")
    #add a title
    title(paste ("Rainfed Yield (kg/ha)1.0"))

    #add a legend
    legend("bottomright", legend=leglabs(round(Y.L.I)), fill=colors, bty="n", cex=0.7 ) #,x.intersp = .5, y.intersp = .5)


    # 2.0 Grid

    brks.2 <-classIntervals(Yields$V6, n=9, style = "quantile")
    brks.2 <- brks.2$brks

    plot(counties.rg, axes=TRUE, border="grey", xlim = c(-82, -80),
        ylim = c(24, 37))
    points(Yield.g, cex= 1.1, bg=colors[findInterval(Yields$V6, Y.L.I,all.inside=TRUE)], pch=21)
    #abline(v=GG$V1, col="grey40")
    #abline(h=GG$V2, col="grey10", lty="dotted") 
    #add a title
    title(paste ("Rainfed Yield (kg/ha)2.0"))

    #add a legend
    legend("bottomright", legend=leglabs(round(Y.L.I)), fill=colors, bty="n", cex=0.7 ) #,x.intersp = .5, y.intersp = .5)
dev.off()

【问题讨论】:

  • 您希望我们检查无法重现的非常长的代码(因为我们无权访问您的数据)。您既没有指定问题所在。甚至有问题吗?此外,你没有给我们一个关于输出应该是什么的模糊描述。请更新您的问题。
  • 欢迎来到 StackOverflow。也许如果您创建了一个 reproducible example 来展示您的问题/问题,人们会发现它更容易回答。
  • 嗨!非常感谢您指出这个缺点。我将附上一些数据并尝试使问题更清楚一些。问题是我对 StackOverflow 很陌生,但我已经使用了很多,并帮助我快速学习 R。由于这段代码是来自各地的点点滴滴,我想知道它是否有意义。让我弄清楚如何附加一些数据,我很乐意将它们放在这里。

标签: r maps spatial


【解决方案1】:

针对您关于设置中断和匹配图例的具体问题,有一个相当简单的解决方法。

您使用带有 style="quantile" 的 classInterval 函数来定义您的休息时间。如果您希望地图显示“从 4000 到 9000,4 个地图中的每一个都有 500 个间隔”,为什么不使用 style="fixed"

brks.all <-classIntervals(Yields$V3, n=10, style = "fixed",
  fixedBreaks=seq(from=4000, to=9000, by=500)
brks.all <- brks.all$brks 

请注意,按我的计算,从 4k 到 9k 到 500 会创建 10 个间隔,而 9 个颜色渐变通常不会构成一张漂亮的地图。

或者,classInt 中的 dataPrecision 变量也可以帮助您获得更接近您想要的标签和中断,但仍基于分位数(如果在地图上不统一)

【讨论】:

  • 非常感谢!!这是一个很大的帮助。当我键入此代码时,会出现以下错误消息:“警告消息:在 classIntervals(Yields$V3, n = 12, style = "fixed", fixedBreaks = seq(from = 4000, : variable range greater than fixedBreaks" 但是,我继续前进,当我输入“brks.all”时,我可以看到新的间隔,我也可以在地图上显示它!!我认为这可能是要走的路。
  • 我明白你关于有 10 个间隔和 9 个颜色渐变的观点......我可能只有 9 个间隔,所以我会有一个匹配。我正在四处寻找R中是否有其他着色方式。
  • 请注意,您的错误消息告诉您 V3 中有小于 4000 或大于 9000 的值。因此,这些值不会分配给地图中的任何类(颜色)。至于颜色,看起来你已经安装了 RColorBrewer。访问他们的网站以了解有关颜色和映射的更多信息。这是一种真正的艺术形式。
  • 谢谢!我昨天晚上做的,这个网站真的很棒!感谢您的建议!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-20
相关资源
最近更新 更多