Skip to content

Commit

Permalink
Merge pull request #29 from jimjam-slam/dev
Browse files Browse the repository at this point in the history
v0.0.3
  • Loading branch information
jimjam-slam authored Sep 29, 2023
2 parents e3c6e51 + 8fcef92 commit 0f6e65c
Show file tree
Hide file tree
Showing 20 changed files with 241 additions and 794 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
^.*\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
^README\.Rmd$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ inst/png/
inst/svg/
inst/cairo/

README.html
38 changes: 28 additions & 10 deletions DESCRIPTION
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
674 changes: 0 additions & 674 deletions LICENSE

This file was deleted.

8 changes: 1 addition & 7 deletions NAMESPACE
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)
10 changes: 10 additions & 0 deletions NEWS.md
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.
131 changes: 68 additions & 63 deletions R/geom_flag.R
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"))
11 changes: 11 additions & 0 deletions R/lflags.R
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"
50 changes: 38 additions & 12 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -1,21 +1,47 @@
---
output:
md_document:
variant: markdown_github
output: github_document
---
## ggflags

flag geom for ggplot2
<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r demo}
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

## ggflags: Plot flags of the world in ggplot2

A flag geom for ggplot2. Uses circular SVG flags.

# Install

Install from R-Universe:

```r
install.packages("ggflags", repos = c(
"https://jimjam-slam.r-universe.dev",
"https://cloud.r-project.org"))
```

# Use

```{r demo, warning=FALSE}
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))
```
Expand All @@ -24,6 +50,6 @@ The flag SVG assets, used under the [CC-BY licence](https://github.com/eosrei/em

(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)
40 changes: 27 additions & 13 deletions README.md
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 removed README_files/figure-markdown_github/demo-1.png
Binary file not shown.
Binary file modified data/lflags.rda
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions man/dot-flaglist.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added man/figures/README-demo-1.png
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
Loading

0 comments on commit 0f6e65c

Please sign in to comment.