空间映射

时间:2012-08-13 13:14:46

标签: r maps spatial

我正在尝试在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()

1 个答案:

答案 0 :(得分:2)

在回答您关于设置中断和让您的图例匹配的特定查询时,有一个相当简单的解决方法。

使用带有style =“quantile”的classInterval函数来定义中断。如果您希望地图显示“从4000到9000,每个地图有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变量也可以帮助您获得更接近您想要的标签和符号,但仍然基于分位数(如果不是跨地图统一)