我希望基于兩個因素對圖形進行構面,使用兩個構面因素值的組合重命名構面,但根據原始因素中的水平保留構面的順序。
資料看起來像這樣:
library(tidyverse)
set.seed(100)
tmp.d <- data.frame(
sector = factor(rep(c("B","A"),c(6,3)), levels = c("B","A")),
subsector = factor(rep(c("a","b","c"), each = 3), levels = c("c","b","a")),
year = factor(rep(2020:2022,3)),
value = sample(8:15,9, replace = TRUE)
)
#> tmp.d
# sector subsector year value
#1 B a 2020 9
#2 B a 2021 14
#3 B a 2022 13
#4 B b 2020 15
#5 B b 2021 10
#6 B b 2022 8
#7 A c 2020 9
#8 A c 2021 13
#9 A c 2022 11
這是按部門和子部門繪制和分面的...
ggplot(tmp.d, aes(x = year, y = value, group = 1))
geom_path()
facet_wrap(facets = list("sector","subsector"))
...看起來像這樣:

請注意,構面保持由“部門”和“子部門”的因子水平設定的順序。這是可取的。
但是,我不想在單獨的行上列出扇區和子扇區,而是希望分面標簽讀取“[扇區]:[子扇區]”,如“B:b”。
嘗試1:
向 tmp.d 添加一個幫助列,其中包含構面標簽。
tmp.d <- tmp.d %>% mutate(label = factor(paste0(sector, ": ", subsector)))
ggplot(tmp.d, aes(x = year, y = value, group = 1))
geom_path()
facet_wrap(facets = list("label"))
產生:

Here, the facet labels are correct, but I've lost the order from the sector/subsector factor levels.
Attempt 2:
I think the answer may lay in a custom as_labeller function or perhaps even changing setting for an existing labeller like label_value which has a multi_line = [bool] attribute that controls whether the facet values appear on a single line or mulitple lines. Other versions of the label_ family have another attribute sep = which I beleive controls how the values are seperated in on the same line. Presumably, the combination of ...multi_line = FALSE, sep = ": "... might format the label and preserve the desired order.
The labeller is applied in the call to facet_wrap().
ggplot(tmp.d, aes(x = year, y = value, group = 1))
geom_path()
facet_wrap(facets = list("sector","subsector"), labeller = [the labeller function])
Setting the labeller to an existing labeller function without changing default settings (see below) yields the same output as my original attempt above.
...
facet_wrap(facets = list("sector","subsector"), labeller = label_value)
...
Attempting to change the attribute values for label_value like so...
...
facet_wrap(facets = list("sector","subsector"), labeller = label_value(multi_line = FALSE))
...
... does not work because the label_value function requires a label value that I do not know how to provide. Passing the facet factors as names or as character strings (either as a list or vector) does not appear to work. The examples I found in the documentation or elsewhere use facet_grid instead of facet_wrap, and the labels is provided as a formula like ~sector subsector which I assume is treated like a grid/matrix where sectors are columns and subsectors are rows. In my case, most (but not necessarily all) combinations of sector/subsector will be unique (i.e., Sectors A and B do not share subsectors).
Question Summary
Is there a simple way to acheive my objectives (restated for convenience):
- facet on two factor variables (facet_wrap, not facet_grid)
- 根據因子水平保留構面順序
- 將分面標簽重新格式化為單行,扇區和子扇區由冒號分隔
謝謝,
uj5u.com熱心網友回復:
哇,這比我預期的要復雜得多......一種解決方案是將它們組合到不同的領域:
tmp.d |>
arrange(sector, subsector) |> # arrange by factor levels
mutate(
facet =
paste0(sector, ": ", subsector) |>
fct_inorder(ordered = TRUE) # use that order for the new field
) |>
ggplot(aes(x = year, y = value, group = 1))
geom_path()
facet_wrap(facets = ~facet) # here
如果“,”是可以接受的,這也有效:
ggplot(tmp.d, aes(x = year, y = value, group = 1))
geom_path()
facet_wrap(
facets = sector~subsector,
labeller =
labeller( # here
sector = label_value, #
subsector = label_value, #
.multi_line = FALSE #
)
)
可以做類似的事情,用purrr::partial()它代替默認值,但你又得到一個逗號。我認為值得在他們的 github 頁面上創建一個問題以向函式添加sep引數label_*()
...
facet_wrap(
facets = sector~subsector,
labeller = purrr::partial(label_value, multi_line = FALSE)
)
uj5u.com熱心網友回復:
更新:同時 yake84 已經完成了答案:要自動化,只需fct_inorder..在使用后添加arrange:
tmp.d %>%
arrange(sector, subsector) %>%
mutate(my_label = paste(sector,subsector, sep=":") %>%
fct_inorder(ordered = TRUE)) %>%
ggplot(aes(x = year, y = value, group = 1))
geom_path()
facet_wrap( ~ my_label)
第一個答案: 只需將您的標簽轉換為嘗試 1 以考慮并定義級別:
library(tidyverse)
tmp.d %>%
mutate(my_label = paste(sector,subsector, sep=":")) %>%
mutate(my_label = factor(my_label, levels = c("B:b", "B:a", "A:c"))) %>%
ggplot(aes(x = year, y = value, group = 1))
geom_path()
facet_wrap( ~ my_label)

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/419358.html
標籤:
上一篇:ggplot2圖例沒有出現
