0

I would like to show all the possible combinations of factors in boxplots, even if they don't appear in the data. But ggplot keeps removing missing values from the data and hence removes the corresponding boxes. Is there any way/hack to stop this?

For example, I need to show 'e' for all groups, even if all its values are NA? BTW, I don't want to replace NAs with any other value.

set.seed(666)
dat <- data.frame(matrix(runif(60,1,20),ncol=6))
names(dat) <- letters[1:6]
longdat <- melt(dat,measure.vars=c('b','c','d','e','f'))
longdat$a <- 'a'
longdat2 <- longdat
longdat2$a <- 'xa'
longdat2$value[longdat2$variable=='e'] <- NA
longdat <- rbind(longdat,longdat2)
library(ggplot2)
ggplot(longdat, aes(x=variable, y=value, group=interaction(a,variable))) + 
    geom_boxplot(aes(colour = variable), position=position_dodge2(width=0.8,preserve='single'),na.rm=T)

t reflec

2
  • how it will appear if it's all values in NA ? you mean increase the width between d and f ?illustrate it please. Jul 26, 2019 at 14:15
  • I changed the example. Imagine there are three subgroups instead of two and the middle one is missing. How can I preserve a space for that?
    – Ben
    Jul 28, 2019 at 2:19

2 Answers 2

1

I changed dat[,5] <- NA and made it after melt to get the "e" factor in longdat also made 2nd dataframe and changed the NA with zero and used it and changed the colour of "e" with White.

set.seed(666)
dat <- data.frame(matrix(runif(60,1,20),ncol=6))
names(dat) <- letters[1:6]

library(reshape2)
longdat <- melt(dat)
longdat <- longdat %>% mutate(value = ifelse(variable == "e",NA,value))
longdat2 <- longdat %>% mutate(value = ifelse(variable == "e",0,value))

library(ggplot2)
ggplot() + geom_boxplot(data = longdat2,aes(x=variable, y=value,colour = variable),na.rm = F) +
        scale_colour_manual(name = "Hello", 
                              labels = c("a","b","c","d","e","f"),values = c("green","red","black","blue","white","yellow"))

enter image description here

3
  • Thanks Omar. But this is just a simple example. Is there any more general way to do it? Do you think making X variable a factor would work?
    – Ben
    Jul 26, 2019 at 4:03
  • No it won't, because variable is factor in the dataframe Jul 26, 2019 at 4:06
  • Provide another complix example so i can try to get away to get what you want. the problem is i can't provide more level to the legend if this level's values is NA. Jul 26, 2019 at 4:09
0

It's using e as an index variable when you melt the dataset. You'll probably see 'using e as id variables' when you run this code. Adding an index variable (this can be anything - row_number(), just a number, whatever) to the dataset and telling melt to use that as an index will solve your issue.

set.seed(666)
dat <- data.frame(matrix(runif(60,1,20),ncol=6))
names(dat) <- letters[1:6]
dat[,5] <- NA
dat <- dat %>% mutate(index = row_number())

library(reshape2)
longdat <- melt(dat, id.vars = 'index')
library(ggplot2)
ggplot(longdat, aes(x=variable, y=value)) + geom_boxplot(aes(colour = variable))

See the output here

2
  • Thanks. That was an example. Any suggestion if I don't want to use mutate? For example, what if I want to use data.table or data.frame? Is there anything equivalent?
    – Ben
    Jul 26, 2019 at 5:51
  • You just do dat$index <- 1 It just needs some variable it can use as index so it won't pick up on the var that only has NAs
    – Sushmita
    Jul 26, 2019 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.