jimjam-slam / ggflags

A flag geom for ggplot2. Tweaks the original by using round flags (great for plotting as points).
91 stars 14 forks source link

Use ggsvg for rendering #36

Open jimjam-slam opened 1 month ago

jimjam-slam commented 1 month ago

Avoids clipping problems with grImport2 (see #17). Still a WIP: flags are successfully drawn, but problems to fix are:

jimjam-slam commented 1 month ago

Seeing how https://github.com/coolbutuseless/ggsvg/blob/main/R/ggsvg.R does it is v helpful, even though that package is much more complex with its CSS overrides!

jimjam-slam commented 1 month ago

This is looking pretty good now! 🥳

library(ggplot2)
devtools::load_all()

gdp <- data.frame(
  name = c("us", "cn", "jp", "de", "in", "gb", "fr", "it", "ca"),
  gdp = c(26854, 19374, 4410, 4309, 3740, 3160, 2924, 2170, 2090),
  x = c(1, 2, 3, 1, 2, 3, 1, 2, 3),
  y = c(3, 3, 3, 2, 2, 2, 1, 1, 1))

p1 <- ggplot(gdp) +
  aes(x, y) +
  geom_flag(aes(country = name, size = gdp),
    key_glyph = draw_key_flag("jp")) +
  geom_point(aes(size = gdp), colour = "#00ff0099") +
  geom_text(
    aes(label = toupper(name)),
    nudge_y = -0.3,
    size = 9,
    family = "Inter",
    fontface = "bold") +
  geom_text(
    aes(label = scales::label_dollar()(gdp)),
    family = "Inter",
    nudge_y = -0.45,
    size = 12) +
  scale_size(
    range = c(1, 60),
    guide = guide_none()
    ) +
  scale_x_continuous(expand = expansion(.22)) +
  scale_y_continuous(expand = expansion(.22)) +
  theme_void(base_size = 28, base_family = "Inter") +
  theme(
    plot.title = element_text(face = "bold"),
    plot.subtitle = element_text(colour = "#333333")
  ) +
  labs(
    title = "Top 10 economies by GDP",
    subtitle = "In $US billions in 2023",
    caption = "Source: International Monetary Fund"
  ) +
  coord_cartesian(clip =  "off")

ggsave("test.png", p1, bg = "white", width = 10, height = 13)

test9