How to remove NA from a factor variable (and from a ggplot chart)?

时光毁灭记忆、已成空白 提交于 2019-12-19 09:19:33

问题


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?

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

Error: unexpected symbol in: mycode

Can someone suggest something?

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


回答1:


assuming your data is in a data frame called dat

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

not sure how to solve the problem inside of ggplot code




回答2:


I'd use qplot instead of ggplot in this way:

qplot(x=column, data=subset(dataframe,!is.na(column)))

I hope this helps.




回答3:


Answers on this related thread: NA's are being plotted in boxplot ggplot2

In brief, instead of the usual:

ggplot(data=data)

use

ggplot(data=na.omit(data[,c("var1","var2",...)])) 

where var1, var2 etc are the variables you are plotting.



来源:https://stackoverflow.com/questions/17415672/how-to-remove-na-from-a-factor-variable-and-from-a-ggplot-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!