Saving ggplot as object not list

I am trying to save several bar charts in RStudio so that I can use ggarrange() to format them for a publication. Below I have the code for one of the plots. If I don't assign the plot to "b" it makes the plot without issue. But then I assign this to a1 (a1<-ggplot...) it saves as a "List of 9" and then I try to see the plot by typing r print(a1) and it says "Error in default[missing] : object of type 'closure' is not subsettable". I'm not sure how to assign it as an object so that I can use ggarrange() to form a plot with several plots.

Here is an update with a sample data set. As I said, if I don't try to assign the plot to anything I can get a plot. But if I try to assign it to a1 it provides the following error.

Code:

> dput(head(organic_acids_total_merged_numeric))
structure(list(Sample = c("Aer_24hr", "Aer_24hr", "Aer_24hr", 
"Aer_24hr", "Aer_24hr", "Aer_24hr"), Acid = c("Acetic Acid", 
"Butyric Acid", "Formic Acid", "Isobutyric Acid", "Isovaleric Acid", 
"Lactic Acid"), Percent = c(0.08, 0.02, 0.02, 0, 0, 0.04), stdev = c(0.07, 
0.02, 0.02, 0, 0, 0.04)), row.names = c(NA, 6L), class = "data.frame")

library(ggplot2)

a1<-ggplot(organic_acids_total_merged_numeric,aes(x=factor(Sample,level=c("Aer_24hr","Aer_28day","Ana_8hr")),y=Percent,fill=factor(Acid,level=c("Acetic Acid",
                                                                                                                                             'Butyric Acid',
                                                                                                                                             'Formic Acid'))))+
  geom_col(position=position_dodge())+
  labs(title="Organic Acid Profile",y="% of Dry Matter by Mass",x='Treatment Group',fill='Organic Acid')+
  scale_x_discrete(guide=guide_axis(angle=90))+
  geom_errorbar(aes(ymin=Percent-stdev,
                    ymax=Percent+stdev),
                position=position_dodge())+
  scale_fill_brewer(palette="Set3")+
  theme_classic()+
  theme(plot.title=element_text(size=20,hjust=0.5),
        axis.text.x=element_text(size=20),axis.title=element_text(size=20),
        axis.text.y=element_text(size=15),legend.text=element_text(size=12))

a1

error:

> a1
Error in default[missing] : object of type 'closure' is not subsettable

Your code is not reproducible... so I can't help you directly.

Here is a simple example of what you seem to be trying to do based only on your statements (not your code)

library(tidyverse)
library(ggpubr)

p1 <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
p2 <- ggplot(mtcars, aes(mpg, wt)) + geom_point()


ggarrange(p1,p2,labels = 1:2)

Taking a peek at your code, in general I would avoid using single letters as object names, not least because c() is a commonly used function in R, and I like to avoid name collisions.

If you are having trouble with your specific code and would like help, then please provide a reprex.

Sorry for the confusion, here is an update with a sample data set and I'll update my post as well. As I said, if I don't try to assign the plot to anything I can get a plot. But if I try to assign it to a1 it provides the following error.

Code:

organic_acids_total_merged_numeric(
  Sample=c(Aer_24hr,Aer28_day,Ana_8hr),
  Acid=c(Acetic Acid,Butyric Acid,Formic Acid),
  Percent=(0.08,0.02,0.02),
  stdev(0.07,0.02,0.02)
)

library(ggplot2)

a1<-ggplot(organic_acids_total_merged_numeric,aes(x=factor(Sample,level=c("Aer_24hr","Aer_28day","Ana_8hr")),y=Percent,fill=factor(Acid,level=c("Acetic Acid",
                                                                                                                                             'Butyric Acid',
                                                                                                                                             'Formic Acid'))))+
  geom_col(position=position_dodge())+
  labs(title="Organic Acid Profile",y="% of Dry Matter by Mass",x='Treatment Group',fill='Organic Acid')+
  scale_x_discrete(guide=guide_axis(angle=90))+
  geom_errorbar(aes(ymin=Percent-stdev,
                    ymax=Percent+stdev),
                position=position_dodge())+
  scale_fill_brewer(palette="Set3")+
  theme_classic()+
  theme(plot.title=element_text(size=20,hjust=0.5),
        axis.text.x=element_text(size=20),axis.title=element_text(size=20),
        axis.text.y=element_text(size=15),legend.text=element_text(size=12))

a1

error:

> a1
Error in default[missing] : object of type 'closure' is not subsettable

Are you sure you arent getting errors before you get to ggplot because...

organic_acids_total_merged_numeric(
  Sample=c(Aer_24hr,Aer28_day,Ana_8hr),
  Acid=c(Acetic Acid,Butyric Acid,Formic Acid),
  Percent=(0.08,0.02,0.02),
  stdev(0.07,0.02,0.02)
)

Does not define a data structure and the components are largely undefined.

Sorry. I may not have formatted this correctly for the reprex. Here's the data frame again. But yes, it all works for ggplot just fine.

> dput(head(organic_acids_total_merged_numeric))
structure(list(Sample = c("Aer_24hr", "Aer_24hr", "Aer_24hr", 
"Aer_24hr", "Aer_24hr", "Aer_24hr"), Acid = c("Acetic Acid", 
"Butyric Acid", "Formic Acid", "Isobutyric Acid", "Isovaleric Acid", 
"Lactic Acid"), Percent = c(0.08, 0.02, 0.02, 0, 0, 0.04), stdev = c(0.07, 
0.02, 0.02, 0, 0, 0.04)), row.names = c(NA, 6L), class = "data.frame")

your code worked fine, and produced the following image

I got no error message or warning of any kind, I'm using ggplot2 version 3.3.6

Like I said, I can make a plot just fine. But when I try to assign it to something like a1:

a1<-ggplot(...)

It doesn't assign as an object. So when I go forward and try to print it or use it in a function like ggarrange() I get the error

> a1
Error in default[missing] : object of type 'closure' is not subsettable

but it does assign as an object, at least in the reprex you provided me.

Here is all the code and the result.

organic_acids_total_merged_numeric <-structure(list(Sample = c("Aer_24hr", "Aer_24hr", "Aer_24hr", 
                          "Aer_24hr", "Aer_24hr", "Aer_24hr"), Acid = c("Acetic Acid", 
                                                                        "Butyric Acid", "Formic Acid", "Isobutyric Acid", "Isovaleric Acid", 
                                                                        "Lactic Acid"), Percent = c(0.08, 0.02, 0.02, 0, 0, 0.04), stdev = c(0.07, 
                                                                                                                                             0.02, 0.02, 0, 0, 0.04)), row.names = c(NA, 6L), class = "data.frame")


library(ggpubr)

a1<-ggplot(organic_acids_total_merged_numeric,aes(x=factor(Sample,level=c("Aer_24hr","Aer_28day","Ana_8hr")),y=Percent,fill=factor(Acid,level=c("Acetic Acid",
                                                                                                                                                'Butyric Acid',
                                                                                                                                                'Formic Acid'))))+
  geom_col(position=position_dodge())+
  labs(title="Organic Acid Profile",y="% of Dry Matter by Mass",x='Treatment Group',fill='Organic Acid')+
  scale_x_discrete(guide=guide_axis(angle=90))+
  geom_errorbar(aes(ymin=Percent-stdev,
                    ymax=Percent+stdev),
                position=position_dodge())+
  scale_fill_brewer(palette="Set3")+
  theme_classic()+
  theme(plot.title=element_text(size=20,hjust=0.5),
        axis.text.x=element_text(size=20),axis.title=element_text(size=20),
        axis.text.y=element_text(size=15),legend.text=element_text(size=12))

ggarrange(a1,a1,labels = 1:2)

I'm using R 4.1.2
with the following packages

> packageVersion("ggpubr")
[1] ‘0.4.0’
> packageVersion("ggplot2")
[1] ‘3.3.6’
ggarrange(d1,e1,c1,labels="a","b","c")

Error in default[missing] : object of type 'closure' is not subsettable

I did the d1, e1 and c1 plots the exact same and I'm still getting the error

What happens whe you run, not your own code, but the reprex code exactly as i wrote it back to you ? Error or no error?

Here the labels code seems suspect; missing c() wrap

I tried your code and I get the same error. I also tried the c() in the labels and that didn't work. I don't know if this will help, but below is the full code I'm using. I was trying to avoid inputing everything as I know it is long, but maybe it'll help.


# 1) RSTUDIO PACKAGE INSTALLATION ################################################

library(janitor)
library(tidyverse)
library(ggplot2)
library(readr)
library(dplyr)
library(rlang)
library(plotrix)
library(RColorBrewer)
library(ggpubr)

# 2) IMPORT THE DATA #############################################################
organic_acids<-read.csv(file='Oct19_Queue_organic acid summary8.csv') 
dput(organic_acids)

# view(organic_acids)

colnames(organic_acids)<-gsub("ï..","",colnames(organic_acids))
colnames(organic_acids)<-gsub("X..","",colnames(organic_acids))
colnames(organic_acids)<-gsub("..db.","",colnames(organic_acids))

# view(organic_acids)

# 3) TOTAL OA GRAPH ##############################################################
## A) Creating average data.frame ################################################

organic_acids_total_avg<-organic_acids %>% 
  select(Sample_overview,SA_avg,LA_avg,FA_avg,AA_avg,PA_avg,IBA_avg,BA_avg,IVA_avg,
         VA_avg,OA_total_avg) #select avg columns

# view(organic_acids_total_avg)

organic_acids_total_avg_na<-organic_acids_total_avg %>%
  na_if("") %>% 
  na.omit


# view(organic_acids_total_avg_na)

organic_acids_total_avg_na<-organic_acids_total_avg_na %>% 
  rename('Sample'='Sample_overview','Succinic Acid'='SA_avg',
         'Lactic Acid'='LA_avg','Formic Acid'='FA_avg','Acetic Acid'='AA_avg',
         'Propionic Acid'='PA_avg','Isobutyric Acid'='IBA_avg',
         'Butyric Acid'='BA_avg','Isovaleric Acid'='IVA_avg',
         'Valeric Acid'='VA_avg','Total Organic Acid'='OA_total_avg') # rename columns


# view(organic_acids_total_avg_na)


## B) Creating stdev data.frame ################################################

organic_acids_total_stdev<-organic_acids %>% 
  select(Sample_overview,SA_stdev,LA_stdev,FA_stdev,AA_stdev,PA_stdev,IBA_stdev,
         BA_stdev,IVA_stdev,VA_stdev,OA_total_stdev) # select Stdev columns

# view(organic_acids_total_stdev)

organic_acids_total_stdev_na<-organic_acids_total_stdev %>% 
  na_if("") %>% 
  na.omit # remove rows with NA or zeroes

# view(organic_acids_total_stdev_na)

organic_acids_total_stdev_na<-organic_acids_total_stdev_na %>% 
  rename('Sample'='Sample_overview','Succinic Acid'='SA_stdev',
         'Lactic Acid'='LA_stdev','Formic Acid'='FA_stdev','Acetic Acid'='AA_stdev',
         'Propionic Acid'='PA_stdev','Isobutyric Acid'='IBA_stdev',
         'Butyric Acid'='BA_stdev','Isovaleric Acid'='IVA_stdev',
         'Valeric Acid'='VA_stdev','Total Organic Acid'='OA_total_stdev') # Rename columns


# view(organic_acids_total_stdev_na)

## C) Changing data.frame dimensions to longer data frame #######################

organic_acids_total_avg2<-organic_acids_total_avg_na %>% 
  pivot_longer(cols="Succinic Acid":"Total Organic Acid",
               names_to="Acid",
               values_to="Percent")

# view(organic_acids_total_avg2)

organic_acids_total_stdev2<-organic_acids_total_stdev_na %>% 
  pivot_longer(cols="Succinic Acid":"Total Organic Acid",
               names_to="Acid",
               values_to="stdev")

# view(organic_acids_total_stdev2)

## D) Merging data.frames ########################################################

organic_acids_total_merged<-merge(organic_acids_total_avg2,organic_acids_total_stdev2,by=c("Sample","Acid"))

# view(organic_acids_total_merged)
# 
# summary(organic_acids_total_merged)

organic_acids_total_merged_numeric<-transform(organic_acids_total_merged,Percent=as.numeric(sub("%","",Percent)))

organic_acids_total_merged_numeric<-transform(organic_acids_total_merged_numeric,stdev=as.numeric(sub("%","",stdev)))

# summary(organic_acids_total_merged_numeric)
# view(organic_acids_total_merged_numeric)

# class(organic_acids_total_merged_numeric$Percent)
# class(organic_acids_total_merged_numeric$stdev)

## E) Plotting data ###############################################################

a1<-ggplot(organic_acids_total_merged_numeric,aes(x=factor(Sample,level=c("Initial biomass","Initial biomass with CA",
                                                                "Aer_4hr","Aer_8hr","Aer_24hr",
                                                                "Aer_28day","Ana_4hr","Ana_8hr",
                                                                "Ana_24hr","Ana_28day","CA_4hr",
                                                                "CA_8hr","CA_24hr","CA_28day")),y=Percent,fill=factor(Acid,level=c("Acetic Acid",
                                                                                                                                   'Butyric Acid',
                                                                                                                                   'Formic Acid',
                                                                                                                                   'Isobutyric Acid',
                                                                                                                                   'Isovaleric Acid',
                                                                                                                                   'Lactic Acid',
                                                                                                                          'Propionic Acid',
                                                                                                                                   'Succinic Acid',
                                                                                                                                   'Valeric Acid',
                                                                                                                                   'Total Organic Acid'))))+
  geom_col(position=position_dodge())+
  labs(title="Organic Acid Profile",y="% of Dry Matter by Mass",x='Treatment Group',fill='Organic Acid')+
  scale_x_discrete(guide=guide_axis(angle=90))+
  geom_errorbar(aes(ymin=Percent-stdev,
                    ymax=Percent+stdev),
                position=position_dodge())+
  scale_fill_brewer(palette="Set3")+
  theme_classic()+
  theme(plot.title=element_text(size=20,hjust=0.5),
        axis.text.x=element_text(size=20),axis.title=element_text(size=20),
        axis.text.y=element_text(size=15),legend.text=element_text(size=12))


# ggsave("total_OA_profile.png",h=8,w=12)


# 4) ALL ORGANIC ACIDS WITHOUT TOTAL ##########################################

## A) Creating average data.frame ################################################

organic_acids_no_t_avg<-organic_acids %>% 
  select(Sample_overview,SA_avg,LA_avg,FA_avg,AA_avg,PA_avg,IBA_avg,BA_avg,IVA_avg,
         VA_avg) #select avg columns

# view(organic_acids_no_t_avg)

organic_acids_no_t_avg_na<-organic_acids_no_t_avg %>%
  na_if("") %>% 
  na.omit


# view(organic_acids_no_t_avg_na)

organic_acids_no_t_avg_na<-organic_acids_no_t_avg_na %>% 
  rename('Sample'='Sample_overview','Succinic Acid'='SA_avg',
         'Lactic Acid'='LA_avg','Formic Acid'='FA_avg','Acetic Acid'='AA_avg',
         'Propionic Acid'='PA_avg','Isobutyric Acid'='IBA_avg',
         'Butyric Acid'='BA_avg','Isovaleric Acid'='IVA_avg',
         'Valeric Acid'='VA_avg') # rename columns

# view(organic_acids_no_t_avg_na)


## B) Creating stdev data.frame ################################################

organic_acids_no_t_stdev<-organic_acids %>% 
  select(Sample_overview,SA_stdev,LA_stdev,FA_stdev,AA_stdev,PA_stdev,IBA_stdev,
         BA_stdev,IVA_stdev,VA_stdev) # select Stdev columns

# view(organic_acids_no_t_stdev)

organic_acids_no_t_stdev_na<-organic_acids_no_t_stdev %>% 
  na_if("") %>% 
  na.omit # remove rows with NA or zeroes

# view(organic_acids_no_t_stdev_na)

organic_acids_no_t_stdev_na<-organic_acids_no_t_stdev_na %>% 
  rename('Sample'='Sample_overview','Succinic Acid'='SA_stdev',
         'Lactic Acid'='LA_stdev','Formic Acid'='FA_stdev','Acetic Acid'='AA_stdev',
         'Propionic Acid'='PA_stdev','Isobutyric Acid'='IBA_stdev',
         'Butyric Acid'='BA_stdev','Isovaleric Acid'='IVA_stdev',
         'Valeric Acid'='VA_stdev') # Rename columns

# view(organic_acids_no_t_stdev_na)

## C) Changing data.frame dimensions to longer data frame #######################

organic_acids_no_t_avg2<-organic_acids_no_t_avg_na %>% 
  pivot_longer(cols="Succinic Acid":"Valeric Acid",
               names_to="Acid",
               values_to="Percent")

# view(organic_acids_no_t_avg2)

organic_acids_no_t_stdev2<-organic_acids_no_t_stdev_na %>% 
  pivot_longer(cols="Succinic Acid":"Valeric Acid",
               names_to="Acid",
               values_to="stdev")

# view(organic_acids_no_t_stdev2)

## D) Merging data.frames ########################################################

organic_acids_no_t_merged<-merge(organic_acids_no_t_avg2,organic_acids_no_t_stdev2,by=c("Sample","Acid"))

# view(organic_acids_no_t_merged)
# 
# summary(organic_acids_no_t_merged)

organic_acids_no_t_merged_numeric<-transform(organic_acids_no_t_merged,Percent=as.numeric(sub("%","",Percent)))

organic_acids_no_t_merged_numeric<-transform(organic_acids_no_t_merged_numeric,stdev=as.numeric(sub("%","",stdev)))

# summary(organic_acids_no_t_merged_numeric)
# view(organic_acids_no_t_merged_numeric)

# class(organic_acids_no_t_merged_numeric$Percent)
# class(organic_acids_no_t_merged_numeric$stdev)

## E) Plotting data ###############################################################


b1<-ggplot(organic_acids_no_t_merged_numeric,aes(x=factor(Sample,level=c("Initial biomass","Initial biomass with CA",
                                                                      "Aer_4hr","Aer_8hr","Aer_24hr",
                                                                      "Aer_28day","Ana_4hr","Ana_8hr",
                                                                      "Ana_24hr","Ana_28day","CA_4hr",
                                                                      "CA_8hr","CA_24hr","CA_28day")),y=Percent,fill=factor(Acid,level=c("Acetic Acid",
                                                                                                                                         'Butyric Acid',
                                                                                                                                         'Formic Acid',
                                                                                                                                         'Isobutyric Acid',
                                                                                                                                         'Isovaleric Acid',
                                                                                                                                         'Lactic Acid',
                                                                                                                                         'Propionic Acid',
                                                                                                                                         'Succinic Acid',
                                                                                                                                         'Valeric Acid'))))+
  geom_col(position=position_dodge())+
  labs(title="Organic Acid Profile",y="% of Dry Matter by Mass",x='Treatment Group',fill='Organic Acid')+
  scale_x_discrete(guide=guide_axis(angle=90))+
  geom_errorbar(aes(ymin=Percent-stdev,
                    ymax=Percent+stdev),
                position=position_dodge())+
  scale_fill_brewer(palette="Set3")+
  theme_classic()+
  theme(plot.title=element_text(size=15,hjust=0.5),
        axis.text.x=element_text(size=15),axis.title=element_text(size=15),
        axis.text.y=element_text(size=10),legend.text=element_text(size=10))

# ggsave("organic_acid_no_total.png",h=8,w=12)

# 5) ORGANIC ACIDS WITHOUT SUCCINIC ###########################################

## A) Creating average data.frame ################################################

organic_acids_no_sa_avg<-organic_acids %>% 
  select(Sample_overview,LA_avg,FA_avg,AA_avg,PA_avg,IBA_avg,BA_avg,IVA_avg,
         VA_avg) #select avg columns

# view(organic_acids_no_sa_avg)

organic_acids_no_sa_avg_na<-organic_acids_no_sa_avg %>%
  na_if("") %>% 
  na.omit


# view(organic_acids_no_sa_avg_na)

organic_acids_no_sa_avg_na<-organic_acids_no_sa_avg_na %>% 
  rename('Sample'='Sample_overview',
         'Lactic Acid'='LA_avg','Formic Acid'='FA_avg','Acetic Acid'='AA_avg',
         'Propionic Acid'='PA_avg','Isobutyric Acid'='IBA_avg',
         'Butyric Acid'='BA_avg','Isovaleric Acid'='IVA_avg',
         'Valeric Acid'='VA_avg') # rename columns

# view(organic_acids_no_sa_avg_na)


## B) Creating stdev data.frame ################################################

organic_acids_no_sa_stdev<-organic_acids %>% 
  select(Sample_overview,LA_stdev,FA_stdev,AA_stdev,PA_stdev,IBA_stdev,
         BA_stdev,IVA_stdev,VA_stdev) # select Stdev columns

# view(organic_acids_no_sa_stdev)

organic_acids_no_sa_stdev_na<-organic_acids_no_sa_stdev %>% 
  na_if("") %>% 
  na.omit # remove rows with NA or zeroes

# view(organic_acids_no_sa_stdev_na)

organic_acids_no_sa_stdev_na<-organic_acids_no_sa_stdev_na %>% 
  rename('Sample'='Sample_overview',
         'Lactic Acid'='LA_stdev','Formic Acid'='FA_stdev','Acetic Acid'='AA_stdev',
         'Propionic Acid'='PA_stdev','Isobutyric Acid'='IBA_stdev',
         'Butyric Acid'='BA_stdev','Isovaleric Acid'='IVA_stdev',
         'Valeric Acid'='VA_stdev') # Rename columns

# view(organic_acids_no_sa_stdev_na)

## C) Changing data.frame dimensions to longer data frame #######################

organic_acids_no_sa_avg2<-organic_acids_no_sa_avg_na %>% 
  pivot_longer(cols="Lactic Acid":"Valeric Acid",
               names_to="Acid",
               values_to="Percent")

# view(organic_acids_no_sa_avg2)

organic_acids_no_sa_stdev2<-organic_acids_no_sa_stdev_na %>% 
  pivot_longer(cols="Lactic Acid":"Valeric Acid",
               names_to="Acid",
               values_to="stdev")

# view(organic_acids_no_sa_stdev2)

## D) Merging data.frames ########################################################

organic_acids_no_sa_merged<-merge(organic_acids_no_sa_avg2,organic_acids_no_sa_stdev2,by=c("Sample","Acid"))

# view(organic_acids_no_sa_merged)
# 
# summary(organic_acids_no_sa_merged)

organic_acids_no_sa_merged_numeric<-transform(organic_acids_no_sa_merged,Percent=as.numeric(sub("%","",Percent)))

organic_acids_no_sa_merged_numeric<-transform(organic_acids_no_sa_merged_numeric,stdev=as.numeric(sub("%","",stdev)))

# summary(organic_acids_no_sa_merged_numeric)
# view(organic_acids_no_sa_merged_numeric)

# class(organic_acids_no_sa_merged_numeric$Percent)
# class(organic_acids_no_sa_merged_numeric$stdev)

## E) Plotting data ###############################################################


c1<-ggplot(organic_acids_no_sa_merged_numeric,aes(x=factor(Sample,level=c("Initial biomass","Initial biomass with CA",
                                                                     "Aer_4hr","Aer_8hr","Aer_24hr",
                                                                     "Aer_28day","Ana_4hr","Ana_8hr",
                                                                     "Ana_24hr","Ana_28day","CA_4hr",
                                                                     "CA_8hr","CA_24hr","CA_28day")),y=Percent,fill=factor(Acid,level=c("Acetic Acid",
                                                                                                                                        'Butyric Acid',
                                                                                                                                        'Formic Acid',
                                                                                                                                        'Isobutyric Acid',
                                                                                                                                        'Isovaleric Acid',
                                                                                                                                        'Lactic Acid',
                                                                                                                                        'Propionic Acid',
                                                                                                                                        'Valeric Acid'))))+
  geom_col(position=position_dodge())+
  labs(title="Organic Acid Profile",y="% of Dry Matter by Mass",x='Treatment Group',fill='Organic Acid')+
  scale_x_discrete(guide=guide_axis(angle=90))+
  geom_errorbar(aes(ymin=Percent-stdev,
                    ymax=Percent+stdev),
                position=position_dodge())+
  scale_fill_brewer(palette="Set3")+
  theme_classic()+
  theme(plot.title=element_text(size=15,hjust=0.5),
        axis.text.x=element_text(size=15),axis.title=element_text(size=15),
        axis.text.y=element_text(size=10),legend.text=element_text(size=10))

# ggsave("organic_acid_no_sa.png",h=5,w=10)

# 6) ONLY SUCCINIC ACID #######################################################

## A) Creating average data.frame ################################################

organic_acids_sa_only_avg<-organic_acids %>% 
  select(Sample_overview,SA_avg) #select avg columns

# view(organic_acids_sa_only_avg)

organic_acids_sa_only_avg_na<-organic_acids_sa_only_avg %>%
  na_if("") %>% 
  na.omit


# view(organic_acids_sa_only_avg_na)

organic_acids_sa_only_avg_na<-organic_acids_sa_only_avg_na %>% 
  rename('Sample'='Sample_overview','Succinic Acid'='SA_avg') # rename columns

# view(organic_acids_sa_only_avg_na)


## B) Creating stdev data.frame ################################################

organic_acids_sa_only_stdev<-organic_acids %>% 
  select(Sample_overview,SA_stdev) # select Stdev columns

# view(organic_acids_sa_only_stdev)

organic_acids_sa_only_stdev_na<-organic_acids_sa_only_stdev %>% 
  na_if("") %>% 
  na.omit # remove rows with NA or zeroes

# view(organic_acids_sa_only_stdev_na)

organic_acids_sa_only_stdev_na<-organic_acids_sa_only_stdev_na %>% 
  rename('Sample'='Sample_overview','Succinic Acid'='SA_stdev') # Rename columns

# view(organic_acids_sa_only_stdev_na)

## C) Changing data.frame dimensions to longer data frame #######################

organic_acids_sa_only_avg2<-organic_acids_sa_only_avg_na %>% 
  pivot_longer(cols="Succinic Acid",
               names_to="Acid",
               values_to="Percent")

# view(organic_acids_sa_only_avg2)

organic_acids_sa_only_stdev2<-organic_acids_no_t_stdev_na %>% 
  pivot_longer(cols="Succinic Acid",
               names_to="Acid",
               values_to="stdev")

# view(organic_acids_sa_only_stdev2)

## D) Merging data.frames ########################################################

organic_acids_sa_only_merged<-merge(organic_acids_sa_only_avg2,organic_acids_sa_only_stdev2,by=c("Sample","Acid"))

# view(organic_acids_sa_only_merged)
# 
# summary(organic_acids_sa_only_merged)

organic_acids_sa_only_merged_numeric<-transform(organic_acids_sa_only_merged,Percent=as.numeric(sub("%","",Percent)))

organic_acids_sa_only_merged_numeric<-transform(organic_acids_sa_only_merged_numeric,stdev=as.numeric(sub("%","",stdev)))

# summary(organic_acids_sa_only_merged_numeric)
# view(organic_acids_sa_only_merged_numeric)

# class(organic_acids_sa_only_merged_numeric$Percent)
# class(organic_acids_sa_only_merged_numeric$stdev)

## E) Plotting data ###############################################################


d1<-ggplot(organic_acids_sa_only_merged_numeric,aes(x=factor(Sample,level=c("Initial biomass","Initial biomass with CA",
                                                                     "Aer_4hr","Aer_8hr","Aer_24hr",
                                                                     "Aer_28day","Ana_4hr","Ana_8hr",
                                                                     "Ana_24hr","Ana_28day","CA_4hr",
                                                                     "CA_8hr","CA_24hr","CA_28day")),y=Percent,fill=factor(Acid,level=c('Succinic Acid'))))+
  geom_col(position=position_dodge())+
  labs(title="Succinic Acid",y="% of Dry Matter by Mass",x='Treatment Group',fill='Organic Acid')+
  scale_x_discrete(guide=guide_axis(angle=90))+
  geom_errorbar(aes(ymin=Percent-stdev,
                    ymax=Percent+stdev),
                position=position_dodge())+
  scale_fill_brewer(palette="Set3")+
  theme_classic()+
  theme(plot.title=element_text(size=15,hjust=0.5),
        axis.text.x=element_text(size=15),axis.title=element_text(size=15),
        axis.text.y=element_text(size=10),legend.text=element_text(size=10),
        legend.position="none")

# ggsave("sa_only.png",h=5,w=5)

# 8) TOTAL OA ONLY ############################################################

## A) Creating average data.frame ################################################

organic_acids_total_only_avg<-organic_acids %>% 
  select(Sample_overview,OA_total_avg) #select avg columns

# view(organic_acids_total_only_avg)

organic_acids_total_only_avg_na<-organic_acids_total_only_avg %>%
  na_if("") %>% 
  na.omit


# view(organic_acids_total_only_avg_na)

organic_acids_total_only_avg_na<-organic_acids_total_only_avg_na %>% 
  rename('Sample'='Sample_overview','Total Organic Acid'='OA_total_avg') # rename columns

# view(organic_acids_total_only_avg_na)


## B) Creating stdev data.frame ################################################

organic_acids_total_only_stdev<-organic_acids %>% 
  select(Sample_overview,OA_total_stdev) # select Stdev columns

# view(organic_acids_total_only_stdev)

organic_acids_total_only_stdev_na<-organic_acids_total_only_stdev %>% 
  na_if("") %>% 
  na.omit # remove rows with NA or zeroes

# view(organic_acids_total_only_stdev_na)

organic_acids_total_only_stdev_na<-organic_acids_total_only_stdev_na %>% 
  rename('Sample'='Sample_overview','Total Organic Acid'='OA_total_stdev') # Rename columns

# view(organic_acids_total_only_stdev_na)

## C) Changing data.frame dimensions to longer data frame #######################

organic_acids_total_only_avg2<-organic_acids_total_only_avg_na %>% 
  pivot_longer(cols="Total Organic Acid",
               names_to="Acid",
               values_to="Percent")

# view(organic_acids_total_only_avg2)

organic_acids_total_only_stdev2<-organic_acids_total_only_stdev_na %>% 
  pivot_longer(cols="Total Organic Acid",
               names_to="Acid",
               values_to="stdev")

# view(organic_acids_total_only_stdev2)

## D) Merging data.frames ########################################################

organic_acids_total_only_merged<-merge(organic_acids_total_only_avg2,organic_acids_total_stdev2,by=c("Sample","Acid"))

# view(organic_acids_total_only_merged)
# 
# summary(organic_acids_total_only_merged)

organic_acids_total_only_merged_numeric<-transform(organic_acids_total_only_merged,Percent=as.numeric(sub("%","",Percent)))

organic_acids_total_only_merged_numeric<-transform(organic_acids_total_only_merged_numeric,stdev=as.numeric(sub("%","",stdev)))

# summary(organic_acids_total_only_merged_numeric)
# view(organic_acids_total_only_merged_numeric)

# class(organic_acids_total_only_merged_numeric$Percent)
# class(organic_acids_total_only_merged_numeric$stdev)

## E) Plotting data ###############################################################


e1<-ggplot(organic_acids_total_only_merged_numeric,aes(x=factor(Sample,level=c("Initial biomass","Initial biomass with CA",
                                                                      "Aer_4hr","Aer_8hr","Aer_24hr",
                                                                      "Aer_28day","Ana_4hr","Ana_8hr",
                                                                      "Ana_24hr","Ana_28day","CA_4hr",
                                                                      "CA_8hr","CA_24hr","CA_28day")),y=Percent,fill=factor(Acid,level=c('Total Organic Acid'))))+
  geom_col(position=position_dodge())+
  labs(title="Total Organic Acids",y="% of Dry Matter by Mass",x='Treatment Group',fill='Organic Acid')+
  scale_x_discrete(guide=guide_axis(angle=90))+
  geom_errorbar(aes(ymin=Percent-stdev,
                    ymax=Percent+stdev),
                position=position_dodge())+
  scale_fill_brewer(palette="Set3")+
  theme_classic()+
  theme(plot.title=element_text(size=15,hjust=0.5),
        axis.text.x=element_text(size=15),axis.title=element_text(size=15),
        axis.text.y=element_text(size=10),legend.text=element_text(size=10),
        legend.position="none")


# ggsave("total_OA_only.png",h=5,w=5)


# 9) COMBINE FIGURES ##########################################################

ggarrange(a1,e1,c1,labels=c("a","b","c"))

The dput(head(organic_acids)) gives the following. Again, sorry about the length

> dput(head(organic_acids))
structure(list(ï..Experiment.ID = c("Oct19-Queue-T0", "Oct19-Queue-B-T0", 
"Oct19-Aer-T4-1", "Oct19-Aer-T4-2", "Oct19-Aer-T4-3", "Oct19-Aer-T12-1"
), OA.sample.name = c("T0 A&B", "CA T0 A&B", "Aer T4 1A&B", "Aer T4 2A&B", 
"Aer T4 3A&B", "Aer T8 1A&B"), Sample_overview = c("Initial biomass", 
"Initial biomass with CA", "Aer_4hr", "Aer_4hr", "Aer_4hr", "Aer_8hr"
), pH = c(6.24, 5.34, 6.35, 6.36, 6.36, 6.42), Wet.Biomass.extracted = c(0.9722, 
1.2766, 0.923, 1.1819, 0.908, 0.9389), Moisture.content = c("75.20%", 
"75.07%", "76.65%", "77.00%", "76.60%", "75.67%"), Dry.Biomass.extracted = c(0.241, 
0.318, 0.216, 0.271, 0.213, 0.228), pH_avg = c(6.24, 5.34, 6.356666667, 
NA, NA, 6.403333333), pH_stdev = c(NA, NA, 0.005773503, NA, NA, 
0.02081666), DB_avg = c(0.241, 0.318, 0.233, NA, NA, 0.22), DB_stdev = c(NA, 
NA, 0.033107707, NA, NA, 0.033624845), MC_avg = c("75.20%", "75.10%", 
"76.75%", "", "", "75.85%"), MC_stdev = c(NA, NA, 0.00248928, 
NA, NA, 0.001855378), X..Succinic.Acid..db. = c("0.23%", "0.22%", 
"2.97%", "2.02%", "2.03%", "2.62%"), X..Lactic.Acid..db. = c("0.00%", 
"0.00%", "0.03%", "0.01%", "0.01%", "0.03%"), X..Formic.Acid..db. = c("0.00%", 
"0.00%", "0.04%", "0.02%", "0.01%", "0.05%"), X..Acetic.Acid..db. = c("0.02%", 
"0.01%", "0.09%", "0.03%", "0.03%", "0.09%"), X..Propionic.Acid..db. = c("0.05%", 
"0.04%", "0.88%", "0.62%", "0.61%", "0.90%"), X..Isobutyric.Acid..db. = c("0.00%", 
"0.00%", "0.00%", "0.00%", "0.00%", "0.00%"), X..Butyric.Acid..db. = c("0.00%", 
"0.00%", "0.00%", "0.00%", "0.00%", "0.01%"), X..Isovaleric.Acid..db. = c("0.00%", 
"0.00%", "0.00%", "0.01%", "0.01%", "0.02%"), X..Valeric.Acid..db. = c("0.00%", 
"0.00%", "0.00%", "0.00%", "0.00%", "0.00%"), X..Organic.Acid..db. = c("0.30%", 
"0.27%", "4.01%", "2.72%", "2.71%", "3.72%"), SA_avg = c("0.23%", 
"0.22%", "2.34%", "", "", "2.85%"), LA_avg = c("0.00%", "0.00%", 
"0.02%", "", "", "0.04%"), FA_avg = c("0.00%", "0.00%", "0.03%", 
"", "", "0.05%"), AA_avg = c("0.02%", "0.01%", "0.05%", "", "", 
"0.10%"), PA_avg = c("0.05%", "0.04%", "0.70%", "", "", "0.95%"
), IBA_avg = c("0.00%", "0.00%", "0.00%", "", "", "0.00%"), BA_avg = c("0.00%", 
"0.00%", "0.00%", "", "", "0.01%"), IVA_avg = c("0.00%", "0.00%", 
"0.01%", "", "", "0.02%"), VA_avg = c("0.00%", "0.00%", "0.00%", 
"", "", "0.00%"), OA_total_avg = c("0.30%", "0.27%", "3.15%", 
"", "", "4.00%"), SA_stdev = c("0.00%", "0.00%", "0.55%", "", 
"", "0.50%"), LA_stdev = c("0.00%", "0.00%", "0.01%", "", "", 
"0.02%"), FA_stdev = c("0.00%", "0.00%", "0.01%", "", "", "0.01%"
), AA_stdev = c("0.00%", "0.00%", "0.03%", "", "", "0.07%"), 
    PA_stdev = c("0.00%", "0.00%", "0.16%", "", "", "0.08%"), 
    IBA_stdev = c("0.00%", "0.00%", "0.00%", "", "", "0.00%"), 
    BA_stdev = c("0.00%", "0.00%", "0.00%", "", "", "0.00%"), 
    IVA_stdev = c("0.00%", "0.00%", "0.01%", "", "", "0.01%"), 
    VA_stdev = c("0.00%", "0.00%", "0.00%", "", "", "0.00%"), 
    OA_total_stdev = c("0.00%", "0.00%", "0.75%", "", "", "0.63%"
    )), row.names = c(NA, 6L), class = "data.frame")

Indeed I wont look at your full code, as even the shorter code that works for me doesnt work for you.
I wouldnt try to tackle a harder problem than one I already can't solve.

a) either you don't have the correct packages, and you should address that
b) you just happen to have a corrupt R session, and should restart it
c) assuming your packages are good, and you get simple error on the simplest version of the code discussed :

library(ggpubr)

p1 <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
p2 <- ggplot(mtcars, aes(mpg, wt)) + geom_point()


ggarrange(p1,p2,labels = 1:2)

I would consider a complete reinstall of R itself.

as a final note, ggplot2 makes objects which are 'lists' and theres nothing generally problematic with that, its how it works and its fine...

Ah, restarting Rstudio seemed to fix it. It's odd as I know I have opened and closed R multiple times while troubleshooting, but it looks like it worked this time.

Wouldn't the plotlist option in ggarrange do the job.

ggarrange(plotlist=figList, widths =1,ncol=4, nrow=2,
labels = figLabels, common.legend = TRUE, legend="bottom")

where figList is a list of individual plots

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.