Home > Enterprise >  Issues with ggplot on R: code is correct but I keep receiving the error "Must request at least
Issues with ggplot on R: code is correct but I keep receiving the error "Must request at least

Time:09-21

There was a similar question asked that I found but did not provide a specific answer for my situation. I am conducting an RNA seq and the following code was used to generate a ggplot of Cell Numbers, but I keep getting the same error.

Here is the code for the plot:

metadata %>%
ggplot(aes(x=sample, fill= sample)) 
geom_bar() 
theme_classic() 
theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1)) 
theme(plot.title = element_text(hjust = 0.5, face = "bold")) 
ggtitle("Number of cells")

I believe it is worth noting the metadata has the sample column, but all of the values are "N/A". This column was added earlier using the following code:

metadata$sample <- NA
metadata$sample[which(str_detect(metadata$cellIDs, "^c2_"))] <- "c2"

The cell IDs were added to the metadata using

metadata$cellIDs <- rownames(metadata)

If there is any further code missing that you may need to solve this issue, let me know! Restarting and redownloading R the packages did not work.

Output of dput(head(metadata))

structure(list(seq_folder = structure(c(1L, 1L, 1L, 1L,1L,1L),levels = "SeuratProject", class = "factor"), nUMI = c(5621, 7010, 1360, 1849, 1062, 10906), nGene = c(1901L, 2241L, 630L, 1009L, 633L, 3036L), log10GenesperUMI = c(0.874438700026547, 0.871213604555057, 0.893347946622289, 0.91948245341875,0.925739735597602, 0.862454250595566), mitoratio =c(0.0158334815869062,0.00756062767475036, 0.0301470588235294, 0.018388318009735,0.0715630885122411,0.0151292866312122), percent.mt = c(1.58334815869062, 0.756062767475036,3.01470588235294, 1.8388318009735, 7.15630885122411, 1.51292866312122), cellIDs =c("AAACCTGAGCAGGCTA-1", "AAACCTGAGGTGCTAG-1", "AAACCTGGTAGCGTGA-1", "AAACCTGGTTAAGTAG-1", "AAACCTGTCGCTGATA-1", "AAACGGGAGATGCCAG-1"), sample =c(NA_character_, NA_character_, NA_character_, NA_character_, NA_character_,NA_character_)), row.names = c("AAACCTGAGCAGGCTA-1", "AAACCTGAGGTGCTAG-1", "AAACCTGGTAGCGTGA-1", "AAACCTGGTTAAGTAG-1", "AAACCTGTCGCTGATA-1", "AAACGGGAGATGCCAG-1"), class = "data.frame")

CodePudding user response:

You can avoid this error with all-missing data by "setting" a manual fill scale:

data = data.frame(x = NA_character_)

## same error as in the question
ggplot(data, aes(x = x, fill = x))   
  geom_bar()

## no error
ggplot(data, aes(x = x, fill = x))   
  geom_bar()   
  scale_fill_manual()
  • Related