0
votes

Taking a look at this minimal example:

library(mosaic) 
library(effects) 
library(openxlsx)
library(corrgram) 
library(GGally) 
library(vcd) 
library(corrplot) 
library(ggpubr)
library(scales)

library(dplyr) 
library(tidyr) 
library(ggplot2) 

#------------------------------------------------------------------
library(naniar)

    l1 = c(1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,4,5)
    l2 = c("N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A","N/A",2,2,2,2,2,3,3,3,4,4)
        
        df <- data.frame (p = c(1:20),
                          q1=l1[1:20],
                          q2=l2[1:20]
        )
        dfn<-df %>% replace_with_na_all(condition = ~.x == "N/A")
        
            
        
        preparedataboxplot <- select(dfn,p,q1,q2) %>% gather(key='Question_num', value='Antwort', -p)
        preparedataboxplot <- preparedataboxplot %>% drop_na()
        preparedataboxplot$Antwort<-unlist(preparedataboxplot$Antwort)
        
        
        ggplot(preparedataboxplot,aes(x=factor(Question_num),y=Antwort))+
          stat_boxplot(geom='errorbar',width=0.8)

If q2=l1[1:20] then the boxplots won't show as anticipated: Error

Using q2=l2[1:20]somehow works: enter image description here

I assume there is a problem with the NAs. Can anyone find out what exactly is the problem?

1
some variable that you don't expect/want to be a factor probably got converted to a factor by mistake ... - Ben Bolker
replace_with_na_all returns q2 as a character vector - rawr
Can I use 'as.numeric()' to correct this? - manuel459

1 Answers

0
votes

Try this :

library(tidyverse)

df <- data.frame (p = 1:20,
                  q1=l1[1:20],
                  q2=l2[1:20])

df[df == 'N/A'] <- NA
df <- type.convert(df, as.is = TRUE)


preparedataboxplot <- select(df,p,q1,q2) %>% gather(key='Question_num', value='Antwort', -p)
preparedataboxplot <- preparedataboxplot %>% drop_na()


ggplot(preparedataboxplot,aes(x=factor(Question_num),y=Antwort))+
  stat_boxplot(geom='errorbar',width=0.8)