如何从因子变量(以及ggplot图表)中删除NA? [英] How to remove NA from a factor variable (and from a ggplot chart)?

查看:573
本文介绍了如何从因子变量(以及ggplot图表)中删除NA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于ggplot将它们包括在图中,就好像它们是另一个类别/级别一样,我对因子变量中的NA有问题.我想删除丢失的数据. 很抱歉,我目前没有任何方便的代码,我试图从在data()处找到的数据集中删除因子水平,但它不起作用.

I have a problem with NA in a factor variable since ggplot includes them in the plot as if they are another category/level. I would like to drop the missing data. I am sorry I don't have code handy at the moment, I tried to remove factor levels from dataset that I found at data() and it did not work.

有人遇到了同样的问题吗?

Had someone the same problem?

我尝试了此处建议的解决方案从以下位置删除未使用的因子水平: ggplot条形图,但出现错误

I tried the solution suggested here Remove unused factor levels from a ggplot bar plot but I get an error

错误:mycode中出现意外符号

Error: unexpected symbol in: mycode

有人可以提出建议吗?

此外,如果无法从ggplot代码中删除它们,该如何从因子变量中删除NA?

Also, if there is no way to remove them from inside the ggplot code, how can I remove the NA from a factor variable?

推荐答案

假设您的数据位于名为dat

assuming your data is in a data frame called dat

newdat <- dat[!is.na(dat$Factor), ]

不确定如何解决ggplot代码内部的问题

not sure how to solve the problem inside of ggplot code

这篇关于如何从因子变量(以及ggplot图表)中删除NA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆