par()
and layout()
- cannot be used to place several ggplot2 graphs on one illustration.grid.arrange()
and arrangeGrob()
to combine several ggplot graphs into onemarrangeGrob()
to place several ggplot graphs on several illustrationsplot_grid()
with the align argument. But this package, in turn, does not contain a solution for placing several graphs in different illustrations. To do this, we will use the ggarrange()
function [in ggpubr ], a wrapper over the plot_grid()
function, which can organize graphs in several illustrations. It will also help create a common legend for several graphs. if(!require(devtools)) install.packages("devtools") devtools::install_github("kassambara/ggpubr")
install.packages("ggpubr")
library(ggpubr)
# ToothGrowth data("ToothGrowth") head(ToothGrowth) len supp dose 1 4.2 VC 0.5 2 11.5 VC 0.5 3 7.3 VC 0.5 4 5.8 VC 0.5 5 6.4 VC 0.5 6 10.0 VC 0.5 # mtcars data("mtcars") mtcars$name <- rownames(mtcars) mtcars$cyl <- as.factor(mtcars$cyl) head(mtcars[, c("name", "wt", "mpg", "cyl")]) name wt mpg cyl Mazda RX4 Mazda RX4 2.620 21.0 6 Mazda RX4 Wag Mazda RX4 Wag 2.875 21.0 6 Datsun 710 Datsun 710 2.320 22.8 4 Hornet 4 Drive Hornet 4 Drive 3.215 21.4 6 Hornet Sportabout Hornet Sportabout 3.440 18.7 8 Valiant Valiant 3.460 18.1 6
# (bp) bxp <- ggboxplot(ToothGrowth, x = "dose", y = "len", color = "dose", palette = "jco") bxp # (dp) dp <- ggdotplot(ToothGrowth, x = "dose", y = "len", color = "dose", palette = "jco", binwidth = 1) dp
# (bp) bp <- ggbarplot(mtcars, x = "name", y = "mpg", fill = "cyl", # cyl color = "white", # palette = "jco", # jco - journal color palett ( ). . ?ggpar sort.val = "asc", # sort.by.groups = TRUE, # x.text.angle = 90 # ) bp + font("x.text", size = 8) # (sp) sp <- ggscatter(mtcars, x = "wt", y = "mpg", add = "reg.line", # conf.int = TRUE, # color = "cyl", palette = "jco", # "cyl" shape = "cyl" # "cyl" )+ stat_cor(aes(color = cyl), label.x = 3) # sp
ggarrange()
[in ggpubr ], a wrapper over the plot_grid()
function [in the cowplot package]. Compared to the standard plot_grid()
function, ggarrange()
can place several graphs in several illustrations. ggarrange(bxp, dp, bp + rremove("x.text"), labels = c("A", "B", "C"), ncol = 2, nrow = 2)
plot_grid()
function [in cowplot ]: library("cowplot") plot_grid(bxp, dp, bp + rremove("x.text"), labels = c("A", "B", "C"), ncol = 2, nrow = 2)
grid.arrange()
function [in gridExtra ]: library("gridExtra") grid.arrange(bxp, dp, bp + rremove("x.text"), ncol = 2, nrow = 2)
annotate_figure()
[in ggpubr ]: figure <- ggarrange(sp, bp + font("x.text", size = 10), ncol = 1, nrow = 2) annotate_figure(figure, top = text_grob("Visualizing mpg", color = "red", face = "bold", size = 14), bottom = text_grob("Data source: \n mtcars data set", color = "blue", hjust = 1, x = 1, face = "italic", size = 10), left = text_grob("Figure arranged using ggpubr", color = "green", rot = 90), right = "I'm done, thanks :-)!", fig.lab = "Figure 1", fig.lab.face = "bold" )
annotate_figure()
function supports any ggplot graphics.install.packages(“survminer”)
), and then do the following: # library(survival) fit <- survfit( Surv(time, status) ~ adhere, data = colon ) # library(survminer) ggsurv <- ggsurvplot(fit, data = colon, palette = "jco", # jco pval = TRUE, pval.coord = c(500, 0.4), # p- risk.table = TRUE # ) names(ggsurv) [1] "plot" "table" "data.survplot" "data.survtable"
ggarrange(ggsurv$plot, ggsurv$table, heights = c(2, 0.7), ncol = 1, nrow = 2)
ggarrange(ggsurv$plot, ggsurv$table, heights = c(2, 0.7), ncol = 1, nrow = 2, align = "v")
Source: https://habr.com/ru/post/336492/
All Articles