Skip to content

Instantly share code, notes, and snippets.

@jimjam-slam
Created March 3, 2022 03:11
Show Gist options
  • Save jimjam-slam/28aa18463c7b62a0ae123d5c0e1323f4 to your computer and use it in GitHub Desktop.
Save jimjam-slam/28aa18463c7b62a0ae123d5c0e1323f4 to your computer and use it in GitHub Desktop.
Combine an interacgive with ggflags and custom fonts with Google Fonts (that work even for remote viewers without the font installed) #rstatstips
library(tidyverse)
library(ggiraph)
library(ggflags)
library(gfonts)
library(here)
mydata <- tribble(
~ x, ~ y, ~ flag, ~ is_home,
1, 1, "au", "yes",
2, 2, "us", "no",
3, 3, "gb", "no")
# get the 360info font from google fonts
setup_font(
id = "source-serif-pro", output_dir = "./fonts",
variants = c("regular", "700"),
prefer_local_source = FALSE
)
systemfonts::register_font(
name = "Source Serif Pro",
plain = list("fonts/fonts/source-serif-pro-v11-latin-regular.woff", 0),
bold = list("fonts/fonts/source-serif-pro-v11-latin-700.woff", 0)
)
myplot <-
ggplot(mydata) +
aes(x, y) +
# flag layer is not interactive...
geom_flag(aes(country = flag), size = 20)
# ... but if we want hover we can put (nearly) invisible points over the top
geom_point_interactive(
aes(tooltip = flag, data_id = is_home), size = 10) +
labs(title = "My awesome plot") +
theme_minimal(base_family = "Source Serif Pro") +
theme(plot.title = element_text(face = "bold", size = rel(2)))
# create the interactive, add the remote google fonts url (so it's downloaded),
# and save to disk
girafe(code = print(myplot), fonts = list(sans = "Source Serif Pro")) %>%
htmlwidgets::prependContent(
htmltools::HTML(
"<style>",
"@import url('https://fonts.googleapis.com/css2?family=Source+Serif+Pro:wght@400;700&display=swap');",
"</style>")) %>%
saveWidget("test.html")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment