【问题标题】:How do you remove NA's in geom_bar when plotting data subsets?绘制数据子集时如何删除 geom_bar 中的 NA?
【发布时间】:2014-04-07 09:13:01
【问题描述】:

这与问题here 有关,但建议的解决方案不适用于我的情况。

我已经发布了一个关于我的大号 data.frame here 的问题,但如果您只想下载它(>2000 行),请通过 this link 下载。

所以以下代码用于在ggplot2中创建一个简单的geom_barbarplot

gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SoExample$Structural_motif=="NDBL/beta-sheets",],
aes(x=p53_IHC))
gghist+geom_bar()

这会产生这个情节

如您所见,NA 也被绘制出来。我尝试了各种选项来删除 NA,包括 以下

gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SOExample$Structural_motif=="NDBL/beta-sheets",], aes(x=p53_IHC), drop=TRUE)
gghist+geom_bar()
gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SOExample$Structural_motif=="NDBL/beta-sheets",], aes(x=p53_IHC), na.rm=TRUE)
gghist+geom_bar()
gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SOExample$Structural_motif=="NDBL/beta-sheets",], aes(x=p53_IHC, na.rm=TRUE))
gghist+geom_bar()
gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SOExample$Structural_motif=="NDBL/beta-sheets",], aes(x=factor(p53_IHC), na.rm=TRUE))
gghist+geom_bar()
 gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SOExample$Structural_motif=="NDBL/beta-sheets",], aes(x=factor(p53_IHC))
    gghist+geom_bar(na.rm=TRUE)

然后我尝试了这个

gghist_2<-ggplot(na.omit(ARCTP53_SOExample[ARCTP53_EsoMutClean$Structural_motif=="NDBL/beta-sheets",]), aes(x=p53_IHC))
gghist_2+geom_bar()

这给了我这个错误

Error in as.environment(where) : 'where' is missing

此外,我尝试了 this,这给了我以下 错误

datasub<-ARCTP53_SOExample[!is.na(ARCTP53_SOExample)]
gghist_3<-ggplot(datasub[datasub$Structural_motif=="NDBL/beta-sheets",])
Error in datasub$Structural_motif : 
  $ operator is invalid for atomic vectors

而且这段代码也不起作用:

gghist_3<-ggplot(datasub, aes(x=p53_IHC))
Error: ggplot2 doesn't know how to deal with data of class character

那么,有没有人知道一个简单的解决方案? data.frame 很大,并且根据我正在查看的列而固有地有很多丢失的数据,但并非所有行中都丢失了所有丢失的数据,因此删除其中包含单个“NA”的任何行,将被打败了。

非常感谢您的帮助。

亲切的问候,

奥利弗

编辑在下面 Daniel 的 cmets 之后,这是我在 below 之后得到的。如您所见,问题仍未解决。对不起。

gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SOExample$Structural_motif=="NDBL/beta-sheets" & is.na(ARCTP53_SOExample$p53_IHC) == F,], aes(x=p53_IHC))
gghist+geom_bar()

更新:重新运行代码,由于某种原因它现在可以工作了。得到下面的情节:

【问题讨论】:

    标签: r ggplot2 na


    【解决方案1】:

    我无法下载您的数据,但这应该可以:

    gghist<-ggplot(ARCTP53_SOExample[ARCTP53_SoExample$Structural_motif=="NDBL/beta-sheets" & is.na(ARCTP53_SOExample$p53_IHC) == F,], aes(x=p53_IHC))
    gghist+geom_bar()
    

    【讨论】:

    • 只需重新运行代码即可。之前肯定有错别字。感谢磨坊@Daniel Falbel
    猜你喜欢
    • 1970-01-01
    • 2011-07-07
    • 2021-12-25
    • 1970-01-01
    • 2023-03-04
    • 2014-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多