0

I have 11 plots and used a looping function to plot them see my code below. However, I can't get them to fit in just 1 page or less. The plots are actually too big. I am using R software and writing my work in RMarkdown. I have spent almost an entire week trying to resolve this.

  group_by(Firm_category) %>%
  doo(
    ~ggboxplot(
      data =., x = "Means.type", y = "means",
      fill ="grey", palette = "npg", legend = "none",
      ggtheme = theme_pubr()
      ), 
    result = "plots"
  )
graph3

# Add statistical tests to each corresponding plot

Firm_category <- graph3$Firm_category
xx <- for(i in 1:length(Firm_category)){
  graph3.i <- graph3$plots[[i]] + 
    labs(title = Firm_category[i]) +
    stat_pvalue_manual(stat.test[i, ], label = "p.adj.signif")
  print(graph3.i)
}

#output3.long data sample below as comments
#Firm_category  billmonth  Means.type  means
#Agric          1           Before       38.4444
#Agric          1           After        51.9

Complete data is on my github: https://github.com/Fridahnyakundi/Descriptives-in-R/blob/master/Output3.csv This code prints all the graphs but in like 4 pages. I want to group them into a grid. I have tried to add all these codes below just before my last curly bracket and none is working, please help me out.

library(cowplot)
print(plot_grid(plotlist = graph3.i[1:11], nrow = 4, ncol = 3))

library(ggpubr)
print(ggarrange(graph3.i[1:11], nrow = 4, ncol = 3))

I tried the gridExtra command as well (they all seem to do the same thing). I am the one with a mistake and I guess it has to do with my list. I read a lot of similar work here, some suggested

dev.new() 
dev.off()

I still didn't get what they do. But adding either of them caused my code to stop. I tried defining my 'for' loop function say call it 'XX', then later call it to make a list of graph but it returned NULL output.

I have tried defining an empty list (as I read in some answers here) then counting them to make a list that can be printed but I got so many errors. I have done this for almost 3 days and will appreciate your help in resolving this. Thanks!

Nana
  • 1
  • 1
  • 2
    It could be better if you provide a reproductible exemple .. so that people could just paste your code and run it, and see what's happening .. – MrSmithGoesToWashington Jul 09 '21 at 08:14
  • @MrSmithGoesToWashington The first code is as it is. I am trying to add my data here (output3.long) but let me see if I can edit the question to add data. Thanks for your comment – Nana Jul 09 '21 at 08:43
  • @MrSmithGoesToWashington I have added a link to my data in the question but you can also see it here: https://github.com/Fridahnyakundi/Descriptives-in-R/blob/master/Output3.csv – Nana Jul 09 '21 at 09:12
  • Ok for the data .. you could also have used the dput command ... anyway your code is still not fully reproductible : functions doo, ggboxplot .. and pipeline %>% are not base R .. so before someone answer to your question, he has to guess and search for all the packages you are using ... and there is a lot of chance that he give up before begining to think at your issue .. – MrSmithGoesToWashington Jul 09 '21 at 15:14
  • And also where does come from ```stat.test``` ? – MrSmithGoesToWashington Jul 09 '21 at 15:20

1 Answers1

0

I tried to complete your code ... and this works (but I don't have your 'stat.test' object). Basically, I added a graph3.i <- list() and replaced graph3.i in the loop .. Is it what you wanted to do ?


library(magrittr)
library(dplyr)
library(rstatix)
library(ggplot2)
library(ggpubr)
data <- read.csv(url('http://raw.githubusercontent.com/Fridahnyakundi/Descriptives-in-R/master/Output3.csv'))
graph3 <- data %>% group_by(Firm_category) %>%
  doo(
    ~ggboxplot(
      data =., x = "Means.type", y = "means",
      fill ="grey", palette = "npg", legend = "none",
      ggtheme = theme_pubr()
    ), 
    result = "plots"
  )
graph3

# Add statistical tests to each corresponding plot
graph3.i <- list()
Firm_category <- graph3$Firm_category
xx <- for(i in 1:length(Firm_category)){
  graph3.i[[i]] <- graph3$plots[[i]] + 
    labs(title = Firm_category[i]) # +
    # stat_pvalue_manual(stat.test[i, ], label = "p.adj.signif")
  print(graph3.i)
}


library(cowplot)
print(plot_grid(plotlist = graph3.i[1:11], nrow = 4, ncol = 3))