全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
811 1
2021-05-10

R语言学习笔记之2021.05.11

多图布局

推荐函数:plot_layout
函数命令格式:plot_layout(
ncol = NULL,
nrow = NULL,
byrow = FALSE,#类似于matrix()中的byrow。如果为FALSE,则将按主要列顺序填充图
widths = NULL,
heights = NULL,#网格中每个列和行的相对宽度和高度。会重复出现以匹配网格的尺寸
guides = NULL,
tag_level = NULL,
design = NULL
)
示例:library(ggplot2)
p1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
p2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))
p3 <- ggplot(mtcars) + geom_bar(aes(gear)) + facet_wrap(~cyl)
p4 <- ggplot(mtcars) + geom_bar(aes(carb))
p5 <- ggplot(mtcars) + geom_violin(aes(cyl, mpg, group = cyl))

默认情况下图片的布局是:

p1 + p2 + p3 + p4 + p5

图片略

使用参数改变网格填充方式:

p1 + p2 + p3 + p4 + p5 + plot_layout(byrow = FALSE)

image-20210510102338294

更改网格尺寸:

p1 + p2 + p3 + p4 + p5 + plot_layout(ncol = 2, widths = c(1, 2)) ##改为两列,宽度分别为1,2

image-20210510102436624

在不同嵌套级别定义布局

p1 + p2 +(p3 + p4 +plot_layout(ncol = 1) ) + p5 + plot_layout(widths = c(2, 1)) ##图3和图4嵌套进一个网格并且成一列,总体两列宽度分别为2,1

image-20210510102846935

复杂的布局可以用“设计”参数来创建:

design <- c(area(1, 1, 2),area(1, 2, 1, 3),area(2, 3, 3),area(3, 1, 3, 2),area(2, 2))
p1 + p2 + p3 + p4 + p5 + plot_layout(design = design)

image-20210510103145241

相同的可以指定为字符串:

design <- “122153443”
p1 + p2 + p3 + p4 + p5 + plot_layout(design = design)

错误: Patch areas must be rectangular

正确打开方式为:

design <- "
122
153
443
"
p1 + p2 + p3 + p4 + p5 + plot_layout(design = design)

图片同上

当使用字符串定义设计时,’ # '可以用来表示空areas

design <- "

1##

123

##3

"
p1 + p2 + p3 + plot_layout(design = design)

image-20210510104142646

使用参考线="collect"删除重复的参考线:

p6 <- ggplot(mtcars) + geom_point(aes(mpg, disp, color=cyl))
p7 <- ggplot(mtcars) + geom_point(aes(mpg, hp, color=cyl))
p6 + p7 + plot_layout(guides=‘collect’)

image-20210510104431134

引导位置必须适用于整个拼接

p6 + p7 + plot_layout(guides=‘collect’) &
theme(legend.position=‘bottom’)

image-20210510104451752
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2021-5-10 11:24:30
R语言学习笔记.pdf
大小:(307.63 KB)

 马上下载

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群