我有一个使用的图,facet_grid()
并想修改构面网格的条目。
考虑以下MWE:
library(ggplot2)
ggplot(mpg, aes(displ, cty, fill=cyl)) +
geom_point() +
scale_fill_continuous() +
facet_grid(rows = vars(drv))
我想替换项4
,f
和r
与字符的每一行(例如右手侧c("Case 1", "Case 2", "Case 3")
和添加的灰色框和图例(之间的条目的标题框右(即cyl
)。
的文档对facet_grid
我没有帮助-有人可以指出我正确的方向吗?
您必须为中的labeller
参数提供标签功能,该功能facet_grid()
使用命名的字符向量作为查找表。
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软件包(v0.3.0)创建于2020-05-28
编辑:
要将额外的剥离层用作扩展标题,可以使用facet_nested()
ggh4x(完全免责声明:我构建了该程序包)。
library(ggplot2)
library(ggh4x) #devtools::install_github("teunbrand/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软件包(v0.3.0)创建于2020-05-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软件包(v0.3.0)创建于2020-05-28
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句