Quantcast

Boxplot Fill Pattern

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Boxplot Fill Pattern

Gabriel Yospin
Hello R Help!

I would like to make a legible boxplot of tree growth rates for each of
seven tree species at each of seven different sites. It's a lot of data to
put on one figure, I know. I made a beautiful, interpretable figure using
color, but my target journal can't deal with color figures. I can use seven
shades of grey to fill the boxes, but the figure then becomes
uninterpretable - the colors of adjacent boxes are too similar. I cannot
figure out how to add a pattern, hatching, or cross-hatching to the boxes.

I see that in 2003 Professor Ripley confirmed that one cannot hatch
boxplots:

http://tolstoy.newcastle.edu.au/R/help/03a/2622.html

Is this still true? If so, does anyone have a suggestion for how to make
this figure interpretable in black and white? Should I pick a different
target journal?

Many thanks,

Gabe

Functional code to make the color figure with fake data follows:
#
leg.txt = c("Abies grandis","Acer macrophyllum","Calocedrus
decurrens","Pinus ponderosa","Pseudotsuga meziensii","Quercus
garryana","Quercus kelloggii")
site.txt = c("Brownsville","Chip Ross","Finley","Jim's
Creek","Lowell","Mount Pisgah","South Eugene")
colors = c("gray","red","white","blue","yellow","purple","orange")
# Fake data here:
site = rep(site.txt, each = 21)
sp = rep(rep(leg.txt, each = 3), times = 7)
ga = runif(147, 0, 20)
datnew.lo = data.frame(site,sp,ga)
# Now make the plot:
boxplot(ga~sp*site,data=datnew.lo, range = 1,
        col = colors,
        ylim = c(0,30),
        xaxt = "n",
        xlab = "Site",
        ylab = "Basal Area Growth Increment",
        main = "Basal Area Growth Increment by Site and Species")
axis(1, at = c(4,11,18,25,32,39,46),
     labels = site.txt,
     )
abline(v = 7.5, lty = 3)
abline(v = 14.5, lty = 3)
abline(v = 21.5, lty = 3)
abline(v = 28.5, lty = 3)
abline(v = 35.5, lty = 3)
abline(v = 42.5, lty = 3)
legend("topright", legend = leg.txt, fill = colors, bg = "white")

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Boxplot Fill Pattern

Michael Weylandt
On Thu, Mar 8, 2012 at 1:08 PM, Gabriel Yospin <[hidden email]> wrote:
> Hello R Help!

Hello Gabe Yospin! (I feel like I should start playing some arena rock
anthem now ;-) )

>
> I would like to make a legible boxplot of tree growth rates for each of
> seven tree species at each of seven different sites. It's a lot of data to
> put on one figure, I know. I made a beautiful, interpretable figure using
> color, but my target journal can't deal with color figures. I can use seven
> shades of grey to fill the boxes, but the figure then becomes
> uninterpretable - the colors of adjacent boxes are too similar. I cannot
> figure out how to add a pattern, hatching, or cross-hatching to the boxes.
>
> I see that in 2003 Professor Ripley confirmed that one cannot hatch
> boxplots:
>
> http://tolstoy.newcastle.edu.au/R/help/03a/2622.html
>
> Is this still true? If so, does anyone have a suggestion for how to make
> this figure interpretable in black and white? Should I pick a different
> target journal?
>

I don't think you'd need to change journals just for graphical styles:
do it for much less important things, like impact factor. It sounds
like the idea of small multiples might help here: I'm not a lattice
pro, but here's something you could do in ggplot2 (and I know it's
doable in lattice as well):

I'm gonna put new lines in all your species names since space will be
at a premium:

levels(datnew.lo$sp) <- gsub(" ", "\n", levels(datnew.lo$sp))

library(ggplot2)
ggplot(datnew.lo, aes(x = sp, y = ga)) + geom_boxplot() +
facet_wrap(~site, nrow = 2) + opts(axis.text.x=theme_text(angle=45,
size = 7))

Others with more ggplot / lattice - fu than I can help you tweak this
but hopefully this is a start.

Michael


> Many thanks,
>
> Gabe
>
> Functional code to make the color figure with fake data follows:
> #
> leg.txt = c("Abies grandis","Acer macrophyllum","Calocedrus
> decurrens","Pinus ponderosa","Pseudotsuga meziensii","Quercus
> garryana","Quercus kelloggii")
> site.txt = c("Brownsville","Chip Ross","Finley","Jim's
> Creek","Lowell","Mount Pisgah","South Eugene")
> colors = c("gray","red","white","blue","yellow","purple","orange")
> # Fake data here:
> site = rep(site.txt, each = 21)
> sp = rep(rep(leg.txt, each = 3), times = 7)
> ga = runif(147, 0, 20)
> datnew.lo = data.frame(site,sp,ga)
> # Now make the plot:
> boxplot(ga~sp*site,data=datnew.lo, range = 1,
>        col = colors,
>        ylim = c(0,30),
>        xaxt = "n",
>        xlab = "Site",
>        ylab = "Basal Area Growth Increment",
>        main = "Basal Area Growth Increment by Site and Species")
> axis(1, at = c(4,11,18,25,32,39,46),
>     labels = site.txt,
>     )
> abline(v = 7.5, lty = 3)
> abline(v = 14.5, lty = 3)
> abline(v = 21.5, lty = 3)
> abline(v = 28.5, lty = 3)
> abline(v = 35.5, lty = 3)
> abline(v = 42.5, lty = 3)
> legend("topright", legend = leg.txt, fill = colors, bg = "white")

Thanks for the great reproducible example! Made this much easier and more fun.

>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Boxplot Fill Pattern

Gabriel Yospin
Thanks for the help, Michael. ggplot2 is an interesting package. It would
nice if there were better documentation in the help files, but the (fully
functional) code (with fake data) I've settled on is below. I am
particularly pleased with the theme_bw, which will use less ink than the
default theme, the alignment of the x-grid labels using hjust = 1, and the
y-axis label, which finally has the superscript it requires!

Per your suggestion, I wrote this code while listening to a variety of
classic rock ballads - it was extremely helpful.

- Gabe

site.txt = c("Brownsville","Chip Ross","Finley","Jim's
Creek","Lowell","Mount Pisgah","South Eugene")
leg.txt = c("Abies grandis","Acer macrophyllum","Calocedrus
decurrens","Pinus ponderosa","Pseudotsuga meziensii","Quercus
garryana","Quercus kelloggii")
#
# Fake Data
site = rep(site.txt, each = 21)
sp = rep(rep(leg.txt, each = 3), times = 7)
ga = runif(147, 0, 20)
datnew.lo = data.frame(site,sp,ga)
#
# Make the plot
library(ggplot2)
theme_set(theme_bw())
ggplot(datnew.lo, aes(x = sp, y = ga)) + geom_boxplot() +
facet_wrap(~site, ncol = 7) +
opts(axis.text.x=theme_text(angle=90, size = 8, hjust = 1)) +
xlab("Species") +
ylab(expression(paste('Basal Area Growth Increment (mm  '^{2},')')))

On Thu, Mar 8, 2012 at 12:22 PM, R. Michael Weylandt <
[hidden email]> wrote:

> On Thu, Mar 8, 2012 at 1:08 PM, Gabriel Yospin <[hidden email]> wrote:
> > Hello R Help!
>
> Hello Gabe Yospin! (I feel like I should start playing some arena rock
> anthem now ;-) )
>
> >
> > I would like to make a legible boxplot of tree growth rates for each of
> > seven tree species at each of seven different sites. It's a lot of data
> to
> > put on one figure, I know. I made a beautiful, interpretable figure using
> > color, but my target journal can't deal with color figures. I can use
> seven
> > shades of grey to fill the boxes, but the figure then becomes
> > uninterpretable - the colors of adjacent boxes are too similar. I cannot
> > figure out how to add a pattern, hatching, or cross-hatching to the
> boxes.
> >
> > I see that in 2003 Professor Ripley confirmed that one cannot hatch
> > boxplots:
> >
> > http://tolstoy.newcastle.edu.au/R/help/03a/2622.html
> >
> > Is this still true? If so, does anyone have a suggestion for how to make
> > this figure interpretable in black and white? Should I pick a different
> > target journal?
> >
>
> I don't think you'd need to change journals just for graphical styles:
> do it for much less important things, like impact factor. It sounds
> like the idea of small multiples might help here: I'm not a lattice
> pro, but here's something you could do in ggplot2 (and I know it's
> doable in lattice as well):
>
> I'm gonna put new lines in all your species names since space will be
> at a premium:
>
> levels(datnew.lo$sp) <- gsub(" ", "\n", levels(datnew.lo$sp))
>
> library(ggplot2)
> ggplot(datnew.lo, aes(x = sp, y = ga)) + geom_boxplot() +
> facet_wrap(~site, nrow = 2) + opts(axis.text.x=theme_text(angle=45,
> size = 7))
>
> Others with more ggplot / lattice - fu than I can help you tweak this
> but hopefully this is a start.
>
> Michael
>
>
> > Many thanks,
> >
> > Gabe
> >
> > Functional code to make the color figure with fake data follows:
> > #
> > leg.txt = c("Abies grandis","Acer macrophyllum","Calocedrus
> > decurrens","Pinus ponderosa","Pseudotsuga meziensii","Quercus
> > garryana","Quercus kelloggii")
> > site.txt = c("Brownsville","Chip Ross","Finley","Jim's
> > Creek","Lowell","Mount Pisgah","South Eugene")
> > colors = c("gray","red","white","blue","yellow","purple","orange")
> > # Fake data here:
> > site = rep(site.txt, each = 21)
> > sp = rep(rep(leg.txt, each = 3), times = 7)
> > ga = runif(147, 0, 20)
> > datnew.lo = data.frame(site,sp,ga)
> > # Now make the plot:
> > boxplot(ga~sp*site,data=datnew.lo, range = 1,
> >        col = colors,
> >        ylim = c(0,30),
> >        xaxt = "n",
> >        xlab = "Site",
> >        ylab = "Basal Area Growth Increment",
> >        main = "Basal Area Growth Increment by Site and Species")
> > axis(1, at = c(4,11,18,25,32,39,46),
> >     labels = site.txt,
> >     )
> > abline(v = 7.5, lty = 3)
> > abline(v = 14.5, lty = 3)
> > abline(v = 21.5, lty = 3)
> > abline(v = 28.5, lty = 3)
> > abline(v = 35.5, lty = 3)
> > abline(v = 42.5, lty = 3)
> > legend("topright", legend = leg.txt, fill = colors, bg = "white")
>
> Thanks for the great reproducible example! Made this much easier and more
> fun.
>
> >
> >        [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Boxplot Fill Pattern

Michael Weylandt
It looks very nice -- hopefully your reviewers agree.

The built in documentation is a little sparse (though greatly enhanced
in the newest 0.9.0 release) -- but Hadley's website
http://had.co.nz/ggplot2/ is very good and there's a ggplot2 book
available on Amazon (though it's a little out of date -- I believe
Hadley hinted at some point that after one more big release for
ggplot2 he'd think about rewriting the book)

Michael

On Thu, Mar 8, 2012 at 6:52 PM, Gabriel Yospin <[hidden email]> wrote:

> Thanks for the help, Michael. ggplot2 is an interesting package. It would
> nice if there were better documentation in the help files, but the (fully
> functional) code (with fake data) I've settled on is below. I am
> particularly pleased with the theme_bw, which will use less ink than the
> default theme, the alignment of the x-grid labels using hjust = 1, and the
> y-axis label, which finally has the superscript it requires!
>
> Per your suggestion, I wrote this code while listening to a variety of
> classic rock ballads - it was extremely helpful.
>
> - Gabe
>
> site.txt = c("Brownsville","Chip Ross","Finley","Jim's
> Creek","Lowell","Mount Pisgah","South Eugene")
> leg.txt = c("Abies grandis","Acer macrophyllum","Calocedrus
> decurrens","Pinus ponderosa","Pseudotsuga meziensii","Quercus
> garryana","Quercus kelloggii")
> #
> # Fake Data
> site = rep(site.txt, each = 21)
> sp = rep(rep(leg.txt, each = 3), times = 7)
> ga = runif(147, 0, 20)
> datnew.lo = data.frame(site,sp,ga)
> #
> # Make the plot
> library(ggplot2)
> theme_set(theme_bw())
> ggplot(datnew.lo, aes(x = sp, y = ga)) + geom_boxplot() +
> facet_wrap(~site, ncol = 7) +
> opts(axis.text.x=theme_text(angle=90, size = 8, hjust = 1)) +
> xlab("Species") +
> ylab(expression(paste('Basal Area Growth Increment (mm  '^{2},')')))
>
> On Thu, Mar 8, 2012 at 12:22 PM, R. Michael Weylandt
> <[hidden email]> wrote:
>>
>> On Thu, Mar 8, 2012 at 1:08 PM, Gabriel Yospin <[hidden email]> wrote:
>> > Hello R Help!
>>
>> Hello Gabe Yospin! (I feel like I should start playing some arena rock
>> anthem now ;-) )
>>
>> >
>> > I would like to make a legible boxplot of tree growth rates for each of
>> > seven tree species at each of seven different sites. It's a lot of data
>> > to
>> > put on one figure, I know. I made a beautiful, interpretable figure
>> > using
>> > color, but my target journal can't deal with color figures. I can use
>> > seven
>> > shades of grey to fill the boxes, but the figure then becomes
>> > uninterpretable - the colors of adjacent boxes are too similar. I cannot
>> > figure out how to add a pattern, hatching, or cross-hatching to the
>> > boxes.
>> >
>> > I see that in 2003 Professor Ripley confirmed that one cannot hatch
>> > boxplots:
>> >
>> > http://tolstoy.newcastle.edu.au/R/help/03a/2622.html
>> >
>> > Is this still true? If so, does anyone have a suggestion for how to make
>> > this figure interpretable in black and white? Should I pick a different
>> > target journal?
>> >
>>
>> I don't think you'd need to change journals just for graphical styles:
>> do it for much less important things, like impact factor. It sounds
>> like the idea of small multiples might help here: I'm not a lattice
>> pro, but here's something you could do in ggplot2 (and I know it's
>> doable in lattice as well):
>>
>> I'm gonna put new lines in all your species names since space will be
>> at a premium:
>>
>> levels(datnew.lo$sp) <- gsub(" ", "\n", levels(datnew.lo$sp))
>>
>> library(ggplot2)
>> ggplot(datnew.lo, aes(x = sp, y = ga)) + geom_boxplot() +
>> facet_wrap(~site, nrow = 2) + opts(axis.text.x=theme_text(angle=45,
>> size = 7))
>>
>> Others with more ggplot / lattice - fu than I can help you tweak this
>> but hopefully this is a start.
>>
>> Michael
>>
>>
>> > Many thanks,
>> >
>> > Gabe
>> >
>> > Functional code to make the color figure with fake data follows:
>> > #
>> > leg.txt = c("Abies grandis","Acer macrophyllum","Calocedrus
>> > decurrens","Pinus ponderosa","Pseudotsuga meziensii","Quercus
>> > garryana","Quercus kelloggii")
>> > site.txt = c("Brownsville","Chip Ross","Finley","Jim's
>> > Creek","Lowell","Mount Pisgah","South Eugene")
>> > colors = c("gray","red","white","blue","yellow","purple","orange")
>> > # Fake data here:
>> > site = rep(site.txt, each = 21)
>> > sp = rep(rep(leg.txt, each = 3), times = 7)
>> > ga = runif(147, 0, 20)
>> > datnew.lo = data.frame(site,sp,ga)
>> > # Now make the plot:
>> > boxplot(ga~sp*site,data=datnew.lo, range = 1,
>> >        col = colors,
>> >        ylim = c(0,30),
>> >        xaxt = "n",
>> >        xlab = "Site",
>> >        ylab = "Basal Area Growth Increment",
>> >        main = "Basal Area Growth Increment by Site and Species")
>> > axis(1, at = c(4,11,18,25,32,39,46),
>> >     labels = site.txt,
>> >     )
>> > abline(v = 7.5, lty = 3)
>> > abline(v = 14.5, lty = 3)
>> > abline(v = 21.5, lty = 3)
>> > abline(v = 28.5, lty = 3)
>> > abline(v = 35.5, lty = 3)
>> > abline(v = 42.5, lty = 3)
>> > legend("topright", legend = leg.txt, fill = colors, bg = "white")
>>
>> Thanks for the great reproducible example! Made this much easier and more
>> fun.
>>
>> >
>> >        [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > [hidden email] mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Boxplot Fill Pattern

Michael Friendly
In reply to this post by Gabriel Yospin
On 3/8/2012 1:08 PM, Gabriel Yospin wrote:
> I would like to make a legible boxplot of tree growth rates for each of
> seven tree species at each of seven different sites. It's a lot of data to
> put on one figure, I know. I made a beautiful, interpretable figure using
> color, but my target journal can't deal with color figures. I can use seven
> shades of grey to fill the boxes, but the figure then becomes

If you print your original figure (with the legend) in B/W, you'll see
that the shading levels are not too bad -- they are relatively
distinguishable except for Acer and Pinus/

Thus, one thing I often do in this situation is design a graph so that
it will render reasonably well also in B/W and suggest to the journal
to make a color version available online (or I put it on my own web).

Your final version using ggplot2 is a far worse graph, IMHO
because the labels are illegible, and using no fill in the boxplots
makes it impossible to distinguish the same species across sites.
You could
make it better by
(a) only labeling the species in alternate site panels
(b) rotating those labels by 45^o
(c) using some fill for the boxes

My .05

--
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept.
York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Loading...