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')
相关推荐
君顾13 小时前
智慧零售实战指南:从技术架构到落地方案全解析
java·开发语言·零售
南棱笑笑生4 小时前
20260904实测给飞凌OK3576-C开发板刷入Rockchip原厂的IMG固件【使用飞凌的DTS】切换串口波特率为1.5Mbps
c语言·开发语言·rockchip
平头哥技术团队4 小时前
Day 10 | 工欲善其事:VS Code 配置与项目归档
android·开发语言·前端·javascript·html·交互
乌萨奇也要立志学C++4 小时前
【洛谷】kmp算法
开发语言·算法
传奇开心果编程5 小时前
【Rust入门知识点学与练】第4课:条件判断 if / else
开发语言·学习·rust
m0_380743875 小时前
给 Claude API 调用补上超时重试和错误分类
开发语言·人工智能·php
Draw Stars5 小时前
Git BASH安装教程
开发语言·git·bash
vortex55 小时前
一文讲透 Zsh 与 Bash 的区别
开发语言·bash
千谦阙听5 小时前
C++类和对象(中):默认成员函数、构造与析构、拷贝构造、运算符重载
开发语言·c++·学习
天下无敌笨笨熊9 小时前
C#Modbus串口开发心得
开发语言·c#