forked from ellisp/ggflags
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from jimjam-slam/dev
v0.0.3
- Loading branch information
Showing
20 changed files
with
241 additions
and
794 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
^.*\.Rproj$ | ||
^\.Rproj\.user$ | ||
^LICENSE\.md$ | ||
^README\.Rmd$ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ inst/png/ | |
inst/svg/ | ||
inst/cairo/ | ||
|
||
README.html |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,38 @@ | ||
Package: ggflags | ||
Type: Package | ||
Version: 0.0.2 | ||
Title: Plot flags of the world in ggplot2. | ||
Authors@R: c(person("Baptiste", "Auguie", | ||
Version: 0.0.3 | ||
Title: Plot flags of the world in ggplot2 | ||
Authors@R: c( | ||
person("Baptiste", "Auguie", | ||
email = "[email protected]", | ||
role = c("aut", "cre")), | ||
person("James", "Goldie", | ||
email = "[email protected]", | ||
role = c("ctb"))) | ||
License: GPL-3 | ||
Description: A ggplot2 extension that allows you to plot the flags of the world. It functions essentially as geom_point does, requiring, at minimum, a two-letter lowercase country code for the country aesthetic, and x and y aesthetics. You can also adjust the size. | ||
email = "[email protected]", | ||
role = c("aut", "ctb"), | ||
comment = c( | ||
ORCID = "0000-0002-5024-6207" | ||
)), | ||
person( | ||
given = "Rémi", | ||
family = "Thériault", | ||
role = c("aut", "ctb"), | ||
email = "[email protected]", | ||
comment = c( | ||
ORCID = "0000-0003-4315-6788", | ||
Twitter = "@rempsyc"))) | ||
License: GPL (>= 3) | ||
Description: A ggplot2 extension that allows you to plot the flags of the world. It functions essentially | ||
as geom_point does, requiring, at minimum, a two-letter lowercase country code for the country aesthetic, | ||
and x and y aesthetics. You can also adjust the size. | ||
LazyData: TRUE | ||
Depends: | ||
ggplot2 | ||
R (>= 3.5.0) | ||
Imports: | ||
grid, | ||
grImport2, | ||
plotly | ||
RoxygenNote: 7.1.1 | ||
ggplot2, | ||
scales | ||
Encoding: UTF-8 | ||
RoxygenNote: 7.2.3 | ||
URL: https://github.com/jimjam-slam/ggflags | ||
BugReports: https://github.com/jimjam-slam/ggflags/issues |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,5 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
S3method(makeContent,flag) | ||
S3method(grid::makeContent,flag) | ||
export(geom_flag) | ||
export(scale_country) | ||
importFrom(grImport2,pictureGrob) | ||
importFrom(grid,gList) | ||
importFrom(grid,gTree) | ||
importFrom(grid,makeContent) | ||
importFrom(grid,setChildren) | ||
importFrom(grid,unit) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# ggflags 0.0.3 | ||
|
||
- Adds Rémi Thériault as an author and contributor! | ||
- Package now complies with R CMD CHECK, paving the way for possible CRAN submission in the future! | ||
- The package will also promptly be available on https://jimjam-slam.r-universe.dev | ||
|
||
# 0.0.2 | ||
|
||
- Fork from ellisp/ggflags | ||
- Switches from the use of rectangular PNG flags in ellisp/ggflags to circular SVG flags. This makes them much better for plotting at high resolution, and especially for use in bubble plots. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,82 @@ | ||
|
||
flagGrob <- function(x, y, country, size=1, alpha=1){ | ||
# grob(x=x, y=y, country=country, size=size, cl = "flag") | ||
gTree(x = x, y = y, country = country, size = size, cl = "flag") | ||
} | ||
|
||
#' @title flag geom for ggplot2 | ||
#' @param mapping mapping | ||
#' @param data data | ||
#' @param stat stat | ||
#' @param position position | ||
#' @param na.rm na.rm | ||
#' @param show.legend show.legend | ||
#' @param inherit.aes inherit.aes | ||
#' @param ... ... | ||
#' | ||
#' @examples | ||
#' data(lflags) | ||
#' set.seed(1234) | ||
#' d <- data.frame( | ||
#' x = rnorm(10), y = rnorm(10), | ||
#' country = sample(c("ar", "fr"), 10, TRUE), | ||
#' stringsAsFactors = FALSE | ||
#' ) | ||
#' ggplot2::ggplot(d, ggplot2::aes(x = x, y = y, country = country, size = x)) + | ||
#' geom_flag() + | ||
#' scale_country() | ||
#' @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) | ||
}) | ||
setChildren(x, do.call(gList, flag_pics)) | ||
geom_flag <- function(mapping = NULL, data = NULL, stat = "identity", | ||
position = "identity", na.rm = FALSE, show.legend = NA, | ||
inherit.aes = TRUE, ...) { | ||
ggplot2::layer( | ||
geom = GeomFlag, mapping = mapping, data = data, stat = stat, | ||
position = position, show.legend = show.legend, inherit.aes = inherit.aes, | ||
params = list(na.rm = na.rm, ...) | ||
) | ||
} | ||
|
||
#' @title scale countries | ||
#' @param guide guide | ||
#' @param ... ... | ||
#' @export | ||
scale_country <- function(..., guide = "legend") { | ||
sc <- discrete_scale("country", "identity", scales::identity_pal(), ..., guide = guide, | ||
super = ScaleDiscreteIdentity) | ||
|
||
sc <- ggplot2::discrete_scale("country", "identity", scales::identity_pal(), ..., | ||
guide = guide, | ||
super = ggplot2::ScaleDiscreteIdentity | ||
) | ||
sc | ||
} | ||
|
||
GeomFlag <- ggproto("GeomFlag", Geom, | ||
required_aes = c("x", "y", "country"), | ||
default_aes = aes(size = 5, country="nz"), | ||
|
||
draw_key = function (data, params, size) | ||
{ | ||
flagGrob(0.5,0.5, country=data$country, size=data$size) | ||
}, | ||
|
||
draw_group = function(data, panel_scales, coord) { | ||
coords <- coord$transform(data, panel_scales) | ||
flagGrob(coords$x, coords$y, coords$country, coords$size) | ||
} | ||
#' @noRd | ||
GeomFlag <- ggplot2::ggproto("GeomFlag", ggplot2::Geom, | ||
required_aes = c("x", "y", "country"), | ||
default_aes = ggplot2::aes(size = 5, country = "nz"), | ||
draw_key = function(data, params, size) { | ||
flagGrob(0.5, 0.5, country = data$country, size = data$size) | ||
}, | ||
draw_group = function(data, panel_scales, coord) { | ||
coords <- coord$transform(data, panel_scales) | ||
flagGrob(coords$x, coords$y, coords$country, coords$size) | ||
} | ||
) | ||
|
||
#' @noRd | ||
flagGrob <- function(x, y, country, size = 1, alpha = 1) { | ||
grid::gTree(x = x, y = y, country = country, size = size, cl = "flag") | ||
} | ||
|
||
#' geom_flag | ||
#' | ||
#' @param mapping | ||
#' @param data | ||
#' @param stat | ||
#' @param position | ||
#' @param na.rm | ||
#' @param show.legend | ||
#' @param inherit.aes | ||
#' @param ... | ||
#' | ||
#' @examples | ||
#' data(lflags) | ||
#' set.seed(1234) | ||
#' d <- data.frame(x=rnorm(10), y=rnorm(10), | ||
#' country=sample(c("ar","fr"), 10, TRUE), | ||
#' stringsAsFactors = FALSE) | ||
#' ggplot(d, aes(x=x, y=y, country=country, size=x)) + | ||
#' geom_flag() + | ||
#' scale_country() | ||
#' @importFrom grid unit gTree gList makeContent setChildren | ||
#' @importFrom grImport2 pictureGrob | ||
#' @export | ||
geom_flag <- function(mapping = NULL, data = NULL, stat = "identity", | ||
position = "identity", na.rm = FALSE, show.legend = NA, | ||
inherit.aes = TRUE, ...) { | ||
layer( | ||
geom = GeomFlag, mapping = mapping, data = data, stat = stat, | ||
position = position, show.legend = show.legend, inherit.aes = inherit.aes, | ||
params = list(na.rm = na.rm, ...) | ||
#' @noRd | ||
#' @exportS3Method grid::makeContent | ||
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] * grid::unit(1, "mm"), | ||
height = x$size[ii] * grid::unit(1, "mm"), | ||
distort = FALSE | ||
) | ||
} | ||
) | ||
grid::setChildren(x, do.call(grid::gList, flag_pics)) | ||
} | ||
|
||
#' @noRd | ||
utils::globalVariables(c(".flaglist")) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#' List of country flags | ||
#' | ||
#' @docType data | ||
#' @format A data frame with X rows and X variables: | ||
#' \describe{ | ||
#' \item{state.name}{the name of the state} | ||
#' \item{state.abb}{the name of the abbreviation} | ||
#' \item{state.regex}{the regex for that state} | ||
#' ... | ||
#' } | ||
".flaglist" |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,48 @@ | ||
ggflags | ||
------- | ||
|
||
flag geom for ggplot2 | ||
<!-- README.md is generated from README.Rmd. Please edit that file --> | ||
|
||
## ggflags: Plot flags of the world in ggplot2 | ||
|
||
A flag geom for ggplot2. Uses circular SVG flags. | ||
|
||
# Install | ||
|
||
Install from R-Universe: | ||
|
||
``` r | ||
library(ggflags) | ||
install.packages("ggflags", repos = c( | ||
"https://jimjam-slam.r-universe.dev", | ||
"https://cloud.r-project.org")) | ||
``` | ||
|
||
## Loading required package: ggplot2 | ||
# Use | ||
|
||
``` r | ||
library(ggplot2) | ||
library(ggflags) | ||
|
||
set.seed(1234) | ||
d <- data.frame(x=rnorm(50), y=rnorm(50), | ||
country=sample(c("ar","fr", "nz", "gb", "es", "ca", "lv", "qa"), 50, TRUE), | ||
stringsAsFactors = FALSE) | ||
ggplot(d, aes(x=x, y=y, country=country, size=x)) + | ||
geom_flag() + | ||
d <- data.frame( | ||
x = rnorm(50), y = rnorm(50), | ||
country = sample(c("ar", "fr", "nz", "gb", "es", "ca", "lv", "qa"), 50, TRUE), | ||
stringsAsFactors = FALSE | ||
) | ||
|
||
ggplot(d, aes(x = x, y = y, country = country, size = x)) + | ||
geom_flag() + | ||
scale_country() + | ||
scale_size(range = c(0, 15)) | ||
``` | ||
|
||
![](README_files/figure-markdown_github/demo-1.png) | ||
<img src="man/figures/README-demo-1.png" width="100%" /> | ||
|
||
The flag SVG assets, used under the [CC-BY | ||
licence](https://github.com/eosrei/emojione-color-font/blob/master/LICENSE-CC-BY.txt), | ||
are taken from the EmojiOne set: | ||
<a href="https://github.com/eosrei/emojione-color-font" class="uri">https://github.com/eosrei/emojione-color-font</a> | ||
<https://github.com/eosrei/emojione-color-font> | ||
|
||
(Note: the EmojiOne set doesn’t include UK counties or US states!) | ||
|
||
![](README_files/figure-markdown_github/flags.png) | ||
![](man/figures/flags.png) | ||
|
||
(note: proof of principle only) |
Binary file not shown.
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Oops, something went wrong.