Last active
July 2, 2019 14:10
-
-
Save paleolimbot/cbe59b49351edd15410df78838dd6e15 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(dplyr) | |
library(tibble) | |
SortingRange <- ggproto( | |
"SortingRange", ggplot2:::Range, | |
counted_range = tibble(value = character(0), n = integer(0)), | |
train = function(self, x, ...) { | |
new_counted_range <- tibble(value = x) %>% | |
count(value) | |
self$counted_range <- bind_rows(self$counted_range, new_counted_range) %>% | |
group_by(value) %>% | |
summarise(n = sum(n)) %>% | |
arrange(desc(n)) | |
self$range <- self$counted_range$value | |
invisible(x) | |
} | |
) | |
ScaleDiscretePositionCounted <- ggproto( | |
"ScaleDiscreteCounted", ScaleDiscretePosition, | |
range = ggproto(NULL, SortingRange), | |
clone = function(self) { | |
new <- ggproto_parent(ScaleDiscretePosition, self)$clone() | |
new$range <- ggproto(NULL, SortingRange) | |
new | |
} | |
) | |
scale_x_discrete_sorted <- function(..., position = "bottom", expand = waiver()) { | |
sc <- discrete_scale( | |
c("x", "xmin", "xmax", "xend"), "position_d", identity, ..., | |
expand = expand, guide = "none", position = position, super = ScaleDiscretePositionCounted | |
) | |
sc$range_c <- ggplot2:::continuous_range() | |
sc | |
} | |
ggplot(mpg, aes(class)) + | |
geom_bar() + | |
facet_wrap(vars(drv), ncol = 1, scales = "fixed") + | |
scale_x_discrete_sorted() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Created on 2019-07-02 by the reprex package (v0.2.1)