一、cowplot包
cowplot是一個R包,提供了一組可用於繪製複雜繪圖布局的工具。它的主要目標是簡化多個ggplot2圖形的排版,並提供了額外的標註和注釋功能。cowplot使得組合和渲染高質量的多圖組件成為可能,同時使得不同現有圖形之間庫存游移和調整更加容易。引入cowplot包可以使得繪圖的複雜程度得到顯著降低,從而提高開發效率。
二、cowplot如何添加公共圖例
在多圖拼接中,添加公共的圖例非常重要。cowplot提供了方便且可定製化的方法來添加公共圖例。您需要調用cowplot包的plot_grid()函數並將您要組合的繪圖放入組合中。由於ggplot2返回圖形對象,因此您可以將兩個或更多對象指定給plot_grid()。下面是一個簡單的示例,演示如何將兩個繪製的ggplot2圖形組合併添加一個公共圖例。
library(cowplot)
library(ggplot2)
# Plot1
plot1 <- ggplot(mtcars, aes(x = wt, y = hp, col = factor(cyl))) +
geom_point() +
theme_classic()
# Plot2
plot2 <- ggplot(mtcars, aes(x = wt, y = disp, col = factor(cyl))) +
geom_point() +
theme_classic()
# Add legend to the bottom of the combined plot
combined <- plot_grid(plot1, plot2, nrow = 1, align = "h",
labels = c("A", "B"), label_size = 26)
combined_legend <- get_legend(plot1) # Get legend from plot1
combined <- plot_grid(combined, combined_legend, nrow = 2, rel_heights = c(1, 0.05))
combined
在這個例子中,我們定義了兩個繪圖:plot1和plot2。然後我們將它們組裝成一個組合,然後通過get_legend()函數從plot1中提取圖例。最後,我們將合併後的圖形和圖例再次組合在一起,使它們佔據整個布局的兩個部分。定義的公共圖例會顯示在底部,標註合併後的圖形中線條的含義,使之更加易於理解。
三、cowplot拼圖
在這個例子中,我們建立了一個簡單的橫向拼接的示例,用於演示cowplot中的plot_grid()函數。plot_grid()函數在cowplot中是拼貼圖的骨架函數。它用於將相同大小的ggplot2對象整合到一起。通過使用將注釋信息添加到圖其中的annotate()函數和ggdraw()函數,可以對拼圖進行更密集的自定義。
library(cowplot)
library(ggplot2)
# plot 1 : scatterplot
plot1 <- ggplot(mpg, aes(x = displ, y = hwy, col = class)) +
geom_point() +
theme_classic() +
ggtitle("Scatter Plot")
# plot 2 : bar chart
plot2 <- ggplot(mpg, aes(x=class, y=hwy, fill=class)) +
geom_bar(stat="identity") +
theme_classic() +
ggtitle("Bar Chart")
# plot 3 : histogram
plot3 <- ggplot(mpg, aes(x=hwy)) +
geom_histogram(binwidth = 2, fill='blue', col='white') +
theme_classic()+
ggtitle("Histogram")
# arrange the plots in one view
myplots <- plot_grid(plot1, plot2, plot3, labels=c('A', 'B', 'C'), align='h', ncol=3)
# add metadata to the plot
ggdraw() +
draw_label("My different types of plots!", fontface = 'bold', size = 20,
x = 0, y = 0.9, hjust = 0, vjust = 1) +
draw_label("This is my footnote.", fontface = 'bold', size = 10,
x = 0, y = 0.05, hjust = 0, vjust = 1) +
plot_grid(myplots, labels = c('A', 'B', 'C'), align = 'h', ncol = 3, rel_heights = rep(1, 4),
axis = 'tblr')
在這個示例中,我們定義了三個ggplot對象,並使用plot_grid()函數將它們放在一起。接著,在ggdraw()函數內置元素並將它們添加到拼貼圖中。其中,我們添加並定製了主標題和頁腳。對於拼貼圖的基本初始布局和組裝,可以使用plot_grid()函數,並在需要時使用其他cowplot函數,如annotate()和ggdraw()實現自定義注釋。
四、cowplot公共圖例
我們能輕鬆將一個放在多個圖案,但最難的是將它們放在一起,使它們共用相同的標度,方向、系列標籤等。cowplot提供了一種方法,使得向單個圖案中添加公共圖例變得簡單。下面是一個例子演示如何將一個公共圖例添加到多個ggplot2對象上。
library(cowplot)
library(ggplot2)
# Plot1
Plot1 <- ggplot(mtcars, aes(x = wt, y = hp, col = factor(cyl))) +
geom_point() +
theme_classic() +
ggtitle("Scatter Plot1")
# Plot2
Plot2 <- ggplot(mtcars, aes(x = wt, y = disp, col = factor(cyl))) +
geom_point() +
theme_classic() +
ggtitle("Scatter Plot2")
p_list <- list(Plot1, Plot2)
n <- length(p_list)
# Adding shared legend (to the bottom) and arranging the plot grids
my_legend %
cowplot::add_sub(" ") %>%
cowplot::add_sub("", " ")
}
my_legend <- cowplot::plot_grid(my_legend, ncol = n, align = "h")
p_list <- lapply(seq_along(p_list), function(x) {
cowplot::plot_spacer() + p_list[[x]] +
theme(legend.position = "none") +
theme(plot.margin = unit(c(0, 0, 0.5, 0), "cm")) +
annotation_custom(grid::grobs::grobTree(my_legend),
xmin = -Inf,
xmax = Inf,
ymin = -Inf,
ymax = Inf,
respect = TRUE,
default.units = "native")
})
combined <- cowplot::plot_grid(plotlist = p_list, ncol = 1, align = "h")
combined
在這個例子中,我們定義了兩個繪圖:Plot1和Plot2。然後,我們將它們放在一個共同的圖例下面。通過循環方法使用add_sub()增加很多優化步驟,使字母豎直對齊。然後我們使用plot_spacer()空白圖形將每張圖形包裝起來,並使用annotation_custom()將公共圖例添加到每張圖形中。最後,我們運用plot_grid()函數,將每個已標註並包含了公共圖例的圖形拼接成一個唯一的對象。
五、cowplot與ggpubr
ggpubr是一個R包,提供了一組宏和函數,使得您可以輕鬆製作熱力圖、箱線圖、散點圖等。主要優勢是可以快速方便地插入統計檢驗效果圖,同時也支持對ggplot2繪圖的擴展和註解。Gpubr可以很好地與cowplot包結合使用,使用cowplot包的功能優化樣式和組合單個發布級別的視圖。
# demo data
df <- iris[c(1:5, 51:55, 101:105), ]
# scatterplot matrix with diagonal density plots and correlogram
p <- ggpairs(df, columns = 1:4, ggplot2::aes(color = Species),
diag = list(continuous = wrap('density', alpha = 0.5)),
upper = list(continuous = wrap('points')),
lower = list(continuous = wrap('smooth'))) +
theme_classic() +
theme(legend.position = "bottom")
# Statistical tests
p <- ggpubr::add_y_significance(p, "Sepal.Length", "Sepal.Width", tip_length = 0.01)
# Adjust plot margin
p <- ggplot2::ggplot2::theme(plot.margin = ggplot2::unit(rep(0.1,5), "cm")) + p
# Final plot with cowplot
p <- cowplot::ggdraw() +
cowplot::draw_label("iris dataset scatterplot matrix", fontface = 'bold', size = 20,
x = 0, y = 0.9, hjust = 0, vjust = 1) +
cowplot::ggdraw::draw_label("- Animal 1: Black
- Animal 2: Grey
- Animal 3: Green ",
size = 10, hjust = 0, vjust = 0, fontcolour = 'grey20') +
cowplot::plot_grid(p, labels = c("", ""))
在這個例子中,我們從鳶尾花數據集創建了一個散點圖矩陣,然後運用ggpubr包從中提取一些重要的統計檢驗,並將這些檢驗嵌入到散點圖之中。然後,我們通過運用cowplot包的ggdraw()函數,定製化添加了一些標註,並使用plot_grid()函數將拼圖組合成一張發布級別的可視化。
六、cowplot提取軸標籤
在提取或更改軸標籤等元素時,使用cowplot包提供的功能,可以輕鬆地進行自定義。cowplot包中的get_y_axis_label()和get_x_axis_label()函數可以用於提取軸標籤。
library(ggplot2)
library(cowplot)
# data
df <- diamonds[diamonds$carat < 2, ]
head(df)
# plot 1
p1 <- ggplot(df, aes(carat, price, col = cut)) +
geom_point() +
theme_bw() +
xlab("Carat") +
ylab("Price")
# plot 2 with the rotated y-axis label
p2 <- ggplot(df, aes(carat, price, col = cut)) +
geom_point() +
theme_bw() +
xlab("Carat") +
ylab("Price") +
cowplot::theme(axis.title.y = element_text(angle = 0, vjust = 1))
# Extract y-axis labels from p1 and p2
label_p1 <- cowplot::get_y_axis_label(p1)
label_p2 <- cowplot::get_y_axis_label(p2)
# Invert y-axis label of p2 and check with label of p1
p2_inverted <- p2 + cowplot::theme(axis.title.y = element_text(angle = 180, vjust = 0))
label_p2_inv <- cowplot::get_y_axis_label(p2_inverted)
label_p1
# Output: "Price"
label_p2
# Output: "Price"
label_p2_inv
# Output: "ecirP"
在這個例子中,我們定義了兩個ggplot散點圖,並使用theme()函數的axis.title.y參數實現將y軸標籤旋轉180度。迭代調用get_y_axis_label()函數從兩個圖形中提取y軸標籤。然後,我們通過使用另一個theme()函數的axis.title.y參數將y軸標籤反轉,這通過調用get_y_axis_label()函數時輸出反向標籤作為輸出結果,證明所做的
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/236675.html