Change whisker color in boxplots?

674 views
Skip to first unread message

Nicholas Lewin-Koh

unread,
Jun 7, 2009, 9:50:50 PM6/7/09
to ggplot2
library(hexbin)
data(NHANES)
p<-qplot(Smoke,BMI,data=NHANES,facets=~Race,geom=c("jitter"),alpha=.2)
p + geom_boxplot(outlier.colour="white",outlier.size=.1,colour="red")

actually, I want to suppress the outlier points here, and have the
whiskers the same colour
as the box. Do I need to set some options? Intuitively I thought that
would be the same
colour as the border.

Thanks
Nicholas

learnr

unread,
Jun 8, 2009, 6:47:49 AM6/8/09
to ggplot2
Using ggplot gives better control over individual layers.
Try this:

ggplot(NHANES, aes(Smoke,BMI)) +
geom_jitter(alpha = 0.2) +
geom_boxplot(fill = NA, outlier.colour="white", outlier.size=0,
colour="red") +
facet_grid(~Race)
--
http://learnr.wordpress.com/

Nicholas Lewin-Koh

unread,
Jun 8, 2009, 12:18:18 PM6/8/09
to learnr, ggplot2
Hi,
So why in the first example I gave are the whiskers affected by
the level of alpha in qplot, but not using ggplot? At least from my
reading of the docs
I would assume that the controls for geom_boxplot should be separate?
whereas something like
p<-qplot(Smoke,BMI,data=NHANES,facets=~Race,geom=c("jitter","boxplot"),alpha=.2)
p
would have the same alpha for both jitter and the boxplot, but the whole
boxplot
not just the whiskers. Is this not a buglet?

Nicholas

hadley wickham

unread,
Jun 8, 2009, 3:37:11 PM6/8/09
to Nicholas Lewin-Koh, learnr, ggplot2
On Mon, Jun 8, 2009 at 11:18 AM, Nicholas Lewin-Koh<ni...@hailmail.net> wrote:
>
> Hi,
> So why in the first example I gave are the whiskers affected by
> the level of alpha in qplot, but not using ggplot? At least from my
> reading of the docs
> I would assume that the controls for geom_boxplot should be separate?
> whereas something like
> p<-qplot(Smoke,BMI,data=NHANES,facets=~Race,geom=c("jitter","boxplot"),alpha=.2)
> p
> would have the same alpha for both jitter and the boxplot, but the whole
> boxplot
> not just the whiskers. Is this not a buglet?

It's setting vs. mapping again.

qplot(Smoke,BMI,data=NHANES,facets=~Race,geom=c("jitter"),alpha=.2)

is equivalent to:

ggplot(NHANES, aes(Smoke, BMI, alpha = 0.2)) +
geom_jitter() +
facet_wrap(~ Race)

so when you add on the boxplot the alpha is inherited. What you
probably want is to _set_ the value of alpha:

qplot(Smoke,BMI,data=NHANES,facets=~Race,geom=c("jitter"),alpha=I(.2))

which is equivalent to

ggplot(NHANES, aes(Smoke, BMI)) +
geom_jitter(alpha = 0.2) +
facet_wrap(~ Race)

p + geom_boxplot(outlier.colour="white",outlier.size=.1,colour="red")

Hadley

--
http://had.co.nz/

Nicholas Lewin-Koh

unread,
Jun 8, 2009, 4:05:20 PM6/8/09
to hadley wickham, learnr, ggplot2
Hi Hadley,
Yes, I get your example and it makes sense, thanks. The odd thing is it
is just the whiskers
that inherit alpha not the boxes, is that intentional?

Nicholas


On Mon, 08 Jun 2009 14:37 -0500, "hadley wickham" <h.wi...@gmail.com>
wrote:

hadley wickham

unread,
Jun 8, 2009, 4:42:39 PM6/8/09
to Nicholas Lewin-Koh, learnr, ggplot2
Hmmm. The definition of alpha is that it applies to fill colour if
the geom has a fill, otherwise it applies to line colour. There's a
bit of a conflict here, because the boxplot is made up of filled and
non-filled geoms. Alpha will only affect the fill in the next
version.

Hadley

PS. Are you preparing for your meetup talk?

--
http://had.co.nz/

Reply all
Reply to author
Forward
0 new messages