r library car_R语言 绘图组合布局grid/layout

7b9b37109fea21354898b3c2cfa98e97.png

67715ab7e41954b610f338a623bb701c.png

获取源代码请至公众号后台回复:一页多图

​01—图形组合布局par(mfrow=c(2,2))

par(mfrow=c(2,2)),可以理解将绘图区域分割为2x2的矩阵区域,另可参照《R语言实战》3.5图形的组合

##################par(mfrow=c(2,2))####################attach(iris)opar <- par(no.readonly = TRUE)# data(iris)# head(iris)# > head(iris)# Sepal.Length Sepal.Width Petal.Length Petal.Width Species# 1          5.1         3.5          1.4         0.2  setosa# 2          4.9         3.0          1.4         0.2  setosa# 3          4.7         3.2          1.3         0.2  setosa# 4          4.6         3.1          1.5         0.2  setosa# 5          5.0         3.6          1.4         0.2  setosa# 6          5.4         3.9          1.7         0.4  setosa​par(mfrow=c(2,2)) # 设置2x2的布局plot(Sepal.Length,Sepal.Width, main = "第一张图")boxplot(Sepal.Length, horizontal = TRUE, main = "第二张图") boxplot(Sepal.Width, main = "第三张图") hist(Petal.Length, main = "第四张图")​par(opar)detach(iris)

1120a5ee91d3dbb19de85422e23bf591.png

02—图形组合布局par(fig=c(x1, x2, y1, y2), new = TRUE)

par(fig=c(x1, x2, y1, y2), new = TRUE),取x1,x2,y1,y2四条线圈住的位置绘图图形,另可参照《R语言实战》3.5图形的组合

d5302ad363d4439011b005308d60f32a.png
opar <- par(no.readonly = TRUE)# data(iris)# head(iris)# > head(iris)# Sepal.Length Sepal.Width Petal.Length Petal.Width Species# 1          5.1         3.5          1.4         0.2  setosa# 2          4.9         3.0          1.4         0.2  setosa# 3          4.7         3.2          1.3         0.2  setosa# 4          4.6         3.1          1.5         0.2  setosa# 5          5.0         3.6          1.4         0.2  setosa# 6          5.4         3.9          1.7         0.4  setosa​par(fig=c(0,0.8,0,0.8)) # 设置散点图的布局参数plot(iris$Sepal.Length,iris$Sepal.Width) #绘制散点图​par(fig=c(0,0.8,0.65,1),new = TRUE) #设置上方箱型图的布局参数boxplot(iris$Sepal.Length, horizontal = TRUE, axes=FALSE) # 绘制上方箱型图​par(fig=c(0.65,1,0,0.8),new = TRUE) #设置右侧箱型图的布局参数boxplot(iris$Sepal.Width, axes=FALSE) # 绘制右侧箱型图​mtext("par(fig=c(x1, x2, y1, y2), new = TRUE)",side = 3, outer=TRUE, line=-3)par(opar)

8447c4f08bafbc103261d1be7fd722c4.png

03—图形组合布局grid.layout & vplayout

library(grid)grid.newpage()  ##新建页面pushViewport(viewport(layout = grid.layout(2,2))) #将页面分成2*2矩阵vplayout <- function(x,y){ viewport(layout.pos.row = x, layout.pos.col = y)}​print(p1, vp = vplayout(1,1))   #(1,1)的位置画图1print(p2, vp = vplayout(1,2))   #(1,2)的位置画图2print(p3, vp = vplayout(2,1:2))  #(2,:)的位置画图

示例

library(ggplot2)​#绘制基本ggplot图base <- ggplot(mpg, aes(displ, hwy)) + geom_point()p1 <- base + geom_smooth() + labs(title="图1") #如图1​#用%+%调整映射关系中的数据base <- ggplot(mpg, aes(displ, hwy)) + geom_point()# To override the data, you must use %+% #也即覆盖原始数据必须通过%+%p2 <- base %+% subset(mpg, fl == "p") + labs(title="图2") #图2​#第二种调整数据的方法list# Alternatively, you can add multiple components with a list.# This can be useful to return from a function.p3 <- base + list(subset(mpg, fl == "p"), geom_smooth(), labs(title="图3")) #图3​###########一页多图########library(grid)grid.newpage()  ##新建页面pushViewport(viewport(layout = grid.layout(2,2))) #将页面分成2*2矩阵vplayout <- function(x,y){ viewport(layout.pos.row = x, layout.pos.col = y)}​print(p1, vp = vplayout(1,1))   #(1,1)的位置画图1print(p2, vp = vplayout(1,2))   #(1,2)的位置画图2print(p3, vp = vplayout(2,1:2))  #(2,:)的位置画图

2b768f9e9af893bc31649bb74776782d.png

04—图形组合布局plot_grid {cowplot}

install.packages("cowplot") #安装cowplot包library(cowplot) # 加载?plot_grid  #帮助函数查看具体usage

示例

library(ggplot2)​df <- data.frame(  x = 1:10, y1 = 1:10, y2 = (1:10)^2, y3 = (1:10)^3, y4 = (1:10)^4)​p1 <- ggplot(df, aes(x, y1)) + geom_point()p2 <- ggplot(df, aes(x, y2)) + geom_point()p3 <- ggplot(df, aes(x, y3)) + geom_point()p4 <- ggplot(df, aes(x, y4)) + geom_point()p5 <- ggplot(mpg, aes(as.factor(year), hwy)) +  geom_boxplot() +  facet_wrap(~class, scales = "free_y")# simple gridplot_grid(p1, p2, p3, p4)

8168c9d3c036561c608466dab1eef9fe.png

05—图形组合布局multiplot{Rmisc}

############################# multiplot{Rmisc} ##################################library(Rmisc)library(ggplot2)​df <- data.frame(  x = 1:10, y1 = 1:10, y2 = (1:10)^2, y3 = (1:10)^3, y4 = (1:10)^4)​p1 <- ggplot(df, aes(x, y1)) + geom_point()p2 <- ggplot(df, aes(x, y2)) + geom_point()p3 <- ggplot(df, aes(x, y3)) + geom_point()p4 <- ggplot(df, aes(x, y4)) + geom_point()p5 <- ggplot(mpg, aes(as.factor(year), hwy)) +  geom_boxplot() +  facet_wrap(~class, scales = "free_y")​multiplot(p1, p2, p3, p5, cols=2)​

51ffc9f6caf34f7104c3b72747cbdcae.png

获取源代码请至公众号后台回复:一页多图

【往期回顾推荐】

R 语言 逻辑运算:TRUE/FALSE

R语言入门到可视化精选19题

R语言 高阶可视化绘图系统:ggplot2入门

R语言,入门首看、必看基础概述

R语言数据管理与dplyr、tidyr

快速掌握R语言中的apply函数族 | 精选分享

R语言 分组计算,不止group_by

用R语言让你的可视化图表动起来!动起来!!附源代码

R语言 相关系数混合可视化矩阵实现

《R数据科学》是一本专门讲解tidyverse相关包的书籍,主要涉及dplyr、tidyr、ggplot2、purrr等,非常值得学习,基本上此一本书可以解答数据处理的大部分问题

9f3c463c3c48f38e394f7ebed0d402db.png
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值