diff --git a/NAMESPACE b/NAMESPACE index 6748bcc..11fea7f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/geom_flag.R b/R/geom_flag.R index a89283d..c1fe9e7 100644 --- a/R/geom_flag.R +++ b/R/geom_flag.R @@ -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)) } @@ -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) } ) @@ -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,