fct_relevel() with NA values

Hi all,

I have data that contains some NA values, and I am trying to make a plot as below:

library(ggplot2)
library(forcats)
library(dplyr)
library(ggpubr)

df<-data.frame(Y = rnorm(20, -6, 1),
                  X = sample(c("yes", "no", NA), 20, replace = TRUE))

dfplot<- df   %>% mutate(X=fct_relevel(X, "yes"))%>%
  ggplot(.,
         aes(x=X, y=Y, fill=X))+
  geom_boxplot(size=1, width = 0.2, show.legend = F, outlier.shape = NA,
               position=position_nudge(x=0.3))+
  geom_jitter(show.legend = T, shape=21, width=0.2, size=2)+
  geom_crossbar(data=df %>% group_by(X) %>% summarise(mean=mean(Y), .groups="keep"),
                aes(x=X, ymin=mean, ymax=mean, y=mean), width = 0.2, show.legend = F)+

  labs(x="",
       y="%")

dfplot

however when I try to plot only the "yes" and "no" variables, dropping the "NA" using filter(X!="NA") in the pipe from df I cannot relevel them to the correct order with "yes" as the first column. The same happens if I use drop_na("X") instead of filter(X!="NA") see below for code:

df<-data.frame(Y = rnorm(20, -6, 1),
                  X = sample(c("yes", "no", NA), 20, replace = TRUE))

dfplot<- df %>% filter(X!="NA")  %>% mutate(X=fct_relevel(X, "yes"))%>%
  ggplot(.,
         aes(x=X, y=Y, fill=X))+
  geom_boxplot(size=1, width = 0.2, show.legend = F, outlier.shape = NA,
               position=position_nudge(x=0.3))+
  geom_jitter(show.legend = T, shape=21, width=0.2, size=2)+
  geom_crossbar(data=df %>% group_by(X) %>% summarise(mean=mean(Y), .groups="keep"),
                aes(x=X,ymin=mean, ymax=mean, y=mean), width = 0.2, show.legend = F)+ 

  labs(x="",
       y="%")

dfplot

Any help would be much appreciated. Thanks!

Use this:

filter(!is.na(X))

still not working, same output as above.

dfplot <- df %>% 
  mutate(X=fct_relevel(X, "yes")) %>%
  filter(!is.na(X)) %>% 
  
  ggplot(.,
         aes(x=X, y=Y, fill=X))+
  geom_boxplot(size=1, width = 0.2, show.legend = F, outlier.shape = NA,
               position=position_nudge(x=0.3))+
  geom_jitter(show.legend = T, shape=21, width=0.2, size=2)+
  geom_crossbar(data=df %>% group_by(X) %>% summarise(mean=mean(Y), .groups="keep"),
                aes(x=X, ymin=mean, ymax=mean, y=mean), width = 0.2, show.legend = F)+
  
  labs(x="",
       y="%")

dfplot

image

but i need the yes box to appear first (like in the plot that includes the NA box). I basically want to recreate the top plot, but excluding the NA box. Thanks for taking the time to help!

solution found: need to provide filter(!is.na=(X)) to geom_crossbar() as well.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.