[R] plotting box plots on same x

Marc Schwartz
Fri, 27 May 2005 15:10:58 -0000

On Fri, 2005-05-27 at 10:17 -0400, BJ wrote:
> I am trying to construct a graph of 6 box plots of blood pressures. I
> want them to be on a single set of axis and I want the SBP to be ontop
> of the DBP. I have an array bp with the data in it and I tried
>
> a[1,]<-c(145,60,147,62,140,57)
> a[2,]<-c(160,75,160,74,160,70)
> a[3,]<-c(140,55,140,65,142,55)
>  boxplot(data.frame(a), main = "Blood Pressures",
at=c(1,1,2,2,3,3),
>
names=c("sit","","lie","","stand",""))
>
> which is close to what I want, but it gives me a bunch of empty space at
> the end. is there a better way to do this to avoid this?
>
> As always, Thank You. ~Erithid

To answer both of your posts, use the following:

# Review how your data is structured above. Your code for creating "a"
# is not replicable. For those lacking clinical insight, explaining
# your acronyms would also be helpful...  :-)

SBP <- matrix(c(145, 160, 140,
                147, 160, 140,
                140, 160, 142), ncol = 3)

DBP <- matrix(c(60, 75, 55,
                62, 74, 65,
                57, 70, 55), ncol = 3)

colnames(SBP) <- colnames(DBP) <-
c("sit","lie","stand")

# The key here is to only plot three at a time, lest boxplot()
# default to a 'xlim' of 0.5 to 6.5 (1:# of groups +/- 0.5)
# Then use 'add = TRUE' to plot the second group of 3
# Note also that I set the 'ylim' to the range of the combined
# values in the first plot.

boxplot(data.frame(SBP), main = "Blood Pressures",
        ylim = range(c(SBP, DBP)),
        whisklty = 0, staplelty = 0)

boxplot(data.frame(DBP), add = TRUE, whisklty = 0, staplelty = 0)

Note the final two arguments, which result in the whiskers being drawn
with an "invisible" line.

See ?boxplot and ?bxp for more information.

HTH,

Marc Schwartz