trevorld / ggpattern

ggplot geoms with pattern fills
https://trevorldavis.com/R/ggpattern/dev/
Other
361 stars 18 forks source link

flags for bars #32

Closed espinielli closed 3 years ago

espinielli commented 3 years ago

This is not really an issue of the library in terms of functionality, but rather a request for clarification on how to use it. If solved it could make it as an enhancement of one of the existing examples in the vignette. I am referring to the example of country flags inside the bars of a bar chart.

I would like to do exactly that BUT associating the right flag to the iso code in my data. The following example shows that flags are not associated with the right ISO code for the relevant city. It escapes to me how to make it work 😞 .

Any hints/directions is super welcome.

library(tibble)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggpattern)
library(fs)
library(ggplot2)
#> 
#> Attaching package: 'ggplot2'
#> The following objects are masked from 'package:ggpattern':
#> 
#>     flip_data, flipped_names, gg_dep, has_flipped_aes, remove_missing,
#>     should_stop, waiver

flags <- c(
  system.file("img", "flag", "au.png", package = "ggpattern"),
  system.file("img", "flag", "dk.png", package = "ggpattern"),
  system.file("img", "flag", "gb.png", package = "ggpattern"),
  system.file("img", "flag", "gr.png", package = "ggpattern"),
  system.file("img", "flag", "no.png", package = "ggpattern"),
  system.file("img", "flag", "se.png", package = "ggpattern"),
  system.file("img", "flag", "us.png", package = "ggpattern")
) %>%
  as_tibble() %>%
  transmute(flag = value,
            iso = fs::path_file(flag) %>% fs::path_ext_remove() %>% toupper()) %>%
  relocate(iso, flag)

d <- tribble(
  ~iso, ~city, ~value,
  "US", "New York", 10,
  "US", "Dallas", 13,
  "AU", "Sidney", 21,
  "DK", "Copenhagen", 7
  ) %>%
  left_join(flags)
#> Joining, by = "iso"

ggplot(d, aes(x = city, y = value)) +
  geom_bar_pattern(
    aes(
      pattern_filename = iso
    ),
    pattern         = 'image',
    pattern_type    = 'none',
    fill            = 'grey80',
    colour          = 'black',
    pattern_scale   = -2,
    pattern_filter  = 'point',
    pattern_gravity = 'east',
    stat = "identity"
  ) +
  theme_bw(18) +
  labs(
    title = "Country flags in Bar chart",
    subtitle = "pattern = 'image'"
  ) +
  theme(legend.position = 'none') +
  scale_pattern_filename_discrete(choices = d %>% pull(flag)) +
  coord_flip() +
  scale_pattern_discrete(guide = guide_legend(nrow = 1))

Created on 2021-04-29 by the reprex package (v1.0.0)

espinielli commented 3 years ago

Ok, I got inspired by your blog post and came out with the following solution:

library(tibble)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggpattern)
library(ggplot2)
#> 
#> Attaching package: 'ggplot2'
#> The following objects are masked from 'package:ggpattern':
#> 
#>     flip_data, flipped_names, gg_dep, has_flipped_aes, remove_missing,
#>     should_stop, waiver
library(flagon)

ccodes <- flagon::country_codes %>%
  select(country, ccode)

d <- tribble(
  ~iso, ~city, ~value,
  "us", "New York", 10,
  "us", "Dallas", 13,
  "au", "Sidney", 21,
  "dk", "Copenhagen", 7
  ) %>%
  left_join(ccodes, by = c("iso" = "ccode")) %>%
  mutate(
    flag = flagon::flags(iso)
  ) %>% 
  arrange(-value) %>%  
  mutate(rank = 1:n()) 

ggplot(d, aes(x = city, y = value)) +
  geom_bar_pattern(
    aes(
      pattern_filename = I(flag)
    ),
    pattern         = 'image',
    pattern_type    = 'none',
    fill            = 'grey80',
    colour          = 'black',
    pattern_scale   = -2,
    pattern_filter  = 'point',
    pattern_gravity = 'west',
    stat = "identity"
  ) +
  theme_minimal() +
  labs(
    title = "Country flags in Bar chart"
  ) +
  coord_flip()

Created on 2021-04-29 by the reprex package (v1.0.0)

Maybe worth an update of the gallery in the vignette...

trevorld commented 3 years ago

Thanks for your example.