library(tidyverse)
df <- data.frame(country = c("USA", "USA", "USA",
"UK", "UK", "UK",
"Japan", "Japan", "Japan",
"Germany", "Germany", "Germany",
"Sweden","Sweden","Sweden",
"Norway","Norway","Norway"),
year = c(2000, 2001, 2002,
2000, 2001, 2002,
2000, 2001, 2002,
2000, 2001, 2002,
2000, 2001, 2002,
2000, 2001, 2002),
value = c(0,1,1,
1,1,1,
0,0,0,
0,0,1,
0,1,1,
0,1,0))
ggplot(df, aes(year, country, fill = factor(value)))+
geom_tile(color = "white", size = 1)+
theme_minimal()
This is the data, code, and raw plot that I get.
Is there a way that I can reorder country rows by their value
values and year
at the same time?
My goal: country rows will be ordered by their first value
=1 in year
s.
Problem: because I also want legend to be factor, not continuous. This creates problem in ordering.
Ideal order:
- 1st row: UK
- 2nd row: Sweden or USA (because they have the same values)
- 3rd row: Sweden or USA (because they have the same values)
- 4th row: Norway
- 5th row: Germany
- 6th row: Japan