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

大卫·Z

这是一个例子:

library(ggplot2)
set.seed(123)
df<-data.frame(sid=letters[1:8], 
               groups=rep(1:4, each=2), 
               blp=abs(rnorm(8, 120, 5)),
               bmi=abs(rnorm(8, 25, 5)),
               gender=rep(c("F", "M"), each=4))

ggplot(df, aes(bmi, blp))+
    geom_point(size=2)+
facet_grid(sid~groups)

我想要的是sid按性别区分颜色理想的数字是:在此处输入图片说明

林正

您可以将ggplot转换为grob,并在其中进行更改:

# convert to grob
gp <- ggplotGrob(p) # where p is the original ggplot object

# assign the first 4 right-side facet strips with blue fill
for(i in 1:4){
  grob.i <- grep("strip-r", gp$layout$name)[i]
  gp$grobs[[grob.i]]$grobs[[1]]$children[[1]]$gp$fill <- "blue"
}
# assign the next 4 right-side facet strips with red fill
for(i in 5:8){
  grob.i <- grep("strip-r", gp$layout$name)[i]
  gp$grobs[[grob.i]]$grobs[[1]]$children[[1]]$gp$fill <- "red"
}

grid::grid.draw(gp)

情节

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在ggplot2 facet_grid中为R平方方程着色并将其移动到下一行?

ggplot2:在 facet_grid 中按组添加水平线

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

在ggplot2中,如何在facet_wrap中按组添加文本?

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

ggplot2中的facet_grid错误

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

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

ggplot2 facet_grid:如何修复geom_col中列之间的不同间距

如何在ggplot的facet_grid函数中应用下标

ggplot中的facet_grid中的行式着色

在 facet_grid 中将 label_both 设置为 ggplot2 中主题的默认值

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

ggplot2和facet_grid:为每个图添加最大值

在ggplot2中使用facet_grid()时如何定义常见的y轴限制

如何在ggplot2中为两条线之间的区域着色/着色?

ggplot2:添加用于facet_grid的变量的名称

结合 facet_grid (ggplot2) 和 denscomp (fitdistrplus)

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

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

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

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

在ggplot2中的特定构面之间添加空间(facet_grid)

在ggplot2中旋转切换的构面标签facet_grid

使用ggplot2中的facet_grid更改中断次数

ggplot2 R中的facet_grid函数的非线性拟合

`ggplot2` 中的 `annotate` 与 `facet_grid` 结合时会报告错误

变更单保证金显示在facet_grid ggplot2中?

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