facet_sub 为 ggplot2 添加子图

引言


构建一个 facet_sub, 类似于分面的效果, 给 ggplot2 添加一个 panel, 里面可以插入任何你想插入的图形, 但得是 grob 对象。
安装

install.packages(“devtools”)

devtools::install_github(“junjunlab/JunJunZai”)

or

remotes::install_github(“junjunlab/JunJunZai”)
示例

position 来指定添加的位置, 包括 上下左右:
library(JunJunZai)
library(ggplot2)
library(grid)

p1 <- ggplot(mtcars,
aes(x = hp, y = mpg,color = factor(cyl))) +
geom_point() +
facet_sub(position = “top”)

p2 <- ggplot(mtcars,
aes(x = hp, y = mpg,color = factor(cyl))) +
geom_point() +
facet_sub(position = “right”)

p3 <- ggplot(mtcars,
aes(x = hp, y = mpg,color = factor(cyl))) +
geom_point() +
facet_sub(position = “bottom”)

p4 <- ggplot(mtcars,
aes(x = hp, y = mpg,color = factor(cyl))) +
geom_point() +
facet_sub(position = “left”)

cowplot::plot_grid(plotlist = list(p1,p2,p3,p4),
align = “hv”)

panelWidth 用来控制相对大小:
ggplot(mtcars,
aes(x = hp, y = mpg,color = factor(cyl))) +
geom_point() +
facet_sub(position = “right”,
panelWidth = c(1,0.5))

addGrob 用来接收 grob 的 list, 比如我们画个圈和方框:
ggplot(mtcars,
aes(x = hp, y = mpg,color = factor(cyl))) +
geom_point() +
facet_sub(position = “right”,
addGrob = gList(rectGrob(),circleGrob()))

如果是 ggplot 对象, 你可以使用 ggplotify::as.grob 来转换为 grob, 然后进行插入。
结尾


路漫漫其修远兮,吾将上下而求索。
欢迎加入生信交流群。加我微信我也拉你进 微信群聊 老俊俊生信交流群 (微信交流群需收取 20 元入群费用,一旦交费,拒不退还!(防止骗子和便于管理)) 。QQ 群可免费加入, 记得进群按格式修改备注哦。

声明:文中观点不代表本站立场。本文传送门:https://eyangzhen.com/416758.html

(0)
联系我们
联系我们
分享本页
返回顶部