我一直试图从英国的邮政编码中创建一个会员地点的 Map ,作为学习R的项目 . 我已经取得了几乎我想要的结果,但它证明非常令人沮丧的故障排序 . 这张图片是我目前的最佳努力:
my current best effort

I still want to change:

  • 摆脱了无关的传说(“0.16”,“0.5”方格),它们的大小从arg到geom_point . 如果我删除size = 0.16 arg指南/图例消失,但geom size也会恢复为默认值 . 这也适用于“黑色”指南 - 显然来自一种颜色 - 但为什么呢?

  • 正确剪辑stat_density2d多边形,这些多边形在剪切时表现出不良行为(请参见顶部附近的右下图)

  • 可以控制包含县界的geom_path的线宽:它当前太厚了(想要显示大约1/2厚度)但是我通过包含'size'值来实现的只是让线条变得愚蠢 - 这么厚,以至于它们遮住了整个 Map .

  • R代码使用revgeocode()来查找最接近中心点的地名,但我不知道如何在 Map 上包含注释 . 我想将它包含在北海上方的文本框中(英国 Map 的右上角),可能有一条线/箭头指向该点 . 一个更简单的选项可能只是英国 Map 下面的一些文字,在x轴下面...但我不知道该怎么做 . 在这种情况下,geom_rect / geom_text似乎充满了兴趣 .

  • 最后,我想将 Map 导出为高分辨率图像,但是当我这样做时,一切都会再次变化,请参阅:

this high-res / low-res screenshot

它显示左侧的高分辨率(~1700x1800px)图像和右侧的Rstudio版本(~660x720px) . Map 的比例已经改变,中心点的geom_text和geom_point现在很小 . 如果两个 Map 行之间的差距总是很小(而不是在高分辨率下很小),我会很高兴 .

Code

基础知识:读取成员邮政编码列表,使用邮政编码<> OSGB位置的mySociety表连接,使用spTransform将位置转换为Lat / long,计算binhex和密度图层,使用ggmap绘图 .

所有这些的代码有点冗长,所以我把它上传为Gist:

https://gist.github.com/rivimey/ee4ab39a6940c0092c35

但是为了参考,映射代码的'guts'在这里:

# Get a stylised base map for the whole-of-uk maps.
map.bbox = c(left = -6.5, bottom = 49.5, right = 2, top = 58)
basemap.uk <- get_stamenmap(bb = map.bbox, zoom=calc_zoom(map.bbox), maptype="watercolor")

# Calculate the density plot - a continuous approximation.
smap.den <- stat_density2d(aes(x = lat, y = lon, fill = ..level.., alpha = ..level..),
               data = membs.wgs84.df, geom = "polygon", 
               breaks=2/(1.5^seq(0,12,by=1)), na.rm = TRUE)

# Create a point on the map representing the centroid, and label it.
cmap.p <- geom_point(aes(x = clat, y = clon), show_guide = FALSE, data = centroid.df, alpha = 1)
cmap.t1 <- geom_text(aes(x = clat, y = clon+0.22, label = "Centre", size=0.16), data = centroid.df)
cmap.t2 <- geom_text(aes(x = clat, y = clon+0.1, label = "Centre", size=0.25), data = centroid.df)

# Create an alternative presentation, as binned hexagons, which is more true to the data.
smap.bin <- geom_hex(aes(x = lat, y = lon),
             data = membs.wgs84.df, binwidth = c(0.15, 0.1), alpha = 0.7, na.rm = TRUE)

# Create a path for the county and country boundaries, to help identify map regions.
bounds <- geom_path(aes(x = long, y = lat, group = group, colour = "black"), show_guide = FALSE,
               data = boundaries.subset, na.rm = TRUE)

# Create the first two actual maps: a whole-uk binned map, and a whole-uk density map.
map.bin <- ggmap(basemap.uk) + smap.bin + grad + cmap.p + cmap.t1
map.den <- ggmap(basemap.uk) + smap.den + alpha + cmap.p + cmap.t1

# Create a zoomed-in map for the south-east, to show greater detail. I would like to use this
# bbox but google maps don't respect it :(
map.lon.bbox = c(left = -1, bottom = 51, right = 1, top = 52)
# Get a google terrain map for the south-east, bbox roughly (-1.7,1.7, 50.1, 53)
basemap.lon <- get_map(location = c(0,51.8), zoom = 8, maptype="terrain", color = "bw")
# Create a new hexbin with more detail than earlier.
smap.lon.bin <- geom_hex(aes(x = lat, y = lon),
             data = membs.wgs84.df, bins=26, alpha = 0.7, na.rm = TRUE)

# Noe create the last two maps: binned and density maps for London and the SE.
lonmap.bin <- ggmap(basemap.lon) + bounds + smap.lon.bin + grad + cmap.p + cmap.t2
lonmap.den <- ggmap(basemap.lon) + bounds + smap.den + alpha + cmap.p + cmap.t2

# Arrange the maps in 2x2 grid, and tell the grid code to let the first row be taller than the second.
multiplot(map.bin, lonmap.bin, map.den, lonmap.den, heights = unit( c(10,7), "null"), cols=2 )