rviolin-plotr-base-graphics

Plot empty groups in violin plot


I want to compare a number of violinplots. But I am running into a problem for cases where a group is empty: I would like an empty slot plotted so its easier to compare multiple plots.

lets say I have a dataframe:

df = data.frame("x"=rep(c(1:4), 3), y=rep(c(1:4), each=3))
df$y[df$x==3] = NA 

so all of group 3 is NA and I use vioplot to plot it:

library(vioplot)
vioplot(y ~ x , df)

violin plot

then I get the plot without group 3. Is there a way I can plot all groups 1:4 but 3 is just empty?

Thanks


Solution

  • I found a workaround solution for my specific problem using the at argument:

    df = data.frame("x"=rep(c(1:4), 3), y=rep(c(1:4), each=3))
    df$y[df$x==3] = NA
    
    library(vioplot)
    vioplot(y ~ x , df, at=unique(df$x[! is.na(df$y)]), xaxt="n")
    axis(1, at=unique(df$x))
    

    enter image description here