我试图创建一个显示美国50个州专题地图,但我有麻烦迁往阿拉斯加和夏威夷以可靠的方式。 我有一对夫妇的想法,但他们没有工作。 现在我将演示他们。
首先,我们需要导入的数据; 在使用数据maps
包是不够的,因为它不包括夏威夷和阿拉斯加。
setwd(tempdir())
download.file("https://dl.dropbox.com/s/wl0z5rpygtowqbf/states_21basic.zip?dl=1",
"usmapdata.zip",
method = "curl")
# This is a mirror of http://www.arcgis.com/home/item.html?
# id=f7f805eb65eb4ab787a0a3e1116ca7e5
unzip("usmapdata.zip")
require(rgdal)
all_states <- readOGR("states_21basic/", "states")
require(ggplot2); require(maptools); require(rgeos); require(mapproj);
all_states <- fortify(all_states, region = "STATE_NAME")
现在我们定义一些情节美学:
p <- ggplot() + geom_polygon(
aes(x=long, y=lat, group = group, fill = as.numeric(as.factor(id))),
colour="white", size = 0.25
) + coord_map(projection="azequalarea") +
scale_fill_gradient(limits = c(1,50))
现在我们删除所有的背景等,使他们当我们重叠的不连续的状态不冲突:
p <- p + theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
panel.background=element_blank(),
panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
plot.background=element_blank())
使用视口
我的第一个想法是使用视口:
AK <- p %+% subset(all_states, id == "Alaska") + theme(legend.position = "none")
HI <- p %+% subset(all_states, id == "Hawaii") + theme(legend.position = "none")
contiguous <- p %+% subset(all_states, id != "Alaska" & id != "Hawaii")
grid.newpage()
vp <- viewport(width = 1, height = 1)
print(contiguous, vp = vp)
subvp1 <- viewport(width = 0.25, height = 0.25, x = 0.18, y = 0.33)
print(AK, vp = subvp1)
subvp2 <- viewport(width = 0.12, height = 0.12, x = 0.32, y = 0.27)
print(HI, vp = subvp2)
这看起来不错,但因为它是在图中的微小变化非常敏感,例如调整大小或图例的大小和形状的变化是不能令人满意的。
手动移动阿拉斯加和夏威夷
all_states_AKHImoved <- within(all_states, {
lat[id == "Alaska"] <- lat[id == "Alaska"] - 45
long[id == "Alaska"] <- long[id == "Alaska"] + 40
lat[id == "Hawaii"] <- lat[id == "Hawaii"] + 0
long[id == "Hawaii"] <- long[id == "Hawaii"] + 70
})
p %+% all_states_AKHImoved
这是不令人满意的,因为阿拉斯加通常是不按比例在大多数美国的地图,所以它看起来很大。 也搬迁阿拉斯加和夏威夷改变由地图投影引入的失真。
题
没有人有任何更好的方法?