1

I'm trying to plot 4 groups of data as boxplots on the same set of axis. I've been able to use the print() function to plot them on separate plots, but can't figure out how to plot them all together, preferably using the base package or lattice

below is some code I am trying, but it keeps coming up with error:

Error in x[floor(d)] + x[ceiling(d)] :
non-numeric argument to binary operator

Heres the code I'm currently trying:

Summer <- SeasonalMax$Summer
Autumn <- SeasonalMax$Autumn
Winter <- SeasonalMax$Winter
Spring <- SeasonalMax$Spring

boxplot(Summer, Autumn, Winter, Spring,
    main = "Multiple boxplots for comparision",
    at = c(1,2,3,4),
    names = c("Summer", "Autumn", "Winter", "Spring"),
    las = 2,
    col = c("red","orange", "blue", "pink"))
Natway
  • 11
  • 2

2 Answers2

0

First melt the data into long format

# Dummy dataset
Dat <- data.frame(Spring = runif(10,0,1),
           Summer = runif(10,0,1),
           Autumn = runif(10,0,1),
           Winter = runif(10,0,1))

Then melt the data into long format with either : reshape2 package

library(reshape2)
melt(Dat)

or tidyr package

library(tidyr)
gather(Dat,key="Season",value="Value")

Then when you plot the boxplot , use the formula argument as follows [I will continue with tidyr because I named the columns]

Dat2 <- gather(Dat,key="Season",value="Value")
with(Dat2,boxplot(Value~Season))

And with all your additions

with(Dat2,boxplot(Value~Season,
     main = "Multiple boxplots for comparision",
     at = c(1,2,4,5),
     names = c("Summer", "Autumn", "Winter", "Spring"),
     las = 2,
     col = c("red","orange", "blue", "pink")))
JMilner
  • 491
  • 4
  • 12
  • when I use the gather() function I get the message 'Warning message: attributes are not identical across measure variables; they will be dropped' ... and then if I ignore it my output boxplots are incorrect, any idea as to why? – Natway May 22 '19 at 01:25
  • Does this occur when you use the sample data.frame I suggested in this answer, or is this using your own data? If it is your own data, please post a sample of it. It is most likely due additional variables in the data frame. If there is another variable you dont want to include in the gather process then do the following: `gather(Dat,key="Season",value="Value",-unwantedVariable1, -unwantedvariable2)`. You can add as many `-variable` parameters as you require – JMilner May 22 '19 at 01:31
0

You can use ggplot2 and data.table, I think it is easier, here is the code:

library(data.table)
library(ggplot2)
dat <- data.table(Spring = c(runif(9,0,1),2),
                  Summer = runif(10,0,1),
                  Autumn = runif(10,0,1),
                  Winter = runif(10,0,1))
dat1 = melt(dat)

ggplot(data=dat1,aes(x=variable,y=value)) +geom_boxplot(outlier.colour = "red")
ggplot(data=dat1,aes(x=variable,y=value,colour=variable)) +geom_boxplot() 

boxplot by group

Deng Fei
  • 11
  • 3