How to enforce ggplot's position_dodge on categories with no data?

后端 未结 3 1574
孤街浪徒
孤街浪徒 2020-12-30 01:15

I\'m trying to use position_dodge on ggplot to obtain boxplots of two different signals (ind) sharing the same categories (cat). When there is a category with data for one

3条回答
  •  情歌与酒
    2020-12-30 02:06

    x of B has no values, so you can add "B", 0, "x" which essentially indicates that there is no distribution of "values" for x of B. The median and other percentiles are zero.

     data<-data.frame(cat=c('A','A','A','A','B','B','A','A','A','A','B','B','B'), 
                 values=c(3,2,1,4,NA,NA,4,5,6,7,8,9,0), 
                 ind=c('x','x','x','x','x','x','y','y','y','y','y','y','x'))
    

    Also you do not have to add position parameters here, because when you consider x as a factor, ggplot -- geom_boxplot will automagically dodge to the sides.

    print(ggplot() +
      scale_colour_hue(guide='none') +
      geom_boxplot(aes(x=as.factor(cat), y=values, fill=ind), 
      data=data,
      outlier.size = 1.2,
      na.rm=T))
    

提交回复
热议问题