【发布时间】:2013-12-04 14:49:10
【问题描述】:
SpatialPoly 数据:SpatialData
产量数据:Yield Data
代码:
## Loading packages
library(rgdal)
library(plyr)
library(maps)
library(maptools)
library(mapdata)
library(ggplot2)
library(RColorBrewer)
library(foreign)
library(sp)
## Loading shapefiles and .csv files
#Morocco <- readOGR(dsn=".", layer="Morocco_adm0")
MoroccoReg <- readOGR(dsn=".", layer="Morocco_adm1")
MoroccoYield <- read.csv(file = "F:/Purdue University/RA_Position/PhD_ResearchandDissert/PhD_Draft/Country-CGE/RMaps_Morocco/Morocco_Yield.csv", header=TRUE, sep=",", na.string="NA", dec=".", strip.white=TRUE)
## Reorder the data in the shapefile based on the category variable "ID_1" and change to dataframe
MoroccoReg <- MoroccoReg[order(MoroccoReg$ID_1), ]
MoroccoReg.df <- fortify(MoroccoReg)
## Add the yield impacts column to shapefile from the .csv file by "ID_1"
## Note that in the .csv file, I just added the column "ID_1" to match it with the shapefile
MoroccoReg.df <- cbind(MoroccoReg.df,MoroccoYield,by = 'ID_1')
## Check the structure and contents of shapefile
attributes(MoroccoReg.df)
## Define new theme for map
## I have found this function on the website
theme_map <- function (base_size = 12, base_family = "") {
theme_gray(base_size = base_size, base_family = base_family) %+replace%
theme(
axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.ticks.length=unit(0.3, "lines"),
axis.ticks.margin=unit(0.5, "lines"),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
legend.background=element_rect(fill="white", colour=NA),
legend.key=element_rect(colour="white"),
legend.key.size=unit(1.5, "lines"),
legend.position="right",
legend.text=element_text(size=rel(1.2)),
legend.title=element_text(size=rel(1.4), face="bold", hjust=0),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.margin=unit(0, "lines"),
plot.background=element_blank(),
plot.margin=unit(c(1, 1, 0.5, 0.5), "lines"),
plot.title=element_text(size=rel(1.8), face="bold", hjust=0.5),
strip.background=element_rect(fill="grey90", colour="grey50"),
strip.text.x=element_text(size=rel(0.8)),
strip.text.y=element_text(size=rel(0.8), angle=-90)
)
}
## Plotting
MoroccoRegMap1 <- ggplot(data = MoroccoReg.df, aes(long, lat, group = group))
MoroccoRegMap1 <- MoroccoRegMap1 + geom_polygon(aes(fill = A2Med_noCO2))
MoroccoRegMap1 <- MoroccoRegMap1 + geom_path(colour = 'gray', linestyle = 2)
#MoroccoRegMap <- MoroccoRegMap + scale_fill_gradient(low = "#CC0000",high = "#006600")
MoroccoRegMap1 <- MoroccoRegMap1 + scale_fill_gradient2(name = "%Change in yield",low = "#CC0000",mid = "#FFFFFF",high = "#006600")
MoroccoRegMap1 <- MoroccoRegMap1 + labs(title="SRES_A2, noCO2 Effect")
MoroccoRegMap1 <- MoroccoRegMap1 + coord_equal() + theme_map()
MoroccoRegMap1
结果:
问题:
在产量数据中,我有一列描述了与“ID_1”列中的每个条目对应的标签。我想要实现的是两件事:
1) 绘制地图并将“ID_1”变量条目作为标签添加到地图上,从而识别每个区域;
2) 生成第二个图例,除了捕获数据中的值的图例,以及“ID_1”中的条目及其在数据框中“标签”列中的相应描述。
我希望我清楚地表达了我的问题。
谢谢。
【问题讨论】:
-
所以我下载了您的多边形数据,但无法在 R 或 QGIS 2.0.1 中读取它。你确定文件没有损坏??
-
@jlhoward:我很确定它正在工作。我仔细检查了一下,我能够在 R 中加载文件并执行所有其他操作,将其转换为数据框以便将其与产量数据合并。我能够重现与上面相同的地图。
-
@smailov63 - 我确定您的原始文件没问题,但您是否重新下载了上传的文件?有时它们会在上传过程中损坏。
-
@jlhoward - 我重新上传了多边形数据文件以确保。说实话,我没有使用最初上传的文件运行代码,因为我已经在项目目录中。所以也许你是对的,当我上传它时出了点问题。让我知道重新上传是否解决了问题。谢谢
-
@smailov63 - 所以上传的问题是:“shapefile”不是一个文件,而是一组文件,包括
.shp, .prj, .dbf, .csv, .shx。readOGR(...)寻找所有这些。我找到了一个摩洛哥行政区 shapefile here,但是当我使用它时,您的代码无法运行。你能上传所有Morocco_adm1.*文件吗?