5

I want to plot a lot of boxplots in on particular style to compare them. But when a group is empty the group "isn't plotted".

lets say I have a dataframe:

   a   b
1  1   5
2  1   4
3  1   6
4  1   4
5  2   9
6  2   8
7  2   9
8  3 NaN
9  3 NaN
10 3 NaN
11 4   2
12 4   8

and I use boxplot to plot it:

boxplot(b ~ a , df)

than I get the plot without group 3 (which I can't show because I did not have "10 reputation")

I found some solutions for removing empty groups via Google but my problem is the other way around.

And I found the solution via at=c(1,2,4) but as I generate an Rscript with python and different groups are empty I would prefer, that the groups aren't dropped at all.

Oh I don't think I have the time to grapple with additional packages. Therefore I would be thankful for solutions without them.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
canis
  • 53
  • 3

1 Answers1

2

You can get the group on the x-axis by

boxplot(b ~ a , df, na.action=na.pass)

Or

boxplot(b~factor(a), df)
akrun
  • 874,273
  • 37
  • 540
  • 662