【发布时间】: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。由于这段代码是来自各地的点点滴滴,我想知道它是否有意义。让我弄清楚如何附加一些数据,我很乐意将它们放在这里。