R Merging Boxplots

15,326

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

Share:
15,326
triunenature
Author by

triunenature

I am the Chief Technology Officer at PolicyCheck. Our site is completely built using Flask, with a few small pieces of C Languages I write it (ordered by frequency): Python JavaScript R C Go

Updated on June 18, 2022

Comments

  • triunenature
    triunenature almost 2 years

    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
    triunenature about 10 years
    I had to change how my data was formatted, so I could follow the guide, but it works very well.