前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言 绘图组合布局grid/layout

R语言 绘图组合布局grid/layout

作者头像
拴小林
发布2020-07-10 12:23:30
2K0
发布2020-07-10 12:23:30
举报
文章被收录于专栏:数据驱动实践

01

图形组合布局

par(mfrow=c(2,2))

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

代码语言:javascript
复制
##################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)

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图形的组合

代码语言:javascript
复制
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)

03

图形组合布局

grid.layout & vplayout

代码语言:javascript
复制
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,:)的位置画图

示例

代码语言:javascript
复制
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,:)的位置画图

04

图形组合布局

plot_grid {cowplot}

代码语言:javascript
复制
install.packages("cowplot") #安装cowplot包library(cowplot) # 加载?plot_grid  #帮助函数查看具体usage

示例

代码语言:javascript
复制
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)

05

图形组合布局

multiplot{Rmisc}

代码语言:javascript
复制
############################# 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)
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-05-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据驱动实践 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档