2017-10-28 247 views
2

我需要创建一个欧洲地图来显示各个国家/地区的变量分布。我需要黑白地图。我以ggplot为依据,并以此为例说明了这个approach。我改变了基于this blogpost的传说。这一切工作正常与这个结果:enter image description heregeom_polygon中的纹理填充

我的问题是如何更改地图的方式,我缺少填充信息和显示为纯白色的国家有纹理超过他们(我是思考对角线)?

因为我的剧本是有点乱,我只显示ggplot这里,在数据准备部分:

require(ggplot2) 

plotCoords <- read.csv("http://eborbath.github.io/stackoverflow/PlotCoords.csv") 
showCoords <- read.csv("http://eborbath.github.io/stackoverflow/showCoords.csv") 


ggplot() + 
    geom_polygon(
    data = plotCoords, 
    aes(x = long, y = lat, group = group), 
    fill = "white", colour = "darkgrey", size = 0.6) + 
    geom_polygon(
    data = showCoords, 
    aes(x = long, y = lat, group = group), 
    fill = "grey", colour = "black", size = 0.6) + 
    geom_polygon(
    data = showCoords, 
    aes(x = long, y = lat, group = group, fill = sh_left), 
    colour = "black", size = 0.1) + 
    scale_fill_gradient(
    low = "gray90", high = "gray0", 
    name = "Share of left-wing protesters", 
    guide = guide_colorbar(
     direction = "horizontal", 
     barheight = unit(2, units = "mm"), 
     barwidth = unit(50, units = "mm"), 
     draw.ulim = F, 
     title.position = 'top', 
     title.hjust = 0.5, 
     label.hjust = 0.5 
    )) + 
    scale_x_continuous(element_blank(), breaks = NULL) + 
    scale_y_continuous(element_blank(), breaks = NULL) + 
    coord_map(xlim = c(-26, 47), ylim = c(32.5, 73)) + 
    theme_bw() + 
    theme(legend.justification = c(-0.4, 1.2), legend.position = c(0, 1)) 

第一geom_polygon是背景,我想我必须编辑fill那里。显然,这对于区分没有信息与变量I图的低值很重要。鉴于我不得不依靠黑与白,我提出了使用纹理的想法,但我愿意接受其他建议。

谢谢!

+0

恐怕纹理可能无法与'ggplot2',以下哈德利的答案[这个相关的问题](https://stackoverflow.com/a/2901210/4550695)。虽然这已经很老了,所以也许现在已经实现了! –

+0

关于这个问题,我认为你并没有真正需要将纹理分隔开,没有数据的国家。对于变量值较低的国家来说,它们已经是纯白的灰色边框,与浅灰色和黑色边框形成鲜明对比。剧情看起来不错! –

回答

4

这是technically possible with gridSVG,但不知道这是值得的努力。

enter image description here

我创建了一个基于GeomPolygon一个新的GEOM,并修改了draw_panel方法返回,

gl <- by(munched, munched$group, 
     function(m){ 
      g <- polygonGrob(m$x, m$y, default.units = "native") 

      patternFillGrob(g, 
          pattern = pattern(linesGrob(gp=gpar(col="red",lwd=3)), 
              width = unit(2, "mm"), height = unit(2, "mm"), 
              dev.width = 1, dev.height = 1)) 
     }, simplify = FALSE) 

gTree(children = do.call(gList, gl)) 
+0

代码实验在这里如果有人有进一步推动这个动机https://gist.github.com/baptiste/2e38056222a9b95fbf0723b26100b110 – baptiste

+0

非常感谢!你可以请我指点一下如何用'grid.export'调整'svg'图的大小?我从来没有用过这个。理想情况下,我想将它导出为'.eps'或者'.png'。 – eborbath

+0

我已经尝试过'ggsave'或者'cowplot'包中的'plot_save'命令,但是它们都将动画留下。在我看来,'grid.export'中只能调整分辨率,而不能调整尺寸;因此图例错位等。理想情况下,我只想使用6 * 8英寸的分辨率和分辨率。我应该怎么做? – eborbath