ggarrange()
to change the location of the graphs in rows or columns. ggarrange(sp, # ggarrange(bxp, dp, ncol = 2, labels = c("B", "C")), # nrow = 2, labels = "A" # )
ggdraw() + draw_plot() + draw_plot_label()
. ggdraw()
draw_plot(plot, x = 0, y = 0, width = 1, height = 1)
plot
: plot to place (ggplot2 or gtable)x, y
: x / y coordinates of the lower left corner of the graphwidth, height
: the width and height of the graphic draw_plot_label(label, x = 0, y = 1, size = 16, ...)
label
: label
vectorx, y
: vector with x / y coordinates of each label, respectivelysize
: label font size library("cowplot") ggdraw() + draw_plot(bxp, x = 0, y = .5, width = .5, height = .5) + draw_plot(dp, x = .5, y = .5, width = .5, height = .5) + draw_plot(bp, x = 0, y = 0, width = 1, height = 0.5) + draw_plot_label(label = c("A", "B", "C"), size = 15, x = c(0, 0.5, 0), y = c(1, 1, 0.5))
arrangeGrop()
function [in gridExtra ] helps to change the arrangement of graphs in rows or columns. library("gridExtra") grid.arrange(sp, # arrangeGrob(bxp, dp, ncol = 2),# nrow = 2) #
grid.arrange()
you can also use the layout_matrix
argument to create complex relative positioning of graphs.layout_matrix
is a 2x2 matrix (2 rows and 2 columns). The first line - all units, where the first chart occupies two columns; the second line contains graphs 2 and 3, each of which occupies its own column. grid.arrange(bp, # bxp, sp, # ncol = 2, nrow = 2, layout_matrix = rbind(c(1,1), c(2,3)))
grid.arrange()
using the auxiliary function draw_plot_label()
[in cowplot ].grid.arrange()
or arrangeGrob()
functions grid.arrange()
arrangeGrob()
type), you first need to convert them to ggplot type using the as_ggplot()
function [in ggpubr ]. You can then use the draw_plot_label()
function [in cowplot ]. library("gridExtra") library("cowplot") # arrangeGrob # gtable (gt) gt <- arrangeGrob(bp, # bxp, sp, # ncol = 2, nrow = 2, layout_matrix = rbind(c(1,1), c(2,3))) # p <- as_ggplot(gt) + # ggplot draw_plot_label(label = c("A", "B", "C"), size = 15, x = c(0, 0, 0.5), y = c(1, 0.5, 0.5)) # p
arrangeGrob()
instead of grid.arrange()
. The main difference between these two functions is that grid.arrange()
automatically displays ordered graphs. Since we wanted to add an annotation to the graphs before drawing them, it is preferable to use the arrangeGrob()
function in this case.grid.layout()
function. It also provides the viewport()
helper function for specifying a region, or scope. The print()
function is used to place graphs in a given region.grid.newpage()
function library(grid) # grid.newpage() # : nrow = 3, ncol = 2 pushViewport(viewport(layout = grid.layout(nrow = 3, ncol = 2))) # define_region <- function(row, col){ viewport(layout.pos.row = row, layout.pos.col = col) } # print(sp, vp = define_region(row = 1, col = 1:2)) # print(bxp, vp = define_region(row = 2, col = 1)) print(dp, vp = define_region(row = 2, col = 2)) print(bp + rremove("x.text"), vp = define_region(row = 3, col = 1:2))
ggarrange()
function [in ggpubr ] with these arguments:common.legend = TRUE
: make a shared legendlegend
: set the position of the legend. The allowed value is one of c (“top”, “bottom”, “left”, “right”) ggarrange(bxp, dp, labels = c("A", "B"), common.legend = TRUE, legend = "bottom")
# , ("Species") sp <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width", color = "Species", palette = "jco", size = 3, alpha = 0.6)+ border() # x ( ) y ( ) xplot <- ggdensity(iris, "Sepal.Length", fill = "Species", palette = "jco") yplot <- ggdensity(iris, "Sepal.Width", fill = "Species", palette = "jco")+ rotate() # yplot <- yplot + clean_theme() xplot <- xplot + clean_theme() # ggarrange(xplot, NULL, sp, yplot, ncol = 2, nrow = 2, align = "hv", widths = c(2, 1), heights = c(1, 2), common.legend = TRUE)
Source: https://habr.com/ru/post/337598/
All Articles