R ggmap图例/指南涉及多个图层

时间:2015-03-25 00:42:46

标签: r ggplot2 legend ggmap

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

我仍想改变:

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

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

  • 控制包含县界的geom_path的线宽:它当前太厚(想要显示大约1/2厚度)但是我可以通过包含'size'值来实现这些线条很粗 - 很厚,以至于它们遮住了整个地图。

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

  • 最后,我想将地图导出为高分辨率图像,但当我这样做时,所有内容都会再次发生变化,请参阅:

this high-res / low-res screenshot

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

代码

基础知识:阅读成员邮政编码列表,加入mySociety邮政编码表<> OSGB位置,使用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 )

0 个答案:

没有答案