I have problems with the arrangement of bars in ggplot2 using additionally the package ggpattern.
This is my data:
# reqired packages
library(tibble)
library(ggplot2)
library(ggpattern)
# example data
data <- tibble::tribble(~time, ~origin, ~destination, ~flow,
"2015", "BRA", "CHN", 57,
"2016", "BRA", "CHN", -70,
"2015", "BRA", "USA", 88,
"2016", "BRA", "USA", 200,
"2015", "MEX", "CHN", 99,
"2016", "MEX", "CHN", 70,
"2015", "MEX", "USA", 50,
"2016", "MEX", "USA", 100,
)
I want that all the destination countries have distinct colors and additionally that there is a distinction between the origin countries.
I have something like this in mind:
# plot
plot <- ggplot(data, aes(x = time,
fill = destination)) +
geom_col_pattern(aes(y = flow,
pattern = origin),
col = "black", pattern_colour = "grey",
pattern_spacing = 0.05, pattern_density = 0.09,
pattern_key_scale_factor = 0.5)
plot
which produces following chart:
However, I would prefer to have the bars in such order to have the destinationation countries next to each other. For this plot this would mean that the blue parts are next to eacch other and also the red parts. Or in other words, we first split by destination countries (colors) and then origin countries (ggpattern stripes).
Another thing is that the legend is misleading: there should only be colors in the "destination" legend.
PS: Maybe this approach is a bit problematic when you have positive (eg from "BRA") and negative (eg from "MEX") "flows" in different destination countries in the same year..