作者:白介素2
cowplot包
cowplot包是ggplot2的簡(jiǎn)單附加組件。它旨在為ggplot2提供一個(gè)出版物就緒的主題血崭,一個(gè)需要最小量的軸標(biāo)簽尺寸独撇,情節(jié)背景等丽涩。它的主要目的是方便制作符合要求的圖片。除了提供修改的繪圖主題外拄显,此包還提供了對(duì)ggplot2繪圖的自定義注釋的功能苟径。事實(shí)證明,提供此功能的最簡(jiǎn)單方法是在ggplot2之上實(shí)現(xiàn)通用繪圖畫(huà)布躬审。因此棘街,使用此軟件包可以獲得非常不尋常的效果。
舉例演示
如果你覺(jué)得ggplot2的默認(rèn)主題并不好看
library(ggplot2)
ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) +
geom_point(size = 2.5)
cowplot調(diào)整
可以注意到灰色背景去除了,其實(shí)還有些細(xì)微尺寸上的細(xì)微差別
library(cowplot)
ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) +
geom_point(size = 2.5)
更重要的是cowplot的默認(rèn)主題與 save_plot 函數(shù)很好的銜接起來(lái)
這樣輸出的pdf文件不需要再增加其它參數(shù)就已經(jīng)很好了
cowplot中使用save_plot函數(shù)承边,而不是ggsave()
library(cowplot)
plot.mpg <-ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) +
geom_point(size=2.5)
# use save_plot() instead of ggsave() when using cowplot
save_plot("mpg.png", plot.mpg,
base_aspect_ratio = 1.3 # 給legend留出一定的空間
)
plot.mpg
增加網(wǎng)格線
cowplot默認(rèn)狀態(tài)下為無(wú)網(wǎng)格線
background_grid()函數(shù)可以增加背景網(wǎng)格線
plot.mpg+background_grid(major = "xy",minor = "none")
更改主題
theme_set函數(shù)能夠方便的更改主題
theme_set(theme_light())
plot.mpg
cowplot組圖功能
ggplot2的一個(gè)限制是它不容易為標(biāo)題添加標(biāo)簽和其他注釋遭殉。ggplot2嚴(yán)格地將繪圖面板(軸內(nèi)的部分)與繪圖的其余部分分開(kāi),雖然通巢┲可以直接修改其中一個(gè)险污,但我們不能輕易地更改它們。為了以通用方式解決這個(gè)問(wèn)題翔始,cowplot在ggplot2之上實(shí)現(xiàn)了一個(gè)通用的繪圖層罗心。 在此繪圖層中,您可以在圖形頂部添加任意圖形元素城瞎。
在學(xué)術(shù)繪圖中我們經(jīng)巢趁疲可能需要組合多個(gè)圖的情況
假設(shè)我們要組合這兩個(gè)圖
plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) +
geom_point(size=2.5)
plot.mpg
plot.diamonds <- ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar() +
theme(axis.text.x = element_text(angle=70, vjust=0.5))
plot.diamonds
*plot.grid函數(shù)能夠讓我們方便的組合這兩個(gè)圖
*labels參數(shù)指定編號(hào)
*nrow, ncol參數(shù)指定行列有幾個(gè)圖
plot_grid(plot.mpg, plot.diamonds, labels = c("A", "B"))
對(duì)齊坐標(biāo)軸設(shè)置
align參數(shù),h表示 horizontally 水平對(duì)齊脖镀, vertically ("v") 垂直對(duì)齊
plot_grid(plot.mpg, plot.diamonds, labels = c("A", "B"),align = "h")
指定輸出的行列
通常plot_grid會(huì)自動(dòng)合理的輸出飒箭,但也可根據(jù)需要自行指定
plot_grid(plot.mpg, plot.diamonds, labels = c("A", "B"),align="v",nrow = 2)
save_plot函數(shù)輸出文件
plot_grid能夠很好的配合save_plot函數(shù)輸出文件
假設(shè)我們要輸出一個(gè)2*2的文件,注意除在plot_grid文件中指定行列外蜒灰,save_plot也需要指定
plot2by2 <- plot_grid(plot.mpg, NULL, NULL, plot.diamonds,
labels=c("A", "B", "C", "D"), ncol = 2)
save_plot("plot2by2.png", plot2by2,
ncol = 2, # we're saving a grid plot of 2 columns
nrow = 2, # and 2 rows
# each individual subplot should have an aspect ratio of 1.3
base_aspect_ratio = 1.3
)
plot2by2
類的圖形注釋
我們要在之前的圖形上添加圖注
draw_plot_label參數(shù)編號(hào)
draw_label參數(shù):增加文字圖注
ggdraw(plot.mpg) +
draw_plot_label("A", size = 14) +
draw_label("DRAFT!", angle = 45, size = 80, alpha = .2)
給之前的圖添加標(biāo)簽
# plot.mpg and plot.diamonds were defined earlier
library(viridis)
ggdraw() +
draw_plot(plot.diamonds + theme(legend.justification = "bottom"), 0, 0, 1, 1) +
draw_plot(plot.mpg + scale_color_viridis(discrete = TRUE) +
theme(legend.justification = "top"), 0.5, 0.52, 0.5, 0.4) +
draw_plot_label(c("A", "B"), c(0, 0.5), c(1, 0.92), size = 15)
除此以外cowplot還有其它更高級(jí)的功能弦蹂,但是可能用的不多,我們不再詳細(xì)介紹