您必须为facet_grid() 中的labeller 参数提供标签函数,该函数使用命名字符向量作为查找表。
library(ggplot2)
ggplot(mpg, aes(displ, cty, fill=cyl)) +
geom_point() +
scale_fill_continuous() +
facet_grid(rows = vars(drv),
labeller = as_labeller(c("4" = "Case1", "f" = "Case2", "r" = "Case3")))
由reprex package (v0.3.0) 于 2020 年 5 月 28 日创建
编辑:
要使用额外的条带层作为跨越标题,您可以使用来自 ggh4x 的 facet_nested()(完整免责声明:我构建了该包)。
library(ggplot2)
library(ggh4x)
ggplot(mpg, aes(displ, cty, fill=cyl)) +
geom_point() +
scale_fill_continuous() +
facet_nested(rows = vars("title", drv),
labeller = as_labeller(c("4" = "Case1", "f" = "Case2",
"r" = "Case3", "title" = "My Title Here")))
由reprex package (v0.3.0) 于 2020 年 5 月 28 日创建
如果您不是特别在意条带,您可以使用辅助 y 轴导轨。
library(ggplot2)
ggplot(mpg, aes(displ, cty, fill=cyl)) +
geom_point() +
scale_fill_continuous() +
facet_grid(rows = vars(drv),
labeller = as_labeller(c("4" = "Case1", "f" = "Case2", "r" = "Case3"))) +
guides(y.sec = guide_none("My Title Here"))
由reprex package (v0.3.0) 于 2020 年 5 月 28 日创建