多米尼加共和国的R等值线图

时间:2015-06-20 11:37:22

标签: r choropleth

我想制作多米尼加共和国的等值线图。至于边界,我所需要的只是该国家内的州。

我尝试了以下内容:

map <- GetMap('Dominican Republic' , zoom = 8 , ) 
PlotOnStaticMap(map) 
PlotPolysOnStaticMap(map , polys)

2 个答案:

答案 0 :(得分:4)

您可以在没有RGoogleMaps的情况下执行此操作。有shapefile&amp;加州大学戴维斯分校的RData文件适用于地球上的每个地区。以下带注释的代码:

  • 下载DR
  • 的管理级1区域的RData SpatialPolygonsDataFrame
  • 抓住我的theme_map
  • 制作样本数据集
  • 绘制一个具有适当DR地图投影的等值区

您可以使用类似方式使用其他实际shapefile:

library(sp)
library(ggplot2)

# theme_map
devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")

# the DR "shapefile"
download.file("http://biogeo.ucdavis.edu/data/gadm2/R/DOM_adm1.RData", "DOM_adm1.RData")
load("DOM_adm1.RData")

# for plotting with ggplot2
dr_map <- fortify(gadm)


# build some sample data, note that we'll be using
# the polygon "id" for plotting so you'll need that in the
# data frame
set.seed(1492)
choro <- data.frame(id=gadm@data$ID_1,
                    place=gadm@data$NAME_1,
                    value=sample(100, nrow(gadm@data)))

# you can look at the data with this (it won't be shown here)
head(choro)

# plot
gg <- ggplot()

# base map layer
gg <- gg + geom_map(data=dr_map, map=dr_map,
                    aes(x=long, y=lat, map_id=id),
                    fill="white", color="#7f7f7f", size=0.25)

# DR choro layer
gg <- gg + geom_map(data=choro, map=dr_map, 
                    aes(fill=value, map_id=id), 
                    color="#7f7f7f", size=0.25)

# lambert is a gd proj for DR
gg <- gg + coord_map("lambert", lat0=17.5, lat1=20)

# clean up map chart junk
gg <- gg + theme_map()

# move legend
gg <- gg + theme(legend.position="right")

# plot it
gg

enter image description here

答案 1 :(得分:0)

我创建了一个R包choroplethrAdmin1,旨在简化R中世界上每个国家的行政级别1等值区域地图的创建。

ordered_table = {}

function ordered_table.insert(t, k, v)
  if not rawget(t._values, k) then -- new key 
    t._keys[#t._keys + 1] = k
  end
  if v == nil then -- delete key too.
    ordered_table.remove(t, k)
  else -- update/store value
    t._values[k] = v 
  end
end

local function find(t, value)
  for i,v in ipairs(t) do
    if v == value then
      return i
    end
  end
end  

function ordered_table.remove(t, k)
  local v = t._values[k]
  if v ~= nil then
    table.remove(t._keys, find(t._keys, k))
    t._values[k] = nil
  end
  return v
end

function ordered_table.index(t, k)
    return rawget(t._values, k)
end

function ordered_table.pairs(t)
  local i = 0
  return function()
    i = i + 1
    local key = t._keys[i]
    if key ~= nil then
      return key, t._values[key]
    end
  end
end

function ordered_table.new(init)
  init = init or {}
  local t = {_keys={}, _values={}}
  local n = #init
  if n % 2 ~= 0 then
    error"in ordered_table initialization: key is missing value"
  end
  for i=1,n/2 do
    local k = init[i * 2 - 1]
    local v = init[i * 2]
    if t._values[k] ~= nil then
      error("duplicate key:"..k)
    end
    t._keys[#t._keys + 1]  = k
    t._values[k] = v
  end
  return setmetatable(t,
    {__newindex=ordered_table.insert,
    __len=function(t) return #t._keys end,
    __pairs=ordered_table.pairs,
    __index=t._values
    })
end

--- Example Usage:
local t = ordered_table.new{
  "hello", 1,  -- key, value pairs
  2, 2,
  50, 3,
  "bye", 4,
  200, 5
}

print(#t)
print("hello is", t.hello)
print()
for k, v in pairs(t) do  --- Lua 5.2 __pairs metamethod
  print(k, v)
end
t.bye = nil -- delete that
t[2] = 7 -- use integer keys
print(#t) 

enter image description here

要创建一个等值区,您需要有一个包含两列的data.frame:一个命名区域,一个命名值。区域必须是地图的名称。

library(choroplethr)
library(choroplethrAdmin1)

# ?admin1_map just draws an outline
admin1_map("dominican republic")

enter image description here