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

Feature request: Add possibility for custom country categories #34

Closed
rempsyc opened this issue May 31, 2024 · 1 comment
Closed

Feature request: Add possibility for custom country categories #34

rempsyc opened this issue May 31, 2024 · 1 comment

Comments

@rempsyc
Copy link

rempsyc commented May 31, 2024

Follow-up on #6 and #10.

Here is my use case: My data set has MANY countries and it is not useful to plot all countries when most of them after the top 8 have very small percentages. So I create a category called "Other" to group all other countries together, which I also want to represent on the plot. But then I get the error stated above. I wish there was a way to plot custom categories or just NA values (but that it also appears in the plot legend).

packageVersion("ggflags")
#> [1] '0.0.4'

country_4legend <- c("us", "gb", "nl", "de", "ca", "be", "au", "ru", "other")

df <- data.frame(x = rep(1:10, each = 10),
                 y = rep(1:10, 10),
                 country = c(country_4legend[-9], 
                             rep(country_4legend, each = 10), "other", "other"))

# Custom category doesn't work
ggplot2::ggplot(df, ggplot2::aes(x, y, country = country)) +
  ggflags::geom_flag(size = 8.5)
#> Error in (function (classes, fdef, mtable) : unable to find an inherited method for function 'grobify' for signature '"NULL"'

# Missing values work
country_4legend <- c("us", "gb", "nl", "de", "ca", "be", "au", "ru", NA)

df <- data.frame(x = rep(1:10, each = 10),
                 y = rep(1:10, 10),
                 country = c(rep(country_4legend, each = 11), NA))

ggplot2::ggplot(df, ggplot2::aes(x, y, country = country)) +
  ggflags::geom_flag(size = 8.5)
#> Warning: Removed 12 rows containing missing values or values outside the scale range
#> (`geom_flag()`).

# But not the legend
ggplot2::ggplot(df, ggplot2::aes(x, y, country = country)) +
  ggflags::geom_flag(size = 8.5) +
  ggflags::scale_country(breaks = country_4legend)
#> Warning: Removed 12 rows containing missing values or values outside the scale range
#> (`geom_flag()`).
#> Error in (function (classes, fdef, mtable) : unable to find an inherited method for function 'grobify' for signature '"NULL"'

country_4legend2 <- c(country_4legend[-9], "NA")
ggplot2::ggplot(df, ggplot2::aes(x, y, country = country)) +
  ggflags::geom_flag(size = 8.5) +
  ggflags::scale_country(breaks = country_4legend2)
#> Warning: Removed 12 rows containing missing values or values outside the scale range
#> (`geom_flag()`).

Created on 2024-05-31 with reprex v2.1.0

@rempsyc
Copy link
Author

rempsyc commented May 31, 2024

Actually, the feature already exists! It is just not documented but labels can be fed directly to scale_country().

library(ggflags)

d <- data.frame(
  x = rnorm(10), y = rnorm(10),
  country = sample(c("AR", "FR"), 10, TRUE),
  stringsAsFactors = FALSE
)
p <- ggplot2::ggplot(d, ggplot2::aes(x = x, y = y, country = tolower(country))) +
  geom_flag() +
  scale_country(labels = c("AR", "FR"))
p

Created on 2024-05-31 with reprex v2.1.0

@rempsyc rempsyc closed this as completed May 31, 2024
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

No branches or pull requests

1 participant