Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ggsvg for rendering #36

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft

Use ggsvg for rendering #36

wants to merge 10 commits into from

Conversation

jimjam-slam
Copy link
Owner

@jimjam-slam jimjam-slam commented Jun 2, 2024

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

  • Flags are a bit blurry,
  • Flag justification is off
  • Plot render produces error (flags render but later plot layers don't): Error in if (rasterRatio > vpRatio) { : missing value where TRUE/FALSE needed }

@jimjam-slam jimjam-slam marked this pull request as draft June 2, 2024 09:33
@jimjam-slam
Copy link
Owner Author

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
Copy link
Owner Author

jimjam-slam commented Jun 3, 2024

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

)
grid::setChildren(x, do.call(grid::gList, flag_pics))
flag_size <- size * grid::unit(1, "mm")
flag_grob <- ggsvg::svg_to_rasterGrob(
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I convert the svg_text to raw using charToRaw(), I can skip ggsvg and use rsvg::rsvg_nativeraster() instead (which takes the same arguments). That's probably for the best, as I don't think Mike intends for ggsvg to be super maintained, and rsvg is well maintained.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant