排除图中的分类级别

时间:2018-02-15 17:30:10

标签: r categorical-data loess

我正在使用R,我有这个分类变量,它有一些'NA'值。我的stats_smooth黄土曲线在这个NA点没有做任何事情,因此情节看起来很奇怪。

我想要一个没有“NA”值的图表。

我该怎么做?

当前代码:

ggplot(data=dat, aes(factor(Make_ends_meet),number_shares)) + geom_jitter(size=2.5) + geom_violin() + stat_smooth(aes(x=Make_ends_meet, y=number_shares, group=1), method="loess")# + facet_grid(. ~ category, margins=TRUE)

剧情 The Loess curve goes up, until it reaches the last categorical variable which is also a number. After that, it stops. The plot continues for categorical variable "NA", which I wish to remove so that I have a plot of values 1 - 5

1 个答案:

答案 0 :(得分:0)

如何在情节之前过滤掉NA值? dplyr解决方案看起来像:

library(dplyr)
library(ggplot2)

dat %>%
 filter(!is.na(Make_ends_meet)) %>%
 ggplot(aes(x = factor(Make_ends_meet), y = number_shares)) + 
 geom_jitter(size=2.5) + 
 geom_violin() + 
 stat_smooth(aes(x=Make_ends_meet, y=number_shares, group=1), method="loess")