简体   繁体   English

geom_polygon填充中的纹理

[英]texture in geom_polygon fill

I need to create a European map to show the distribution of a variable across countries. 我需要创建一个欧洲地图来显示变量在各个国家之间的分布。 I need the map in black and white. 我需要黑白地图。 I rely on ggplot and followed this approach as an example. 我依靠ggplot并遵循此方法作为示例。 I changed the legend based on this blogpost . 我根据此博客文章更改了图例。 All this works fine with this result: 所有这些都可以正常工作,结果如下: 在此处输入图片说明

My question is how to change the map in a way that the countries where I am missing the information for fill and are shown as pure white have a texture over-them (I am thinking diagonal lines)? 我的问题是如何更改地图,以使那些我缺少填充信息并且显示为纯白色的国家的纹理超过它们(我在考虑对角线)?

Since my script is a bit messy, I just show the ggplot here, without the data preparation part: 由于我的脚本有点混乱,因此我仅在此处显示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))

The first geom_polygon is for the background, I assume I have to edit the fill there. 第一个geom_polygon用于背景,我假设我必须在此处编辑fill Obviously, this is important to differentiate no information from low values of the variable I plot. 显然,这对于区分无信息与变量I的低值很重要。 Given I have to rely on black and white I came up with the idea of using textures, but I am open to alternative suggestions. 考虑到我必须依靠黑白,我想到了使用纹理的想法,但是我愿意接受其他建议。

Thanks! 谢谢!

it's technically possible with gridSVG , but not sure it's worth the effort. 使用gridSVG技术上是可行的 ,但不确定是否值得付出努力。

在此处输入图片说明

I created a new geom based on GeomPolygon, and modified the draw_panel method to return, 我基于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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM