cowplot包是ggplot2的簡單附加組件。 它旨在爲ggplot2提供一個出版物就緒的主題,這個主題需要最少量的軸標籤尺寸,情節背景等。對'ggplot2'庫的一些有用的擴展和修改。 特別是,這個軟件包可以很容易地將多個'ggplot2'圖組合成一個並用字母標記它們,例如 A,B,C等,這是科學出版物經常需要的。 該軟件包還提供了一個流線型和乾淨的主題,用於Wilke實驗室,因此包名稱代表Claus O. Wilke的繪圖庫。
library(ggplot2) require(cowplot) plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point() + facet_grid(. ~ Species) + stat_smooth(method = "lm") + background_grid(major = 'y', minor = "none") + panel_border() + labs(title = "dot-line plot") plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + geom_point(size=2.5) + labs(title = "dot plot") plot.diamonds <- ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar() + theme(axis.text.x = element_text(angle=70, vjust=0.5)) + labs(title = "bar plot") ggdraw() + draw_plot(plot.iris, 0, .5, 1, .5) + draw_plot(plot.mpg, 0, 0, .5, .5) + draw_plot(plot.diamonds, .5, 0, .5, .5) + draw_plot_label(c("A", "B", "C"), c(0, 0, 0.5), c(1, 0.5, 0.5), size = 15)
其中 draw_plot(plot.iris, 0, .5, 1, .5)代表的是A圖左下角在座標軸的位置是(0,0.5),然後圖片佔據X軸的1(100%),佔據Y軸的0.5(50%)。
image.png
同樣可以使用grid和gridExtra包達到同樣的結果
library(grid) library(gridExtra) grid.newpage() ###新建圖表版面 grid.text("title of this panel", vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2)) pushViewport(viewport(layout = grid.layout(2,2))) ####將版面分成2*2矩陣 vplayout <- function(x,y){viewport(layout.pos.row = x, layout.pos.col = y)} print(plot.iris, vp = vplayout(1,1:2)) ###將(1,1)和(1,2)的位置畫圖plot.iris print(plot.mpg, vp = vplayout(2,1)) ###將(2,1)的位置畫圖plot.mpg print(plot.diamonds , vp = vplayout(2,2)) ###將(2,2)的位置畫圖plot.diamonds grid.arrange( arrangeGrob(plot.iris,left="A"), arrangeGrob(plot.mpg, left="B"), arrangeGrob(plot.diamonds, left="C"), layout_matrix = matrix(c(1,1,2,3), ncol=2, byrow=TRUE), top = "Title",left = "This is my global Y-axis title")
image.png