There are several different ways to achieve OP's goal per this answer
(1) reorder_within()
function to reorder name
within period
facets.
library(tidyverse)
library(forcats)
df <- data.frame(
name = c("foo", "bar", "foo", "bar"),
period = c("old", "old", "recent", "recent"),
val = c(1.23, 2.17, 4.15, 3.65)
)
reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
new_x <- paste(x, within, sep = sep)
stats::reorder(new_x, by, FUN = fun)
}
scale_x_reordered <- function(..., sep = "___") {
reg <- paste0(sep, ".+$")
ggplot2::scale_x_discrete(labels = function(x) gsub(reg, "", x), ...)
}
ggplot(df, aes(reorder_within(name, val, period), val)) +
geom_col() +
scale_x_reordered() +
facet_grid(period ~ ., scales = "free", space = "free") +
coord_flip() +
theme_minimal() +
theme(panel.grid.major.y = element_blank())
Or (2) similar idea
### https://trinkerrstuff.wordpress.com/2016/12/23/ordering-categories-within-ggplot2-facets/
df %>%
mutate(name = reorder(name, val)) %>%
group_by(period, name) %>%
arrange(desc(val)) %>%
ungroup() %>%
mutate(name = factor(paste(name, period, sep = "__"),
levels = rev(paste(name, period, sep = "__")))) %>%
ggplot(aes(name, val)) +
geom_col() +
facet_grid(period ~., scales = "free", space = 'free') +
scale_x_discrete(labels = function(x) gsub("__.+$", "", x)) +
coord_flip() +
theme_minimal() +
theme(panel.grid.major.y = element_blank()) +
theme(axis.ticks.y = element_blank())
Or (3) orders the entire data frame, and also orders the categories (period
) within each facet group!
### https://drsimonj.svbtle.com/ordering-categories-within-ggplot2-facets
#
df2 <- df %>%
# 1. Remove any grouping
ungroup() %>%
# 2. Arrange by
# i. facet group (period)
# ii. value (val)
arrange(period, val) %>%
# 3. Add order column of row numbers
mutate(order = row_number())
df2
#> name period val order
#> 1 foo old 1.23 1
#> 2 bar old 2.17 2
#> 3 bar recent 3.65 3
#> 4 foo recent 4.15 4
ggplot(df2, aes(order, val)) +
geom_col() +
facet_grid(period ~ ., scales = "free", space = "free") +
coord_flip() +
theme_minimal() +
theme(panel.grid.major.y = element_blank())
# To finish we need to replace the numeric values on each x-axis
# with the appropriate labels
ggplot(df2, aes(order, val)) +
geom_col() +
scale_x_continuous(
breaks = df2$order,
labels = df2$name) +
# scale_y_continuous(expand = c(0, 0)) +
facet_grid(period ~ ., scales = "free", space = "free") +
coord_flip() +
theme_minimal() +
theme(panel.grid.major.y = element_blank()) +
theme(legend.position = "bottom",
axis.ticks.y = element_blank())
Created on 2018-11-05 by the reprex package (v0.2.1.9000)
grid.arrange
from thegridExtra
package. But I agree that it generally doesn't result in a very nice plot. (You'll find that a lot in ggplot; if something is really hard to do, it's probably because it's trying to keep you from doing something stupid. Not always, but a lot...) – Mongoosegrid.arrange
. But most people just assume that faceting = arranging multiple plots that are generally similar. – Mongoosediscrete_scale
(e.g. alphabetical, or some overall order by mean value of y) is somewhat arbitrary anyway, so the notion that several facets must share the same categorical scale is a bit artificial to me. In my mind it makes more sense to decide that x, while showing categories, is ranked by some metric, and let the labels fall where they may in each facet. In that sense, the common scale that is shared across all facets is that numerical metric. It is a bit like plotting text labels in a scatterplot. – Kymberlykymograph