Home > Net >  How to change the NA value in barplot?
How to change the NA value in barplot?

Time:12-16

when I try to plot df I find NA in the barplot. I can't understand why. Here is df

dput(df)
structure(list(Cancer.type = c(" BLCA", " BLCA", " BLCA", "BRCA", 
"BRCA", "BRCA", "COAD", "COAD", "COAD", "ESCA", "ESCA", "ESCA", 
"GBM", "GBM", "GBM", "KIRC", "KIRC", "KIRC", "LIHC", "LIHC", 
"LIHC", "LUAD", "LUAD", "LUAD", "LUSC", "LUSC", "LUSC", "PRAD", 
"PRAD", "PRAD", "STAD", "STAD", "STAD", "THCA", "THCA", "THCA", 
"UCEC", "UCEC", "UCEC"), Number.of.genes = c(4L, 5L, 7L, 8L, 
6L, 2L, 1L, 10L, 5L, 3L, 5L, 8L, 2L, 4L, 10L, 2L, 8L, 6L, 5L, 
5L, 6L, 5L, 8L, 3L, 4L, 8L, 4L, 4L, 6L, 6L, 2L, 5L, 9L, 8L, 3L, 
5L, 4L, 3L, 9L), Condition = c("Down ", "Up", "ns", "Down ", 
"Up", "ns", "Down ", "Up", "ns", "Down ", "Up", "ns", "Down ", 
"Up", "ns", "Down ", "Up", "ns", "Down ", "Up", "ns", "Down ", 
"Up", "ns", "Down ", "Up", "ns", "Down ", "Up", "ns", "Down ", 
"Up", "ns", "Down ", "Up", "ns", "Down ", "Up", "ns")), class = "data.frame", row.names = c(NA, 
-39L))

Here is my script

ggplot(df,aes(fill = factor(Condition, levels = c('Down','Up','ns')),
               y = Number.of.genes, x =Cancer.type))   
 geom_bar(position="stack", stat="identity") theme_bw() 
   theme(axis.text.x = element_text(size = 10, angle = 45, hjust = 1, colour = 'black')) 
   scale_fill_manual(values=c("seagreen2", "deepskyblue3",'goldenrod1')) 
   labs(fill = 'Condition', x = NULL)

enter image description here

Thanks for any help.

CodePudding user response:

In your Condition column, the Down actually has a trailing space. So you can do 3 things:

  1. remove the space from your data frame

df$Condition <- gsub(' ', '', df$Condition)

  1. Add the space in levels()

ggplot(df,aes(fill = factor(Condition, levels = c('Down ','Up','ns'))

  1. Remove the levels argument

ggplot(df,aes(fill = factor(Condition)

  • Related