2

I'm pretty new to R and I am trying to map the distribution of an ant species in Argentina using the adm1 (or state) divisions.

I have downloaded data from the GADM website and I have a csv file that I've created that contains info saying whether the species is present or absent in each adm1. Even though I don't have a gradient can I still make a choropleth? If not, what other types of maps could I use?

I've looked at several sites including Infomaps using R, How to make choropleths in R, and the Choropleth Map Challenge, which have been really helpful but they all have numeric data and I'm using a present(1) or absent(0) column. The different packages I've tried are sp(with RColorBrewer), ggplot2, rgeos, and maptools.

Here is the code I have so far:

library(sp)
library(RColorBrewer)
write.csv(atr, "atr_data.csv")
atr_data<-read.csv("atr_data.csv", header=TRUE)
  spcode country_code adm1_code newcol
1    atr          VEN     VE.AR      0
2    atr          PRY     PY.CE      0
3    atr          PAN     PA.CL      0
4    atr          PAN     PA.CL      0
5    atr          PAN     PA.PN      0
6    atr          PAN     PA.PN      0

I'm in the process of making a column with the full adm1 names instead of the codes so that it will match up with the GADM file (so I haven't written the code to merge the data yet).

#to retrieve map for Argentina ARG
con <- url("http://gadm.org/data/rda/ARG_adm1.RData")
print(load(con))
close(con)
#to generate random colors on map
col = rainbow(length(levels(gadm$NAME_1)))
spplot(gadm, "NAME_1", col.regions=col, main="ARG Regions", colorkey = FALSE, lwd=.4,col="white")  
#this piece of code is a mess  
col_no <- as.factor(as.numeric(atr_data$newcol[order],
                c(0,1)))
levels(col_no)<- c("0", "1")
gadm$col_no <- col_no
myPalette<-brewer.pal(3, "Purples")
spplot(gadm, "col_no", col=grey(.9), 
col.regions=myPalette,
main="Distribution of Atratus in Argentina")

Any help would be greatly appreciated, thanks!

4

1 回答 1

4

您提供的示例中出现了几个小问题。

首先,空间多边形数据帧的数据槽可以用gadm@data$col_no代替来访问gadm$col_no。完成填写存在/不存在表后,通过直接访问插槽或spCbindmaptools包中使用,您可以将存在/不存在数据添加到空间多边形数据框。

其次,如果您的col_no因子中只有 2 个级别,则必须将子集MyPalette设置为 2 种颜色,因为 Brewer 调色板仅适用于至少 3 个级别。

library(sp)
library(RColorBrewer)
con <- url("http://gadm.org/data/rda/ARG_adm1.RData")
print(load(con))
close(con)

# Randomly assigning presence/absence data for display purposes only
gadm@data$col_no <- as.factor(rbinom(n = 24, size = 1, prob = 0.5))

myPalette <- brewer.pal(3, "Purples")

# col.regions is limited to 2 colors below with the middle color dropped.
spplot(gadm, zcol = "col_no", colorkey = TRUE, col.regions = myPalette[-2],
    main="Distribution of Atratus in Argentina")

在此处输入图像描述

于 2013-06-03T21:32:49.713 回答