Search code examples
rcategorical-dataloess

Exclude categorical level in plots


I'm using R and I have this categorical variable that has some 'NA' values. My stats_smooth loess curve doesn't do anything at this NA point, and therefore the plot looks weird.

I'd like to have a graph without the value "NA".

How do I do this?

Current code:

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)

Plot 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


Solution

  • What about just filtering out those NA values before the plot? A dplyr solution could look something like:

    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")