Skip to content

Vertical boxplot with a continuous X axis

5 messages · Richard M. Heiberger, Bert Gunter, Sebastien Bihorel

#
Yes, this is exactly what the panel function
    panel.bwplot.intermediate.hh
along with the
    position()
function in the HH package was designed for.

Continuing with the example in the linked stackoverflow

df <- structure(list(X1 = c(67.0785968921204, 45.5968692796599,
36.9528452430474,
59.0160838607585, 50.0432724395945, 44.3381091105162, 57.9705240678336,
52.5298724133533, 62.0004216014734, 54.1111551461596), X2 = c(66.4508598009841,
46.9692156851792, 37.1419457255043, 60.0582991817961, 50.7717368681294,
44.6962314013419, 57.5490276313784, 52.6394305113891, 62.9297233041122,
56.8151766642767), X3 = c(66.4517425831512, 46.2946539468733,
36.5946733567535, 59.2477934854157, 49.1558840130484, 44.7507905380111,
59.1132983272444, 53.710627728232, 61.7923277906642, 57.5999862897015
), X4 = c(66.1516449763315, 45.4660590159847, 37.2239262718906,
59.2975530712561, 50.2546321578291, 44.7220452966667, 59.8879656465763,
52.321734919241, 62.0802304973764, 56.6507005349853), X5 = c(66.1810558292955,
46.3301985628267, 36.4487743101244, 60.054119632362, 49.1593136549535,
44.5708909518076, 58.5865142665164, 52.5527273219855, 61.3749185309236,
54.1823379401272), X6 = c(65.9530929286517, 45.5120010675769,
36.7924160587984, 58.9428613519645, 50.3412809263164, 44.9671678827697,
57.8718260182012, 51.8954544252633, 62.0173019998447, 56.3833840769146
), X7 = c(66.3862581408135, 46.5872469340431, 36.7585555977493,
58.1374309578563, 50.3399735165261, 44.5739565876491, 57.5245695195136,
52.7613488669329, 61.2500297922529, 55.9202360622414), X8 = c(67.5577910713347,
46.1891742544371, 36.4689522649804, 59.5271358261971, 49.6776114214636,
44.1995317742719, 58.4881363877987, 52.1946266979144, 62.1149998459759,
55.3748655464147), X9 = c(66.3943390258844, 45.843835703738,
37.3485122393333, 59.8591304277037, 49.387883195468, 44.4283817056918,
58.1874530826789, 53.5091378916001, 62.1187451212786, 55.3632760142297
), X10 = c(66.9748072219828, 46.20735965374, 36.7069272963502,
59.5035069226904, 48.8446530329762, 44.8753686249692, 58.0223695284058,
53.2732448917674, 61.2509571513, 54.9615424261546), age = c(55,
37, 31, 59, 49, 47, 69, 68, 69, 66)), .Names = c("X1", "X2",
"X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10", "age"), row.names =
c("GSM1051533_7800246024_R03C02",
"GSM1051534_7800246024_R04C02", "GSM1051535_7800246024_R05C02",
"GSM1051536_7800246024_R06C02", "GSM1051537_7800246085_R01C01",
"GSM1051538_7800246085_R02C01", "GSM1051539_7800246085_R03C01",
"GSM1051540_7800246085_R04C01", "GSM1051541_7800246085_R05C01",
"GSM1051542_7800246085_R06C01"), class = "data.frame")

##  install.packages("HH")  ## if necessary
library(HH)

df.melt <- reshape2::melt(df, id.vars=c("age"))
df.melt$age.pos <- factor(df.melt$age)
position(df.melt$age.pos) <- levels(df.melt$age.pos)


## three options of x ticks and color

bwplot(value ~ age.pos, data=df.melt,
       horizontal=FALSE,
       panel=panel.bwplot.intermediate.hh,
       scales=list(
         x=list(
           at=position(df.melt$age.pos), ## placement of tick labels and marks
           limits=c(29, 71),             ## x limits
           tck=1)),                      ## draw tick marks
       xlab="age",
       main=list("value ~ age", cex=1.4))

bwplot(value ~ age.pos, data=df.melt,
       horizontal=FALSE,
       panel=panel.bwplot.intermediate.hh,
       scales=list(
         x=list(
        ## at=position(df.melt$age.pos), ## placement of tick labels and marks
           limits=c(29, 71),             ## x limits
           tck=1)),                      ## draw tick marks
       xlab="age",
       main=list("value ~ age", cex=1.4))

bwplot(value ~ age.pos, data=df.melt,
       horizontal=FALSE,
       panel=panel.bwplot.intermediate.hh,
       col="blue",                       ## constant color
       scales=list(
         x=list(
       ##  at=position(df.melt$age.pos), ## placement of tick labels and marks
           limits=c(29, 71),             ## x limits
           tck=1)),                      ## draw tick marks
       xlab="age",
       main=list("value ~ age", cex=1.4))

On Thu, Feb 23, 2017 at 11:52 PM, Sebastien Bihorel
<sebastien.bihorel at cognigencorp.com> wrote:
#
Sebastien:

The linked post is unclear: two of the rows have the same age, so
should there be 10 boxplots for the 10 rows or 9 for the 9 different
ages? I assumed the latter, as otherwise how could one disciminate
rows with the same age that have overlapping values?

For lattice, I just used age as the group= parameter to group by ages
and used panel.bwplot for the panel.groups function. My "aesthetics"
are different than shown in the linked post, but can be altered in
lattice through the panel.bwplot and par.settings parameters as
described in ?panel.bwplot:

   y <- unlist(df[,-11])
   age <- rep(df[,"age"],10) ## Could be done programatically
   xyplot(y~age, groups = age,
          panel = function(...){
             panel.abline(h = seq(40,65, by=5),
                          v = seq(30,70, by =5),
                          col="lightgray")
             panel.superpose(...)
          },
          panel.groups = function(x,y,...)
                panel.bwplot(x,y,horiz=FALSE,fill = "lightgray",
                        pch = 16, cex=.7,col= "black",box.width = 1.5),
          par.settings =list(box.rectangle = list(col="black"))
   )

However, note that this too could produce a hopeless mishmosh if the
ages are close together so that the boxplots overlap; or you could end
up getting nearly invisible skinny boxes, as in the plot shown in the
linked post.  So on the whole, I think you are better off treating the
ages as a factor and thereby separating them, e.g.

   bwplot(y~age, fill = "lightgray", horiz = FALSE,
          panel = function(...){
             panel.abline(h = seq(40,65, by=5),col="lightgray")
             panel.bwplot(...)
             },
          scales = list(x= list(lab = sort(unique(age)))),
          par.settings =list(box.rectangle = list(col="black"))
   )



Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Feb 23, 2017 at 8:52 PM, Sebastien Bihorel
<sebastien.bihorel at cognigencorp.com> wrote:
#
Thanks for your reply

----- Original Message -----
From: "Richard M. Heiberger" <rmh at temple.edu>
To: "Sebastien Bihorel" <sebastien.bihorel at cognigencorp.com>
Cc: "r-help" <r-help at r-project.org>
Sent: Friday, February 24, 2017 1:10:44 AM
Subject: Re: [R] Vertical boxplot with a continuous X axis

Yes, this is exactly what the panel function
    panel.bwplot.intermediate.hh
along with the
    position()
function in the HH package was designed for.

Continuing with the example in the linked stackoverflow

df <- structure(list(X1 = c(67.0785968921204, 45.5968692796599,
36.9528452430474,
59.0160838607585, 50.0432724395945, 44.3381091105162, 57.9705240678336,
52.5298724133533, 62.0004216014734, 54.1111551461596), X2 = c(66.4508598009841,
46.9692156851792, 37.1419457255043, 60.0582991817961, 50.7717368681294,
44.6962314013419, 57.5490276313784, 52.6394305113891, 62.9297233041122,
56.8151766642767), X3 = c(66.4517425831512, 46.2946539468733,
36.5946733567535, 59.2477934854157, 49.1558840130484, 44.7507905380111,
59.1132983272444, 53.710627728232, 61.7923277906642, 57.5999862897015
), X4 = c(66.1516449763315, 45.4660590159847, 37.2239262718906,
59.2975530712561, 50.2546321578291, 44.7220452966667, 59.8879656465763,
52.321734919241, 62.0802304973764, 56.6507005349853), X5 = c(66.1810558292955,
46.3301985628267, 36.4487743101244, 60.054119632362, 49.1593136549535,
44.5708909518076, 58.5865142665164, 52.5527273219855, 61.3749185309236,
54.1823379401272), X6 = c(65.9530929286517, 45.5120010675769,
36.7924160587984, 58.9428613519645, 50.3412809263164, 44.9671678827697,
57.8718260182012, 51.8954544252633, 62.0173019998447, 56.3833840769146
), X7 = c(66.3862581408135, 46.5872469340431, 36.7585555977493,
58.1374309578563, 50.3399735165261, 44.5739565876491, 57.5245695195136,
52.7613488669329, 61.2500297922529, 55.9202360622414), X8 = c(67.5577910713347,
46.1891742544371, 36.4689522649804, 59.5271358261971, 49.6776114214636,
44.1995317742719, 58.4881363877987, 52.1946266979144, 62.1149998459759,
55.3748655464147), X9 = c(66.3943390258844, 45.843835703738,
37.3485122393333, 59.8591304277037, 49.387883195468, 44.4283817056918,
58.1874530826789, 53.5091378916001, 62.1187451212786, 55.3632760142297
), X10 = c(66.9748072219828, 46.20735965374, 36.7069272963502,
59.5035069226904, 48.8446530329762, 44.8753686249692, 58.0223695284058,
53.2732448917674, 61.2509571513, 54.9615424261546), age = c(55,
37, 31, 59, 49, 47, 69, 68, 69, 66)), .Names = c("X1", "X2",
"X3", "X4", "X5", "X6", "X7", "X8", "X9", "X10", "age"), row.names =
c("GSM1051533_7800246024_R03C02",
"GSM1051534_7800246024_R04C02", "GSM1051535_7800246024_R05C02",
"GSM1051536_7800246024_R06C02", "GSM1051537_7800246085_R01C01",
"GSM1051538_7800246085_R02C01", "GSM1051539_7800246085_R03C01",
"GSM1051540_7800246085_R04C01", "GSM1051541_7800246085_R05C01",
"GSM1051542_7800246085_R06C01"), class = "data.frame")

##  install.packages("HH")  ## if necessary
library(HH)

df.melt <- reshape2::melt(df, id.vars=c("age"))
df.melt$age.pos <- factor(df.melt$age)
position(df.melt$age.pos) <- levels(df.melt$age.pos)


## three options of x ticks and color

bwplot(value ~ age.pos, data=df.melt,
       horizontal=FALSE,
       panel=panel.bwplot.intermediate.hh,
       scales=list(
         x=list(
           at=position(df.melt$age.pos), ## placement of tick labels and marks
           limits=c(29, 71),             ## x limits
           tck=1)),                      ## draw tick marks
       xlab="age",
       main=list("value ~ age", cex=1.4))

bwplot(value ~ age.pos, data=df.melt,
       horizontal=FALSE,
       panel=panel.bwplot.intermediate.hh,
       scales=list(
         x=list(
        ## at=position(df.melt$age.pos), ## placement of tick labels and marks
           limits=c(29, 71),             ## x limits
           tck=1)),                      ## draw tick marks
       xlab="age",
       main=list("value ~ age", cex=1.4))

bwplot(value ~ age.pos, data=df.melt,
       horizontal=FALSE,
       panel=panel.bwplot.intermediate.hh,
       col="blue",                       ## constant color
       scales=list(
         x=list(
       ##  at=position(df.melt$age.pos), ## placement of tick labels and marks
           limits=c(29, 71),             ## x limits
           tck=1)),                      ## draw tick marks
       xlab="age",
       main=list("value ~ age", cex=1.4))

On Thu, Feb 23, 2017 at 11:52 PM, Sebastien Bihorel
<sebastien.bihorel at cognigencorp.com> wrote:
#
Thanks for your reply

----- Original Message -----
From: "Bert Gunter" <bgunter.4567 at gmail.com>
To: "Sebastien Bihorel" <sebastien.bihorel at cognigencorp.com>
Cc: "R-help" <r-help at r-project.org>
Sent: Friday, February 24, 2017 2:01:36 AM
Subject: Re: [R] Vertical boxplot with a continuous X axis

Sebastien:

The linked post is unclear: two of the rows have the same age, so
should there be 10 boxplots for the 10 rows or 9 for the 9 different
ages? I assumed the latter, as otherwise how could one disciminate
rows with the same age that have overlapping values?

For lattice, I just used age as the group= parameter to group by ages
and used panel.bwplot for the panel.groups function. My "aesthetics"
are different than shown in the linked post, but can be altered in
lattice through the panel.bwplot and par.settings parameters as
described in ?panel.bwplot:

   y <- unlist(df[,-11])
   age <- rep(df[,"age"],10) ## Could be done programatically
   xyplot(y~age, groups = age,
          panel = function(...){
             panel.abline(h = seq(40,65, by=5),
                          v = seq(30,70, by =5),
                          col="lightgray")
             panel.superpose(...)
          },
          panel.groups = function(x,y,...)
                panel.bwplot(x,y,horiz=FALSE,fill = "lightgray",
                        pch = 16, cex=.7,col= "black",box.width = 1.5),
          par.settings =list(box.rectangle = list(col="black"))
   )

However, note that this too could produce a hopeless mishmosh if the
ages are close together so that the boxplots overlap; or you could end
up getting nearly invisible skinny boxes, as in the plot shown in the
linked post.  So on the whole, I think you are better off treating the
ages as a factor and thereby separating them, e.g.

   bwplot(y~age, fill = "lightgray", horiz = FALSE,
          panel = function(...){
             panel.abline(h = seq(40,65, by=5),col="lightgray")
             panel.bwplot(...)
             },
          scales = list(x= list(lab = sort(unique(age)))),
          par.settings =list(box.rectangle = list(col="black"))
   )



Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Thu, Feb 23, 2017 at 8:52 PM, Sebastien Bihorel
<sebastien.bihorel at cognigencorp.com> wrote: