简体   繁体   中英

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. 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:

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. Obviously, this is important to differentiate no information from low values of the variable I plot. 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.

在此处输入图片说明

I created a new geom based on GeomPolygon, and modified the draw_panel method to return,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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