「 獲取原始碼請至公眾號後臺回覆:一頁多圖」
圖形組合布局
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)圖形組合布局
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圖形的組合
opar <- par(no.readonly = TRUE)
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)圖形組合布局
grid.layout & vplayout
library(grid)grid.newpage() pushViewport(viewport(layout = grid.layout(2,2))) vplayout <- function(x,y){ viewport(layout.pos.row = x, layout.pos.col = y)}
print(p1, vp = vplayout(1,1)) print(p2, vp = vplayout(1,2)) print(p3, vp = vplayout(2,1:2))示例
library(ggplot2)
base <- ggplot(mpg, aes(displ, hwy)) + geom_point()p1 <- base + geom_smooth() + labs(title="圖1")
base <- ggplot(mpg, aes(displ, hwy)) + geom_point()p2 <- base %+% subset(mpg, fl == "p") + labs(title="圖2")
p3 <- base + list(subset(mpg, fl == "p"), geom_smooth(), labs(title="圖3"))
library(grid)grid.newpage() pushViewport(viewport(layout = grid.layout(2,2))) vplayout <- function(x,y){ viewport(layout.pos.row = x, layout.pos.col = y)}
print(p1, vp = vplayout(1,1)) print(p2, vp = vplayout(1,2)) print(p3, vp = vplayout(2,1:2))圖形組合布局
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")plot_grid(p1, p2, p3, p4)圖形組合布局
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)「 獲取原始碼請至公眾號後臺回覆:一頁多圖」
R 語言 邏輯運算:TRUE/FALSE
R語言入門到可視化精選19題
R語言 高階可視化繪圖系統:ggplot2入門
R語言,入門首看、必看基礎概述
R語言數據管理與dplyr、tidyr
快速掌握R語言中的apply函數族 | 精選分享
R語言 分組計算,不止group_by
用R語言讓你的可視化圖表動起來!動起來!!附原始碼
R語言 相關係數混合可視化矩陣實現
《R數據科學》是一本專門講解tidyverse相關包的書籍,主要涉及dplyr、tidyr、ggplot2、purrr等,非常值得學習,基本上此一本書可以解答數據處理的大部分問題