geom_polygon中的纹理填充

时间:2017-10-28 18:13:38

标签: r ggplot2 data-visualization

我需要创建一张欧洲地图,以显示各国之间变量的分布情况。我需要黑白地图。我依靠ggplot并以此approach为例。我根据this blogpost更改了图例。所有这些都适用于此结果:enter image description here

我的问题是如何改变地图的方式,我错过填充信息的国家和显示为纯白色的国家有一个纹理覆盖他们(我在想对角线)?

由于我的脚本有点乱,我只是在这里显示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图的低值没有信息很重要。鉴于我必须依赖黑白,我提出了使用纹理的想法,但我愿意接受其他建议。

谢谢!

1 个答案:

答案 0 :(得分:6)

它的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))