在情节中停止打印na

时间:2017-06-15 17:54:35

标签: r plot

我正在绘制一个分类变量。没有NA但是情节出现NA条,没有任何观察。如何在我的情节中停止对此NA进行打印?

ggplot(data.frame(cat.hour), 
       aes(x=factor(cat.hour, levels=c("1 min","2 min","3 min","4 min","5 min","6 min","7 min","8 min","9 min","10 min")))) + 
  geom_bar(fill="lightgreen")

enter image description here

1 个答案:

答案 0 :(得分:0)

正如您在电话unique(cat.hour)中看到的那样,您的数据中存在NA。

过滤非NA值将是您的绘图的简单解决方法。但是,我鼓励您在情节之外进行计算。

require(ggplot2)
require(dplyr)
require(magrittr)

cat.hour %>%
   na.omit() %>%
   mutate(variable = factor(variable, levels=c("1 min","2 min","3 min","4 min","5 min","6 min","7 min","8 min","9 min","10 min"))) -> cat.hour

g <- ggplot(cat.hour, aes(x = variable))
g <- g + geom_bar(fill = "lightgreen")
g