上下文:我有一个数据框,其中Type记录了两个类别的数字列(conc在uptake下面的示例中)。根据我的真实数据,我需要绘制如下所示的多面图。
问题:设置scales = "free_y"不会“释放”同一行的各个面的 y 比例,事实上,当类别的值conc远大于类别的值时uptake,后者会被“压垮”。
问题:是否可以使用facet_grid()或更改facet-label来释放同一行的facet的y轴刻度facet_wrap()以匹配那些facet_grid()?
试验和错误:我已经尝试过facet_wrap(),但没有实现facet_grid()y 轴刻度良好的整齐布局。也许有一种方法可以定义facet_wrap()分面标签,但使用switch(即 strip_position)参数并没有成功。
我知道一些SO帖子已经在寻找类似的东西(参见如何在facet_wrap中定位条带标签,就像在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")
Run Code Online (Sandbox Code Playgroud)

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

由reprex 包于 2022 年 3 月 30 日创建(v2.0.1)
该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")
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2022 年 3 月 30 日创建(v2.0.1)
| 归档时间: |
|
| 查看次数: |
2945 次 |
| 最近记录: |