简体   繁体   English

从 R 中的绘图中删除 NA

[英]Remove NA from plot in R

I have the following table that I want to plot in R我有我想在 R 中绘制的下表

     A    B    C  NA
0   500  200  200 0

This table is generated from a variable.该表是从变量生成的。 I had previously removed the NAs using data<-data[!(data$pid3==""),]我以前使用data<-data[!(data$pid3==""),]删除了data<-data[!(data$pid3==""),]

If I use the plot() function with this variable, the NA and "" shows up in the plot.如果我将此变量与plot()函数一起使用,则plot()显示 NA 和 ""。 How do I get rid of this in the plot?我如何在情节中摆脱这一点?

Thank you!谢谢!

There is a difference between "" and NA in R: R 中的""NA之间有区别:

> is.na("")
[1] FALSE
> is.na(NA)
[1] TRUE

If you want to remove NAs, you should use something like this:如果你想删除 NAs,你应该使用这样的东西:

data <- data[!is.na(data$pid3),]

(It may be a good idea to remove empty strings as well, so you can run the command above in addition to your previous filtering step.) (删除空字符串也可能是一个好主意,因此除了之前的过滤步骤之外,您还可以运行上面的命令。)

I figured it out.我想到了。 I was able to achieve this using the ggplot2 package.我能够使用ggplot2包实现这一点。

I generate the table using我使用生成表

table <- data %>% group_by(pid3) %>% summarise(n = n())

Then I plot using ggplot2然后我使用ggplot2

ggplot(table, aes(x = pid3, y = n)) +
  geom_bar(stat="identity", position=position_dodge()) + theme_classic()

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

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