歡迎來到醫(yī)科研,這里是白介素2的讀書筆記,跟我一起聊臨床與科研的故事, 生物醫(yī)學(xué)數(shù)據(jù)挖掘,R語言,TCGA、GEO數(shù)據(jù)挖掘。
cowplot包
cowplot包是ggplot2的簡單附加組件。它旨在為ggplot2提供一個出版物就緒的主題,一個需要最小量的軸標簽尺寸,情節(jié)背景等。它的主要目的是方便制作符合要求的圖片。除了提供修改的繪圖主題外,此包還提供了對ggplot2繪圖的自定義注釋的功能。 事實證明,提供此功能的最簡單方法是在ggplot2之上實現(xiàn)通用繪圖畫布。因此,使用此軟件包可以獲得非常不尋常的效果。
舉例演示 如果你覺得ggplot2的默認主題并不好看
1library(ggplot2) 2ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + 3 geom_point(size = 2.5)
image.pngcowplot調(diào)整 可以注意到灰色背景去除了,其實還有些細微尺寸上的細微差別
1library(cowplot) 2ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + 3 geom_point(size = 2.5)
image.png更重要的是cowplot的默認主題與 save_plot 函數(shù)很好的銜接起來,這樣輸出的pdf文件不需要再增加其它參數(shù)就已經(jīng)很好了
1library(cowplot) 2plot.mpg <-ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + 3 geom_point(size=2.5) 4# use save_plot() instead of ggsave() when using cowplot 5save_plot("mpg.png", plot.mpg, 6 base_aspect_ratio = 1.3 # 給legend留出一定的空間 7) 8plot.mpg 9
image.png增加網(wǎng)格線 cowplot默認狀態(tài)下為無網(wǎng)格線 background_grid()函數(shù)可以增加背景網(wǎng)格線
1plot.mpg+background_grid(major = "xy",minor = "none")
image.png更改主題 theme_set函數(shù)能夠方便的更改主題
1theme_set(theme_light()) 2plot.mpg
image.pngcowplot組圖功能 ggplot2的一個限制是它不容易為標題添加標簽和其他注釋。ggplot2嚴格地將繪圖面板(軸內(nèi)的部分)與繪圖的其余部分分開,雖然通??梢灾苯有薷钠渲幸粋€,但我們不能輕易地更改它們。為了以通用方式解決這個問題,cowplot在ggplot2之上實現(xiàn)了一個通用的繪圖層。在此繪圖層中,您可以在圖形頂部添加任意圖形元素。 在學(xué)術(shù)繪圖中我們經(jīng)??赡苄枰M合多個圖的情況
假設(shè)我們要組合這兩個圖 1plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + 2 geom_point(size=2.5) 3plot.mpg 4plot.diamonds <- ggplot(diamonds, aes(clarity, fill = cut)) + geom_bar() + 5 theme(axis.text.x = element_text(angle=70, vjust=0.5)) 6plot.diamonds 7
image.png
image.png1plot_grid(plot.mpg, plot.diamonds, labels = c("A", "B"))
image.png對齊坐標軸設(shè)置 align參數(shù),h表示 horizontally 水平對齊, vertically ("v") 垂直對齊
1plot_grid(plot.mpg, plot.diamonds, labels = c("A", "B"),align = "h")
image.png指定輸出的行列 通常plot_grid會自動合理的輸出,但也可根據(jù)需要自行指定
1plot_grid(plot.mpg, plot.diamonds, labels = c("A", "B"),align="v",nrow = 2)
image.pngsave_plot函數(shù)輸出文件 plot_grid能夠很好的配合save_plot函數(shù)輸出文件 假設(shè)我們要輸出一個2*2的文件,注意除在plot_grid文件中指定行列外,save_plot也需要指定
1plot2by2 <- plot_grid(plot.mpg, NULL, NULL, plot.diamonds, 2 labels=c("A", "B", "C", "D"), ncol = 2) 3save_plot("plot2by2.png", plot2by2, 4 ncol = 2, # we're saving a grid plot of 2 columns 5 nrow = 2, # and 2 rows 6 # each individual subplot should have an aspect ratio of 1.3 7 base_aspect_ratio = 1.3 8 ) 9plot2by2
image.png類的圖形注釋 我們要在之前的圖形上添加圖注 draw_plot_label參數(shù)編號 draw_label參數(shù):增加文字圖注
1ggdraw(plot.mpg) + 2 draw_plot_label("A", size = 14) + 3 draw_label("DRAFT!", angle = 45, size = 80, alpha = .2)
image.png給之前的圖添加標簽
1# plot.mpg and plot.diamonds were defined earlier 2library(viridis) 3ggdraw() + 4 draw_plot(plot.diamonds + theme(legend.justification = "bottom"), 0, 0, 1, 1) + 5 draw_plot(plot.mpg + scale_color_viridis(discrete = TRUE) + 6 theme(legend.justification = "top"), 0.5, 0.52, 0.5, 0.4) + 7 draw_plot_label(c("A", "B"), c(0, 0.5), c(1, 0.92), size = 15)
image.png 除此以外cowplot還有其它更高級的功能,但是可能用的不多,我們不再詳細介紹參考源代碼鏈接(https://github.com/wilkelab/cowplot)
|