Last active
June 5, 2019 06:27
-
-
Save baptiste/2e38056222a9b95fbf0723b26100b110 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(ggplot2) | |
library(gridSVG) | |
library(grid) | |
plotCoords <- read.csv("http://eborbath.github.io/stackoverflow/PlotCoords.csv") | |
showCoords <- read.csv("http://eborbath.github.io/stackoverflow/showCoords.csv") | |
GeomHatch <- ggproto("GeomHatch", GeomPolygon, | |
required_aes = c("x", "y"), | |
default_aes = aes(colour = NA, fill = NA, size = 0.5, linetype = 1, | |
alpha = NA), | |
draw_panel = function(data, panel_params, coord) { | |
n <- nrow(data) | |
if (n == 1) return(zeroGrob()) | |
munched <- coord_munch(coord, data, panel_params) | |
# Sort by group to make sure that colors, fill, etc. come in same order | |
munched <- munched[order(munched$group), ] | |
# For gpar(), there is one entry per polygon (not one entry per point). | |
# We'll pull the first value from each group, and assume all these values | |
# are the same within each group. | |
first_idx <- !duplicated(munched$group) | |
first_rows <- munched[first_idx, ] | |
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)) | |
} | |
) | |
geom_hatch <- function(mapping = NULL, data = NULL, stat = "identity", | |
position = "identity", na.rm = FALSE, show.legend = NA, | |
inherit.aes = TRUE, ...) { | |
layer( | |
geom = GeomHatch, mapping = mapping, data = data, stat = stat, | |
position = position, show.legend = show.legend, inherit.aes = inherit.aes, | |
params = list(na.rm = na.rm, ...) | |
) | |
} | |
p <- ggplot() + | |
geom_hatch( | |
data = plotCoords, | |
aes(x = long, y = lat, group = group), | |
fill = NA, colour = NA, 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)) | |
grid.newpage() | |
grid.draw(grid.force(ggplotGrob(p))) | |
grid.export("test.svg", rootAttrs = c(width=1000,height=1000,viewBox="0 0 1000 1000")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment