is there a way to reorder those facets based on their Y value (aka count
) in decreasing order dynamically - without hardcoding the order (given a similar column to package
with hundreds of levels?
library(cranlogs)
library(tidyverse)
tidyverse_downloads <- cran_downloads(
packages = pkgs,
from = "2017-01-01",
to = "2017-06-30") %>%
tibble::as_tibble() %>%
group_by(package)
tidyverse_downloads %>%
ggplot(aes(x = date, y = count)) +
#geom_hline(yintercept = 0, color = "grey40") +
geom_line() +
#geom_smooth(method = "loess") +
theme_minimal() +
facet_wrap(~ package, scale = "free_y",ncol = 2) + theme(axis.text.x = element_text(angle = 90, hjust = 1))
What I have tried was to use the package library(tidytext) and scale_y_reordered
tidyverse_downloads %>%
mutate(count = tidytext::reorder_within(count, date, package)) %>%
tidytext::scale_y_reordered() +
ggplot(aes(x = date, y = count)) +
geom_line() +
theme_minimal() +
facet_wrap(~ package, scale = "free_y",ncol = 2) + theme(axis.text.x = element_text(angle = 90, hjust = 1))
however, that doesn't produce any outcome. Im doing something wrong I guess. Appreciate any help
You can sum the total downloads of each package to gather the factor levels in decreasing order.
library(cranlogs)
library(tidyverse)
pkgs <- c('dplyr', 'broom', 'ggplot2', 'purrr')
tidyverse_downloads <- cran_downloads(
packages = pkgs,
from = "2017-01-01",
to = "2017-06-30") %>%
tibble::as_tibble()
tidyverse_downloads %>%
group_by(package) %>%
summarise(count = sum(count)) %>%
arrange(desc(count)) %>%
pull(package) -> levels
levels
#[1] "ggplot2" "dplyr" "purrr" "broom"
Assign the levels in package
according to levels
above.
tidyverse_downloads %>%
mutate(package = factor(package, levels)) %>%
ggplot(aes(x = date, y = count)) +
geom_line() +
theme_minimal() +
facet_wrap(~ package, scale = "free_y",ncol = 2) +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.