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)
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
CodePudding user response:
For the graph, can you change the NAs to 0? e.g.
df <- df %>%
zoo::na.fill(0)
vioplot(y ~ x , df, ylim = c(1, 4))
CodePudding user response:
Or, using ggplot...
df %>% ggplot() geom_violin(aes(x=as.factor(x), y=y))
Gives
as does
df %>% ggplot() geom_violin(aes(x, y=y, group=x))



