Experiment
fct_reorder_within <- function(x, y, group, decreasing = FALSE) {
# Input validation
if (!is.factor(x)) x <- as.factor(x) # Ensure x is a factor
if (length(x) != length(y) || length(x) != length(group)) {
stop("All arguments (x, y, group) must have the same length")
}
# Create a data frame (base R)
df <- data.frame(x = x, y = y, group = group, stringsAsFactors = FALSE)
# Create a unique identifier combining x and group
# Rationale: Ensures uniqueness across groups
df$combined <- paste(df$x, df$group, sep = "__")
# Order the data by group and value
# Rationale: order() with decreasing = FALSE puts smallest first, so highest is last
ord <- order(df$group, df$y, decreasing = decreasing)
df <- df[ord, ]
# Create a factor with ordered levels based on the combined identifier
# Rationale: Levels go smallest to largest, flipping to largest at top with coord_flip
result <- factor(df$combined, levels = unique(df$combined))
# Reorder back to original row order
# Rationale: Matches input row order for plotting
result <- result[order(ord)]
return(result)
}
library(tidyverse)
data <- tibble(
country = c("Sudan", "South Africa", "Zambia", "South Africa", "Egypt", "Morocco"),
category = c("biofuel", "biofuel", "biofuel", "coal", "coal", "coal"),
value = c(0.94, 0.4, 0.88, 192.73, 179.22, 33.12)
)
tidytext::reorder_within(data$country, data$category, data$value)
## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA
## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA
## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA
## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA
## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA
## Warning in mean.default(X[[i]], ...): argument is not numeric or logical:
## returning NA
## [1] Sudan___0.94 South Africa___0.4 Zambia___0.88
## [4] South Africa___192.73 Egypt___179.22 Morocco___33.12
## attr(,"scores")
## Egypt___179.22 Morocco___33.12 South Africa___0.4
## NA NA NA
## South Africa___192.73 Sudan___0.94 Zambia___0.88
## NA NA NA
## 6 Levels: Egypt___179.22 Morocco___33.12 ... Zambia___0.88
fct_cross(data$country, data$category) |>
fct_reorder(data$value) |>
levels()
## [1] "South Africa:biofuel" "Zambia:biofuel" "Sudan:biofuel"
## [4] "Morocco:coal" "Egypt:coal" "South Africa:coal"
library(tidyverse)
data <- tibble(
country = c("Sudan", "South Africa", "Zambia", "South Africa", "Egypt", "Morocco"),
category = c("biofuel", "biofuel", "biofuel", "coal", "coal", "coal"),
value = c(0.94, 0.4, 0.88, 192.73, 179.22, 33.12)
)
tidytext::reorder_within(x = data$country,
by = data$value,
within = data$category) |>
tibble(cat = _, data$value) |>
arrange(cat)
## # A tibble: 6 × 2
## cat `data$value`
## <fct> <dbl>
## 1 South Africa___biofuel 0.4
## 2 Zambia___biofuel 0.88
## 3 Sudan___biofuel 0.94
## 4 Morocco___coal 33.1
## 5 Egypt___coal 179.
## 6 South Africa___coal 193.
fct_cross(data$category, data$country, sep = "_____") |>
fct_reorder(data$value) |> # reorder on value (by)
fct_reorder(data$category) |> # reorder on cat (within)
tibble(cat = _, value = data$value) |>
arrange(cat)
## # A tibble: 6 × 2
## cat value
## <fct> <dbl>
## 1 biofuel_____South Africa 0.4
## 2 biofuel_____Zambia 0.88
## 3 biofuel_____Sudan 0.94
## 4 coal_____Morocco 33.1
## 5 coal_____Egypt 179.
## 6 coal_____South Africa 193.