Skip to content

Commit

Permalink
Add a function to import Lato
Browse files Browse the repository at this point in the history
  • Loading branch information
dickoa committed Nov 16, 2023
1 parent 738d36e commit 1d0859b
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 37 deletions.
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

export(display_unhcr_all)
export(display_unhcr_pal)
export(import_lato)
export(lato_installed)
export(lato_registered)
export(scale_color_unhcr_b)
export(scale_color_unhcr_c)
export(scale_color_unhcr_d)
Expand All @@ -14,6 +17,8 @@ export(scale_fill_unhcr_d)
export(theme_unhcr)
export(unhcr_pal)
import(ggplot2)
importFrom(extrafont,font_import)
importFrom(extrafont,fonts)
importFrom(ggrepel,GeomLabelRepel)
importFrom(ggrepel,GeomTextRepel)
importFrom(graphics,image)
Expand All @@ -22,3 +27,5 @@ importFrom(graphics,plot)
importFrom(graphics,rect)
importFrom(graphics,text)
importFrom(scales,gradient_n_pal)
importFrom(systemfonts,register_font)
importFrom(systemfonts,system_fonts)
63 changes: 63 additions & 0 deletions R/lato.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#' Check if Lato font is installed in the system
#'
#' Verify if you have Lato font installed in your system
#'
#' @importFrom systemfonts system_fonts
#'
#' @returns TRUE if Lato is installed in the system
#' @export
lato_installed <- function() {
sys_fonts <- system_fonts()
any(grepl("lato", sys_fonts$family, ignore.case = TRUE))
}

#' Check if Lato font is registered through \code{extrafont}
#'
#' Verify if Lato is registered through \code{extrafont}
#'
#' @importFrom systemfonts system_fonts
#' @importFrom extrafont fonts
#'
#' @returns TRUE if Lato is registered
#' @export
lato_registered <- function() {
any(grepl("lato", fonts(), ignore.case = TRUE))
}

#' Import Lato font
#'
#' Import Lato font for use in R graphic devices
#'
#' @importFrom systemfonts register_font
#' @importFrom extrafont font_import
#'
#' @export
import_lato <- function() {
if (!lato_installed()) {
font_dir <- system.file("fonts", "Lato",
package = "unhcrthemes")
register_font(
name = "Lato",
plain = file.path(font_dir, "Lato-Regular.ttf"),
italic = file.path(font_dir, "Lato-Italic.ttf"),
bold = file.path(font_dir, "Lato-Bold.ttf"),
bolditalic = file.path(font_dir,
"Lato-BoldItalic.ttf"))
pattern <- "(?i)lato-(regular|bold|italic|bolditalic)"
suppressMessages(extrafont::font_import(font_dir,
pattern = pattern,
prompt = FALSE))
suppressMessages(extrafont::loadfonts())
} else {
font_dir <- systemfonts::system_fonts()
b <- grepl("lato", font_dir$family, ignore.case = TRUE)
font_dir <- font_dir[b, ]
font_dir <- unique(dirname(font_dir$path))
pattern <- "(?i)lato-(regular|bold|italic|bolditalic)"
suppressMessages(extrafont::font_import(font_dir,
pattern = pattern,
prompt = FALSE))
suppressMessages(extrafont::loadfonts())
}
update_geom_font_defaults()
}
2 changes: 2 additions & 0 deletions R/theme_unhcr.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
#'
#' data(mpg, package = "ggplot2")
#'
#' import_lato()
#'
#' ggplot(mpg, aes(displ, hwy)) +
#' geom_point() +
#' theme_unhcr("Lato")
Expand Down
35 changes: 1 addition & 34 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,42 +1,9 @@
#' @noRd
lato_exists <- function() {
sys_fonts <- systemfonts::system_fonts()
any(grepl("lato", sys_fonts$family, ignore.case = TRUE))
}

.onLoad <- function(libname, pkgname) {
font_dir <- system.file("fonts", "Lato",
package = "unhcrthemes")

if (!lato_exists()) {
systemfonts::register_font(
name = "Lato",
plain = file.path(font_dir, "Lato-Regular.ttf"),
italic = file.path(font_dir, "Lato-Italic.ttf"),
bold = file.path(font_dir, "Lato-Bold.ttf"),
bolditalic = file.path(font_dir,
"Lato-BoldItalic.ttf"))
pattern <- "(?i)lato-(regular|bold|italic|bolditalic)"
suppressMessages(extrafont::font_import(font_dir,
pattern = pattern,
prompt = FALSE))
suppressMessages(extrafont::loadfonts())
} else {
font_dir <- systemfonts::system_fonts()
b <- grepl("lato", font_dir$family, ignore.case = TRUE)
font_dir <- font_dir[b, ]
font_dir <- unique(dirname(font_dir$path))
pattern <- "(?i)lato-(regular|bold|italic|bolditalic)"
suppressMessages(extrafont::font_import(font_dir,
pattern = pattern,
prompt = FALSE))
suppressMessages(extrafont::loadfonts())
}
update_geom_font_defaults()
}

.onAttach <- function(libname, pkgname) {
if (!lato_exists()) {
if (!lato_installed()) {
packageStartupMessage("Lato font is missing, please install it!")
}
}
11 changes: 11 additions & 0 deletions man/import_lato.Rd

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

14 changes: 14 additions & 0 deletions man/lato_installed.Rd

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

14 changes: 14 additions & 0 deletions man/lato_registered.Rd

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

2 changes: 2 additions & 0 deletions man/theme_unhcr.Rd

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

5 changes: 3 additions & 2 deletions vignettes/articles/design-unhcr-charts.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pak::pkg_install("unhcr-dataviz/unhcrthemes")
library(unhcrthemes)
library(refugees)
library(tidyverse)
import_lato()
```

Some packages installed above will be loaded using the `::` notation to make the code and intentions clearer.
Expand Down Expand Up @@ -235,7 +236,7 @@ ggplot(

### Stacked bar chart

Let’s make a stacked bar chart comparing the top 10 countries of origin for all people displaced across borders, by type, in 2022.
Let’s make a stacked bar chart comparing the top 10 countries of origin for all people displaced across borders, by type, in 2022.

**Preparing the data:**

Expand Down Expand Up @@ -1557,4 +1558,4 @@ ggplot(
)
```

We trust that this R package has equipped you with the tools needed to craft visually appealing and on-brand charts with ease. Should you desire further inspiration or in-depth guidance on aligning your data visualizations with UNHCR branding standards, we invite you to explore our [UNHCR data visualization platform](https://dataviz.unhcr.org). There, you will discover a multitude of [practical examples](https://dataviz.unhcr.org/tools/r/) showcasing the package's capabilities in action. Furthermore, you can delve into our comprehensive [data visualization guidelines](https://dataviz.unhcr.org/general_guidance/), which offer invaluable insights into UNHCR's brand recommendations for charts, ensuring that your visualizations harmonize perfectly with the organization's identity and mission.
We trust that this R package has equipped you with the tools needed to craft visually appealing and on-brand charts with ease. Should you desire further inspiration or in-depth guidance on aligning your data visualizations with UNHCR branding standards, we invite you to explore our [UNHCR data visualization platform](https://dataviz.unhcr.org). There, you will discover a multitude of [practical examples](https://dataviz.unhcr.org/tools/r/) showcasing the package's capabilities in action. Furthermore, you can delve into our comprehensive [data visualization guidelines](https://dataviz.unhcr.org/general_guidance/), which offer invaluable insights into UNHCR's brand recommendations for charts, ensuring that your visualizations harmonize perfectly with the organization's identity and mission.
5 changes: 4 additions & 1 deletion vignettes/unhcrthemes.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ knitr::opts_chunk$set(
)
```

The `{unhcrthemes}` package provides tools to create `{ggplot2}` visualizations that adhere to the [United Nations High Commissioner for Refugees (UNHCR) data visualization guidelines](https://dataviz.unhcr.org/general_guidance/).
The `{unhcrthemes}` package provides tools to create `{ggplot2}` visualizations that adhere to the [United Nations High Commissioner for Refugees (UNHCR) data visualization guidelines](https://dataviz.unhcr.org/general_guidance/).

One of the main functions, `theme_unhcr`, allows for the easy application of UNHCR theme to your `{ggplot2}` graphics.

Expand Down Expand Up @@ -64,6 +64,9 @@ After loading the `{unhcrthemes}` package, you can apply the theme to any `{ggpl
library(ggplot2)
library(unhcrthemes)
# Load Lato
import_lato()
# Create some data
df <- data.frame(
date = factor(rep(2010:2020, 2)),
Expand Down

0 comments on commit 1d0859b

Please sign in to comment.