Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to combine multiple plots in R?

I want to arrange the plot in a specific order.

library(ggpubr)
data("ToothGrowth")
data("mtcars")
mtcars$name <- rownames(mtcars)
mtcars$cyl <- as.factor(mtcars$cyl)

# create boxplot
bxp <- ggboxplot(ToothGrowth, x = "dose", y = "len", color = "dose", palette = "jco")
# create dotplot
dp <- ggdotplot(ToothGrowth, x = "dose", y = "len", color = "dose", palette = "jco", binwidth = 1)

# create scatterplot
sp = ggscatter(mtcars, x = "wt", y = "mpg", add = "reg.line", conf.int = TRUE, color = "cyl", palette = "jco", shape = "cyl")+
  stat_cor(aes(color = cyl), label.x = 3)
# arrange the plots
ggarrange(sp,                                                 # First row with scatter plot
          ggarrange(bxp, dp, ncol = 2, labels = c("B", "C")), # Second row with box and dot plots
          nrow = 2, 
          labels = "A")                                       # Labels of the scatter plot

Arranging the plots this way has given me the output.

outputw

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But, I want the output in another way like scatterplot should be in the down pane and bx and bp to be in top pane. I tried

ggarrange(bxp, dp, ggarrange(sp, labels = c("C"), nrow = 1),
         nrow = 2, ncol = 2, labels=c("A", "B"))

But it gave me like as follows:

output2

The scatterplot is not filled with down pane. It’s in a corner of the pane

>Solution :

Try this.

ggarrange(ggarrange(bxp, dp, 
                    nrow = 1, ncol = 2, labels=c("A", "B")),
          ggarrange(sp, labels = c("C"), nrow = 1, ncol = 1),
          nrow = 2, ncol = 1)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading