[英]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.