简体   繁体   English

如何更改条形图中的 NA 值?

[英]How to change the NA value in barplot?

when I try to plot df I find NA in the barplot.当我尝试绘制df时,我在条形图中找到了 NA。 I can't understand why.我不明白为什么。 Here is df这是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)

在此处输入图像描述

Thanks for any help.谢谢你的帮助。

In your Condition column, the Down actually has a trailing space.在您的Condition列中, Down实际上有一个尾随空格。 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()levels()中添加空间

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

  1. Remove the levels argument删除levels参数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM