如何在ggplot2 :: facet_grid中格式化网格标题和条目

伊沃

我有一个使用的图,facet_grid()并想修改构面网格的条目。

考虑以下MWE:

library(ggplot2)
ggplot(mpg, aes(displ, cty, fill=cyl)) + 
  geom_point() +
  scale_fill_continuous() +
  facet_grid(rows = vars(drv))

我想替换4fr与字符的每一行(例如右手侧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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在ggplot2中使用facet_grid制作甜甜圈图?

如何在ggplot2中按组为facet_grid着色?

如何在ggplot2的甘特图R中使用facet_grid

如何在ggplot2中显示obs的方向(标题)

如何在ggplot2中更改Y轴的标题?

结合 facet_grid (ggplot2) 和 denscomp (fitdistrplus)

在 ggplot2 中格式化 Geom_Bar

如何在ggplot2中绘制logit和概率

ggplot2:如何在R中正确格式化sec.axis

如何在Angular Kendo Grid中格式化日期

r-如何在ggplot2中使用facet_grid获得长标签以适合?

R和ggplot 2:如何在ggplot2 R图中设置轴的分位数限制?

ggplot2:如何获取facet_grid()的labeller = label_both和facet_wrap()的ncol选项的合并功能?

如何在Android中格式化日期和时间?

如何在JavaScript中格式化货币和时间

如何在ggplot2中将difftime格式化为hh:mm?

如何在ggplot2中将多个标签格式化为单个图例

如何在ggplot2中正确绘制投影的网格化数据?

ggplot2 facet_grid,带有方面标题

ggplot2中的facet_wrap()和facet_grid()有什么区别?

如何將 ggplot2 geom_col 與 facet_grid 和 coord_flip 一起使用

通过表达式生成时如何在ggplot2中对齐标题和字幕

如何在Rstudio中的ggplot2中添加主标题和操纵轴标签

如何在Java的String格式化程序中格式化Time和String值

ggplot2:facet_grid中x轴标签的条件格式

如何使用ggplot2 facet_grid注释没有数据的构面?

R ggplot2 facet_grid与vars():如何处理缺少的参数?

ggplot2:如何使geom_text()与facet_grid()很好地玩?

如何使用facet_grid(ggplot2)在每个构面上绘制多条线