目標(biāo)
使用R拼出這樣的一張圖:
Let's start
1. 安裝 customLayout
包
install.packages("customLayout")
# or
library(devtools)
install_github("zzawadz/customLayout")
簡單介紹一下 customLayout
包:
lay_new(mat, widths, heights)
- mat: 數(shù)字矩陣,以1開始且連續(xù), 確定區(qū)數(shù)和排布順序
- widths、heights:確定每一區(qū)的比例
lay_show(lay)
- 預(yù)覽分區(qū)
lay_bind_row(lay1, lay2, ..., heights)
- 按行將兩個布局按特定比例合并,不支持合并兩個以上的布局,下同(太拉了~)
lay_bind_col(lay1, lay2, ..., widths)
- 按列將兩個布局按特定比例合并
lay_split_field(lay1, lay2, field = idx)
- 將lay1分割一特定部分(第idx區(qū))給lay2,即將lay2嵌入lay1的內(nèi)部
lay_set(lay)
- 使用指定的布局進行繪圖
lay_grid(grobs, lay)
- 用于繪制ggplot對象,并指定布局
- grobs: 包含多個繪圖對象的list
- lay:繪圖布局
有了這些功能,基本上就可以滿足拼圖需求了。
2. 布局設(shè)計
首先根據(jù)效果圖將圖進行分區(qū)并且編號,方便制定排版方案,編號規(guī)則為從上到下,從左到右
編號之后應(yīng)該是這樣的:
然后確定排版方案:
- 分為三大部分,12、3456、7 三部分
- 其中3456部分又可以分為三小部分3、4、56
- 56又可以分為5、6兩小小部分 按此思路,總結(jié)為:
每大部分內(nèi)部按行合并,最后將三部分按列合并,并且在排列的時候注意比例。
2. 繪制布局
Step 1:繪制 lay_3456
p3 = p4 = p5 = p6 = lay_new(matrix(1))
lay56 = lay_bind_row(p5, p6, heights = c(1, 1))
lay3456 = lay_bind_col(lay_bind_col(p3, p4, widths = c(2, 4)), lay56, widths = c(6, 3))
lay_show(lay3456)
Step 2:繪制 lay_127并與lay3456合并
lay127 = lay_new(matrix(1:4, nrow = 2), heights = c(6, 3), widths = c(6, 9))
lay_show(lay127)
lay_res = lay_split_field(lay127, lay3456, field = 3)
lay_show(lay_res)
這樣布局就已經(jīng)畫好了,接下來直接填圖就行了。
使用布局畫圖
先畫個簡單的快速看下效果:
pdf("customLayout1.pdf", 13, 9)
lay_set(lay_res)
plot(1:10)
plot(1:10)
plot(1:10)
plot(1:10)
plot(1:10)
plot(1:10)
plot(1:10)
dev.off()
再畫個像樣點的并測試能不能合并ggplot對象:
# test customLayout
library("customLayout")
library("ggplot2")
data = iris
colnames(data) = LETTERS[1:ncol(data)]
# plot lay3456
p3 = p4 = p5 = p6 = lay_new(matrix(1))
lay56 = lay_bind_row(p5, p6, heights = c(1, 1))
lay3456 = lay_bind_col(lay_bind_col(p3, p4, widths = c(2, 4)), lay56, widths = c(6, 3))
lay_show(lay3456)
# plot lay127 and lay_res
lay127 = lay_new(matrix(1:4, nrow = 2), heights = c(6, 3), widths = c(6, 9))
lay_show(lay127)
lay_res = lay_split_field(lay127, lay3456, field = 3)
lay_show(lay_res)
# fill lay
p1 <- ggplot(data, aes(A, B)) + geom_point(colour = "#dd1c77")
p2 <- ggplot(data, aes(A)) + geom_histogram(binwidth = 0.1, colour = "#dd1c77", fill= "#dd1c77")
p3 <- ggplot(data, aes(B)) + geom_density(alpha = 0.2, colour = "#dd1c77", fill = "white", size = 2)
p4 <- ggplot(data, aes(A, B, fill = C)) + geom_tile()
p5 <- ggplot(data[data[, "E"] == "setosa",], aes(y = C)) + geom_boxplot(colour = "#dd1c77", size = 2)
p6 <- ggplot(data[data[, "E"] == "versicolor",], aes(y = C)) + geom_boxplot(colour = "#dd1c77", size = 2)
p7 <- ggplot(data, aes(x = B, y = C)) + geom_line(colour = "#dd1c77", size = 2)
pdf("customLayout2.pdf", 13, 9)
plots2 = lapply(c(1:7), function(x) get(paste0("p", x)))
lay_grid(plots2, lay_res)
dev.off()
另外發(fā)現(xiàn)基礎(chǔ)包的圖和ggplot2的圖不能合并,不過一般也不會用基礎(chǔ)包來畫圖~
-
Layout
+關(guān)注
關(guān)注
15文章
410瀏覽量
62619 -
r語言
+關(guān)注
關(guān)注
1文章
30瀏覽量
6428
發(fā)布評論請先 登錄
相關(guān)推薦
HarmonyOS開發(fā)實戰(zhàn):【親子拼圖游戲】

拼圖游戲之新鳥求教老鳥
Matlab與R語言的區(qū)別
r語言是什么_r語言基礎(chǔ)教程
詳細介紹go語言中的閉包的實現(xiàn)
帶你了解go語言中的閉包
如何用C語言實現(xiàn)拼圖游戲項目
C語言+easyX帶你實現(xiàn):數(shù)字拼圖游戲!
PyBadge和PyBadge LC的滑動拼圖

評論