Skip to content

Commit

Permalink
Outlines appearing but some bugs to fix
Browse files Browse the repository at this point in the history
I'm using a geom_point with shape 21 (a ring) to do the borders. They're
rendering, but there are problems:
1) outline sizes aren't calculated properly, so they don't stay on the flag
   edges.
2) colours are totally messed up, and it seems like they stick if i draw a new
   plot with modified data. i'm guessing a scope problem (local vars that don't
   get wiped properly)?
3) flags spill out of the plotting area in the rstudio render and in ggsave. they
   don't in svg export, though. this doesn't seem like desirable behaviour, as the
   geom_point outline clips properly in all circumstances.
  • Loading branch information
jimjam-slam committed Sep 16, 2017
1 parent 401494a commit caee020
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 12 deletions.
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
S3method(makeContent,flag)
export(geom_flag)
export(scale_country)
importFrom(ggplot2,.pt)
importFrom(ggplot2,.stroke)
importFrom(grImport2,pictureGrob)
importFrom(grid,gList)
importFrom(grid,gTree)
importFrom(grid,gpar)
importFrom(grid,grobTree)
importFrom(grid,makeContent)
importFrom(grid,pointsGrob)
importFrom(grid,setChildren)
importFrom(grid,unit)
55 changes: 43 additions & 12 deletions R/geom_flag.R
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@

flagGrob <- function(x, y, country, size=1, alpha=1){
flagGrob <- function(x, y, country, size=1, alpha=1, stroke = 0, colour = 'black'){
# grob(x=x, y=y, country=country, size=size, cl = "flag")
gTree(x = x, y = y, country = country, size = size, cl = "flag")
gTree(x = x, y = y, country = country, size = size, stroke = stroke,
colour = colour, cl = "flag")
}

#' @export
makeContent.flag <- function(x) {
flag_pics <- lapply(seq_along(x$country),
function(ii) {
grImport2::pictureGrob(
picture = .flaglist[[x$country[[ii]]]],
x = x$x[ii], y = x$y[ii],
width = x$size[ii] * unit(1, "mm"),
height = x$size[ii] * unit(1, "mm"),
distort = FALSE)
if (x$stroke[ii] > 0)
{
grobTree(
grImport2::pictureGrob(
picture = .flaglist[[x$country[[ii]]]],
x = x$x[ii], y = x$y[ii],
width = x$size[ii] * unit(1, "mm"),
height = x$size[ii] * unit(1, "mm"),
distort = FALSE),
pointsGrob(
x = x$x[ii], y = x$y[ii], pch = 21,
gp = gpar(
fill = 0, col = x$colour[ii],
fontsize =
(x$size[ii] * .pt) + (x$stroke[ii] * .stroke),
lwd = x$stroke[ii] * .stroke / 2
)
)
)
} else if (x$stroke[ii] == 0)
{
grImport2::pictureGrob(
picture = .flaglist[[x$country[[ii]]]],
x = x$x[ii], y = x$y[ii],
width = x$size[ii] * unit(1, "mm"),
height = x$size[ii] * unit(1, "mm"),
distort = FALSE)
} else
{
stop('ggflags: stroke must be positive or zero.')
}
})
setChildren(x, do.call(gList, flag_pics))
}
Expand All @@ -28,16 +54,20 @@ scale_country <- function(..., guide = "legend") {

GeomFlag <- ggproto("GeomFlag", Geom,
required_aes = c("x", "y", "country"),
default_aes = aes(size = 5, country="nz"),
default_aes = aes(size = 5, country = "nz", stroke = 0,
colour = "black"),

draw_key = function (data, params, size)
{
flagGrob(0.5,0.5, country=data$country, size=data$size)
flagGrob(0.5,0.5, country=data$country, size=data$size, stroke = data$stroke,
colour = data$colour)
},

draw_group = function(data, panel_scales, coord) {
coords <- coord$transform(data, panel_scales)
flagGrob(coords$x, coords$y, coords$country, coords$size)
flagGrob(x = coords$x, y = coords$y,
country = coords$country, size = coords$size,
stroke = coords$stroke, colour = coords$colour)
}
)

Expand All @@ -62,8 +92,9 @@ GeomFlag <- ggproto("GeomFlag", Geom,
#' ggplot(d, aes(x=x, y=y, country=country, size=x)) +
#' geom_flag() +
#' scale_country()
#' @importFrom grid unit gTree gList makeContent setChildren
#' @importFrom grid unit gTree gList grobTree makeContent setChildren pointsGrob gpar
#' @importFrom grImport2 pictureGrob
#' @importFrom ggplot2 .stroke .pt
#' @export
geom_flag <- function(mapping = NULL, data = NULL, stat = "identity",
position = "identity", na.rm = FALSE, show.legend = NA,
Expand Down

0 comments on commit caee020

Please sign in to comment.