I've seen several similar questions answered here before but I can't seem to find the right set of changes to make this work. I've tried fct_reorder
, fct_reorder2
, and reorder_within
and haven't gotten the desired result.
I have the following chart:
What I would like to do is order the discrete x-axis from high to low based on the counts of "High".
Here's what I got:
my_data
# A tibble: 40 x 4
vendor rating n count_high
<chr> <fct> <int> <dbl>
1 1002 Medium 2 0
2 1009 Low 1 0
3 1004 High 1 1
4 1015 Low 2 0
5 1000 High 1 1
6 983 Medium 3 0
7 992 Low 1 0
8 994 Medium 1 0
9 1009 Low 1 0
10 981 Low 1 0
# ... with 30 more rows
my_data %>%
ggplot(aes(x = reorder(vendor, count_high), y = n, fill = rating)) +
geom_col() +
coord_flip() +
facet_wrap(. ~ rating)
And the dput
result:
structure(list(vendor = c("1002", "1009", "1004", "1015", "1000",
"983", "992", "994", "1009", "981", "999", "1008", "989", "988",
"993", "995", "994", "996", "1007", "1005", "981", "992", "1001",
"1012", "999", "1011", "996", "998", "995", "998", "993", "991",
"1000", "1016", "996", "1002", "1004", "970", "1002", "991"),
rating = structure(c(2L, 3L, 1L, 3L, 1L, 2L, 3L, 2L, 3L,
3L, 3L, 3L, 1L, 2L, 3L, 2L, 3L, 3L, 2L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 2L, 3L, 2L, 3L, 2L,
3L), .Label = c("High", "Medium", "Low"), class = "factor"),
n = c(2L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 1L, 3L, 1L, 3L,
4L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 4L, 3L, 2L, 1L, 1L, 1L, 1L,
1L, 1L, 3L, 2L, 2L, 1L, 1L, 1L, 1L, 3L, 1L, 1L), count_high = c(0,
0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0,
0, 4, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0,
0)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-40L))