首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R空间映射

R空间映射
EN

Stack Overflow用户
提问于 2012-08-13 21:14:46
回答 1查看 1.2K关注 0票数 1

我正在尝试在R中制作一些很好的空间地图。我正在尝试理解如何上传数据,以防你想看看它们,但我还没有弄清楚(我对此感到抱歉,但作为一个新用户意味着要寻找所有这些东西)。

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

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

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

我是通过阅读stackoverflow上的文章来做到这一点的,我使用了一些零碎的东西来做到这一点,尽管我以前从未这样做过,我很惊讶自己能以如此之快的速度前进(在StackOverflow上感谢你们!)。

有人能帮我了解一下我的代码是否正确吗?

另外,我怎样才能使4个地图的图例成为正规化的比例?例如,从4000到9000,对于4个映射中的每一个具有500个间隔。我所做的是创建一个单独的文本文件(“text Legend.txt"),我使用它来生成地图和图例中的色标。对吗?

再次声明,你的批评者是最受欢迎的!

谢谢你,大卫

代码语言:javascript
复制
    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()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-14 03:47:46

对于您关于设置分隔符和让图例匹配的特定问题,有一个相当简单的解决方案。

您可以使用带有style=“分位数”的classInterval函数来定义中断。如果您希望地图显示“从4000到9000,4个地图中的每一个都有500个间隔”,为什么不使用style=“固定”

代码语言:javascript
复制
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变量还可以帮助您获得更接近所需的标签和分隔符,但仍然基于分位数(如果在地图中不是统一的)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11934994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档