Closed espinielli closed 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...
Thanks for your example.
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.
Created on 2021-04-29 by the reprex package (v1.0.0)