2

I am trying to use R to show a merged boxplot, I am sure this is easy, I just am missing something:

boxplot(WHO$Male, WHO$Female, ylim=c(0,100))

boxplot(WHO$Female ~ WHO$Year, ylim=c(0,100))
boxplot(WHO$Male ~ WHO$Year, ylim=c(0,100))

All three work, but when I try:

boxplot(WHO$Male ~ WHO$Year, WHO$Female ~ WHO$Year, ylim=c(0,100))

It returns:

Error in as.data.frame.default(data) : 
   cannot coerce class ""formula"" to a data.frame

Note, Year, only contains three numbers, 1990, 2000, 2010

> head(WHO)
  Year WHO.region      Country Male Female
1 1990     Africa      Algeria   66     68
2 1990     Africa       Angola   39     43
3 1990     Africa        Benin   45     50
4 1990     Africa     Botswana   63     66
5 1990     Africa Burkina Faso   45     49
6 1990     Africa      Burundi   47     50
triunenature
  • 651
  • 2
  • 7
  • 23
  • What do you mean by "merged boxplot". You want them both on one plot, side by side? And please post a few lines of the data. – Rich Scriven Mar 20 '14 at 03:57
  • So, if you do Male, Female, you get two boxplots, one showing males data, and one showing females data. If you do Male ~ Year, you get males data broken by year. I want Male and Females data side by side, broken by year – triunenature Mar 20 '14 at 04:03
  • I'm not sure I understand what you want. Are you trying to get years on the x-axis? I'm not sure `boxplot` is the right plot for that. – Rich Scriven Mar 20 '14 at 04:25
  • Yes, both Years, and Gender on x-axis. I created a paint image of what I think it should look like: http://s1.postimg.org/h5k527udb/123.png, color being Male, Female – triunenature Mar 20 '14 at 04:30
  • Sigh. First argument to `boxplot.formula` should be a formula and second argument should be a data.frame. Makes no sense to give two formulas to `boxplot`. (Read the help page.) – IRTFM Mar 20 '14 at 05:42
  • Ok, but what should the formula be? Since its both, WHO$Male ~ Year AND WHO$Female ~ Year? – triunenature Mar 20 '14 at 05:50

1 Answers1

1

reshape2 package does something similar. Actually there was quite similar question - Plot multiple boxplot in one graph, maybe it will be helpful.

Community
  • 1
  • 1
romants
  • 3,660
  • 1
  • 21
  • 33