盘点R语言的那些拼图方法
2019-10-03 本文已影响0人
小洁忘了怎么分身
title: "盘点R语言的那些拼图方法"
author: "花花"
date: "2019-09-18"
1.par里的mfrow
可用于基础包拼图
par(mfrow = c(2,2))
plot(1:100 + rnorm(100))
plot(rnorm(100), type = "l")
hist(rnorm(500))
acf(rnorm(100))
2.grid.arrange
ggplot2拼图御用
https://www.jianshu.com/p/0970fdac9071
if(!require(dplyr))install.packages("dplyr")
library(dplyr)
test <- iris %>%
head(120) %>%
tail(40) %>%
rbind(head(iris,20)) %>%
mutate(n=1:60)
table(test$Species)
#>
#> setosa versicolor virginica
#> 20 20 20
if(!require(ggplot2))install.packages("ggplot2")
library(ggplot2)
p <- ggplot(data = test) +theme_bw()
colnames(test)
#> [1] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
#> [5] "Species" "n"
##密度图
p_density <- p + geom_density(
aes(Sepal.Length, color = Species)
)+theme(legend.position = "none")
##箱线图
p_boxplot <- p + geom_boxplot(
aes(x = Species,y = Sepal.Length,color = Species)
)+theme(legend.position = "none")
##小提琴图
p_violin <- p + geom_violin(
aes(x = Species, y = Sepal.Length, fill = Species)
)+theme(legend.position = "none")
## 点图
p_point <- p + geom_point(aes(x=n,y = Sepal.Length))+theme(legend.position = "none")
## 管他什么图
p_col <- p + geom_col(aes(x=n,y=Sepal.Length,fill = Species))+theme(legend.position = "none")
## 凑数用
p_s <- p + geom_area(aes(x=n,y=Sepal.Length,fill = Species))+theme(legend.position = "none")
if(!require(gridExtra))install.packages("gridExtra")
library(gridExtra)
plots = list(p_density,p_boxplot,p_violin)
lay1 = rbind(c(1, 2),
c(3, 3)) #布局矩阵
grid.arrange(grobs = plots,
layout_matrix = lay1,
widths = c(1, 2), #第一列的宽度为1,第二列的宽度为2
heigths = c(1, 0.1)) #依然不知道为什么高度调整失败
让布局再复杂一点
lay2 = rbind(c(1, 3),
c(2, 3)) #布局矩阵
plots2 <- list(p_s,p_col,p_point)
p1 <- arrangeGrob(grobs = plots,
layout_matrix = lay1)
p2 <- arrangeGrob(grobs = plots2,
layout_matrix = lay2)
grid.arrange(p1, p2, ncol = 2)
grid.arrange(p1, p2, nrow = 2)
3.cowplot
可以给图加上ABCD编号蛮好的。
if(!require(cowplot))install.packages("cowplot")
library(cowplot)
gg <- ggdraw() +
draw_plot(p_boxplot, x=0, y=0.5, width=1, height=0.5) +
draw_plot(p_violin, 0, 0, 0.5, 0.5) +
draw_plot(p_density, 0.5, 0, 0.5, 0.5) +
draw_plot_label(c("A", "B", "C"), c(0, 0, 0.5), c(1, 0.5, 0.5), size = 15, colour = "black")
gg
4.customLayout
这个灰常好!我要详细讲一下
(1)简单布局
if(!require(customLayout))install.packages("customLayout")
library(customLayout)
lay1 <- lay_new(
mat = matrix(1:4, ncol = 2),#数字矩阵
widths = c(3, 2),#宽度比
heights = c(2, 1)) #高度比
lay_show(lay1)
lay2 <- lay_new(
mat = matrix(1:4, ncol = 2),
widths = c(3, 5),
heights = c(2, 4))
lay_show(lay2)
(2)两个简单面板拼接
#1)横向
lay3 = lay_bind_col(lay1, lay2, widths = c(3, 1))
lay_show(lay3)
#2)纵向
lay4 <- lay_bind_row(lay1, lay2, heights = c(5, 2))
lay_show(lay4)
(3)嵌套
lay <- lay_new(
matrix(1:4, nc = 2),
widths = c(3, 2),
heights = c(2, 1))
lay_show(lay)
lay2 <- lay_new(
matrix(1:4, nc = 2),
widths = c(3, 5),
heights = c(2, 4))
lay_show(lay2)
把lay2嵌套金lay1的第一个面板
slay <- lay_split_field(lay1, lay2, field = 1)
lay_show(slay)
把lay1嵌套进lay2的第4个面板
flay <- lay_split_field(lay2, lay1, field = 4)
lay_show(flay)
#############################################################
(4)基础绘图的拼接
#本段代码来自帮助文档
par(mar = c(3, 2, 2, 1))
lay <- lay_new(
matrix(1:4, nc = 2),
widths = c(3, 2),
heights = c(2, 1))
lay2 <- lay_new(matrix(1:3))
cl <- lay_bind_col(lay, lay2, widths = c(3, 1))
lay_show(cl)
lay_set(cl)
plot(1:100 + rnorm(100))
plot(rnorm(100), type = "l")
hist(rnorm(500))
acf(rnorm(100))
pie(c(3, 4, 6), col = 2:4)
pie(c(3, 2, 7), col = 2:4 + 3)
pie(c(5, 4, 2), col = 2:4 + 6)
(5)grid(ggplot2)图形对象的拼接
lay <- lay_new( matrix(1:2, ncol = 1))
lay2 <- lay_new(matrix(1:3))
cl <- lay_bind_col(lay, lay2, widths = c(3, 1))
lay_show(cl)
plots2 <- list(p_boxplot,p_density,p_point,p_col,p_s)
lay_grid(plots2, cl)