R语言中的图片布局设置

R语言中的图片布局是个问题,下面是对图片布局设置的方法。

python 复制代码
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))

# The plots are layed out automatically by default
p1 + p2 + p3 + p4 + p5

# Use parentheses to group plots
(p1 + p2)/ p3 / p4


# Use byrow to change how the grid is filled out 
# byrow = FALSE表示按照列来进行排序
p1 + p2 + p3 + p4 + p5 + plot_layout(byrow = FALSE)

# Change the grid dimensions
# 第二列宽度是第一列的两倍,withths是相对高度和宽度
p1 + p2 + p3 + p4 + p5 + plot_layout(ncol = 2, widths = c(1, 2))

# Define layout at different nesting levels
# 三个维度,第一行p1p2,然后再规定p3p4在同一列,然后p5的高度是相对的两倍
p1 +
  p2 +
  (p3 +
     p4 +
     plot_layout(ncol = 1)
  ) +
  p5 +
  plot_layout(widths = c(2, 1))

# Complex layouts can be created with the `design` argument
# area(t, l, b = t, r = l) tb为上下边界, lr为左右边界
design <- c(
  area(1, 1, 2),
  area(1, 2, 1, 3),
  area(2, 3, 3),
  area(3, 1, 3, 2),
  area(2, 2)
)

plot(design)  # Show the layout to make sure it looks as it should
p1 + p2 + p3 + p4 + p5 + plot_layout(design = design)


# The same can be specified as a character string:
# 和上面的效果一样,数字相同就是占据同样的位置
design <- "
  122
  153
  443
"


#plot(design)  
p1 + p2 + p3 + p4 + p5 + plot_layout(design = design)

# Areas can be left empty
# When using strings to define the design `#` can be used to denote empty
# areas
design <- "
  1##
  123
  ##3
"
p1 + p2 + p3 + plot_layout(design = design)

# Use guides="collect" to remove duplicate guides
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')

# Guide position must be applied to entire patchwork
p6 + p7 + plot_layout(guides='collect') &
  theme(legend.position='bottom')
相关推荐
天燹2 分钟前
Qt 6 嵌入 Android 原生应用完整教程
android·开发语言·qt
liu****13 分钟前
第一章 Qt 概述
开发语言·c++·qt
知行合一。。。15 分钟前
Python--04--数据容器(列表 List)
开发语言·python
程芯带你刷C语言简单算法题17 分钟前
Day48~对于高度为 n 的台阶,从下往上走,每一步的阶数为 1,2,3 中的一个。问要走到顶部一共有多少种走法
c语言·开发语言·学习·算法·c
csbysj202020 分钟前
SQL NOT NULL约束详解
开发语言
低调小一30 分钟前
Kotlin 2025–2026 客户端开发路线:语言升级 × 跨端落地 × AI Agent 入门
开发语言·人工智能·kotlin
研☆香40 分钟前
JS中的三种显示弹窗
开发语言·前端·javascript
王夏奇1 小时前
python中的基础知识点-1
开发语言·windows·python
叫我辉哥e11 小时前
新手进阶Python:办公看板集成多数据源+ECharts高级可视化
开发语言·python·echarts
猛扇赵四那边好嘴.1 小时前
Flutter 框架跨平台鸿蒙开发 - 问答社区应用开发教程
开发语言·javascript·flutter·华为·harmonyos