背景關系:我有一個資料框,其中Type記錄了兩個類別的數字列(在下面conc的uptake示例中)。使用我的真實資料,我需要制作一個多面圖,如下所示。
問題:設定scales = "free_y"不會“釋放”同一行方面的 y 比例,實際上,當類別conc的值遠大于類別的值時uptake,后者被“壓碎”。
問題:是否可以使用facet_grid()或更改 facet-label fromfacet_wrap()以匹配同一行的 facet 的 y 軸比例facet_grid()?
試驗和錯誤:我已經嘗試過facet_wrap(),但沒有達到facet_grid()y 軸刻度很好時的整潔布局。也許有一種方法可以定義facet_wrap()facet-labels,但玩弄switch( ie strip_position ) 引數并沒有成功。
我知道一些 SO 帖子已經在尋找類似的東西(參見How to position strip labels in facet_wrap like in facet_grid)但也許(我希望 ^^)另一個,也許“更簡單”的解決方案從那以后出現了。
library(magrittr) # for %>%
library(tidyr) # for pivot longer
library(ggplot2)
df <- CO2 %>% pivot_longer(cols = c("conc", "uptake"))
# nice facet labels but bad y-axis
ggplot(data = df, aes(x = Type, y = value))
geom_boxplot()
facet_grid(Treatment ~ name, scales = "free_y")

# nice y-axis but bad facet labels
ggplot(data = df, aes(x = Type, y = value))
geom_boxplot()
facet_wrap(Treatment ~ name, scales = "free_y")

由reprex 包創建于 2022-03-30 (v2.0.1)
uj5u.com熱心網友回復:
該ggh4x::facet_grid2()函式有一個independent引數,您也可以使用該引數在行和列中自由縮放。免責宣告:我是 ggh4x 的作者。
library(magrittr) # for %>%
library(tidyr) # for pivot longer
library(ggplot2)
df <- CO2 %>% pivot_longer(cols = c("conc", "uptake"))
ggplot(data = df, aes(x = Type, y = value))
geom_boxplot()
ggh4x::facet_grid2(Treatment ~ name, scales = "free_y", independent = "y")

由reprex 包創建于 2022-03-30 (v2.0.1)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/452995.html
