R script executes all right in RStudio but throws ggplot2-related error in command mode


My goal is to make a script that will do some data exploratory analysis for one type of files (same name in different directories) when supplied with directory name as a command line argument and produce a multi-figure: 4 plots for 4 analyses performed. I have the full code which works OK in Rstudio. Here is the chunk of it, generating 1st of 4 graphs. It throws an error with Aesthetics when run from the command line.

args <- commandArgs(TRUE)

###Check and install missing libraries
list.of.packages <- c("tidyverse", "methods", "Matrix", "WGCNA", "ggplot2", 
                      "tsne", "DESeq2", "reshape2", "RColorBrewer", "readr", "dplyr", "AnnotationDbi",
                      "org.Hs.eg.db", "data.table", "ggsci", "magrittr", "multipanelfigure",
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)


lapply(list.of.packages, FUN = function (X) {
  do.call("library", list(X))

wd <- args[1]

count_data = read.table(file=list.files(pattern = "^all_counts\\.txt$"), header = TRUE, sep = "\t")
row.names(count_data) = count_data$ID
count_data$ID <- NULL
conds = fread("/Users/x/project_y/data/Overview_sampleID.txt", select = c(1:2))

vst = vst(as.matrix(count_data)) 
pcDat = prcomp(t(vst)) #Calculation with variance-stabilizing transformation
var_explained <- pcDat$sdev^2/sum(pcDat$sdev^2)

#Scale and center columns
d=svd(scale(as.matrix(count_data))) #apply SVD
assays=t(d$u) %*% scale(as.matrix(count_data)) #projection on eigenassays, representing the sample space

#Scale and center rows
pcDat2=prcomp(t(as.matrix(count_data)), center=TRUE, scale=TRUE)


tsne_res <- Rtsne(t(count_data), perplexity = 5)

#--------Generating plots-------#

pcDatx = as.data.frame(pcDat$x)
status = factor(conds$Sample_Project)

p1 = ggplot(data = pcDatx, aes(x = PC1, y = PC2, color = status)) +

ggsave("~/testfig.pdf", plot = p1, width = 5, height = 7)

Then from the terminal it throws this kind of error. However, I am not sure why as the length of the data frame is the same as the length of colour variable. Could anyone help with this issue? enter image description here

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum