Histogram Outline Colour

388 views
Skip to first unread message

Nick S

unread,
Jun 30, 2010, 2:56:03 PM6/30/10
to ggplot2
I am exploring the use of some example code I stumbled across recently
which produces back-to-back histograms.

I am having trouble figuring out how to highlight/outline the bins in
black on the top portion of the graph using a call to the colour
parameter. I can't seem to find an example of what I want to do in the
documentation. Perhaps I have just missed it.

x data can be found here: http://dl.dropbox.com/u/3734701/x.csv
y data can be found here: http://dl.dropbox.com/u/3734701/y.csv

When I try to add the following code...I get the following behavior
(http://dl.dropbox.com/u/3734701/back-to-back_2.jpg). I would like
both histograms to have black outlines for the bins.

qplot(x,geom="histogram", fill="Pre-BBXL",
binwidth=0.25,colour="black") +
geom_histogram(data=data.frame(x=y),aes(fill="Post-BBXL",
y=-..count..),binwidth= 0.25,colour="black") +
labs(x="ODs",y="count",fill="") +
xlim(0,14) +
scale_fill_brewer("Set1")

Any advise is appreciated!

Hadley Wickham

unread,
Jun 30, 2010, 3:07:48 PM6/30/10
to Nick S, ggplot2
Hi Nick,

This is a case where it's easier to create the plot with ggplot, not qplot:

ggplot(mapping = aes(fill = "Pre-BBXL")) +
geom_histogram(aes(x), data = x, binwidth=0.25, colour="black") +
geom_histogram(aes(y), data = y, binwidth=0.25, colour="black") +
labs(x="ODs",y="count") +


xlim(0,14) +
scale_fill_brewer("Set1")

Hadley

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Nick S

unread,
Jun 30, 2010, 8:55:33 PM6/30/10
to ggplot2
Thanks for the reply Hadley.

I get the following error when trying to use the code you provided:
Error: ggplot2 doesn't know how to deal with data of class numeric

Any ideas?

Joshua Wiley

unread,
Jun 30, 2010, 9:29:21 PM6/30/10
to Nick S, ggplot2
My guess would be that your data is not in a data frame. Is this what
you want? I just filled the difference in lengths between your two
data files with NAs.

##########
library(ggplot2)
x <- read.table(file="http://dl.dropbox.com/u/3734701/x.csv", sep=",",
header=TRUE)
y <- read.table(file="http://dl.dropbox.com/u/3734701/y.csv", sep=",",
header=TRUE)
dat <- data.frame(x=c(x$x, rep(NA, (nrow(y)-nrow(x)))), y=y$x)

ggplot(data=dat, mapping = aes(fill = "Pre-BBXL")) +
geom_histogram(aes(x=x), binwidth=0.25, colour="black") +
geom_histogram(aes(x=y, y=-..count..), binwidth=0.25, colour="black") +


labs(x="ODs",y="count") +
xlim(0,14) +
scale_fill_brewer("Set1")

############

--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

Nick S

unread,
Jul 1, 2010, 1:43:47 PM7/1/10
to ggplot2
Thanks for the reply.

What I really want to do is have the top half of the histogram be
displayed in one color and the bottom half be displayed in a separate
color.

I have the data in the following format...one column with numeric
values and one column with the categorical variable I would like to
use to differentiate between the two groups (http://dl.dropbox.com/u/
3734701/data.csv)

Is it possible to do this with the data in this format? In previous
examples...I had no luck in displaying the associated category for
each side of the histogram.

Joshua Wiley

unread,
Jul 1, 2010, 5:16:36 PM7/1/10
to Nick S, ggplot2
On Thu, Jul 1, 2010 at 10:43 AM, Nick S <n.si...@gmail.com> wrote:
> Thanks for the reply.
>
> What I really want to do is have the top half of the histogram be
> displayed in one color and the bottom half be displayed in a separate
> color.
>
> I have the data in the following format...one column with numeric
> values and one column with the categorical variable I would like to
> use to differentiate between the two groups (http://dl.dropbox.com/u/
> 3734701/data.csv)
>
> Is it possible to do this with the data in this format? In previous
> examples...I had no luck in displaying the associated category for
> each side of the histogram.

Colouring the different histograms is not an issue with that format of
data, but I am not sure how to specify ..count.. and -..count..
without setting almost everything manually. Have you considered
faceting at all? I think it easier than trying to read an upside down
histogram. Here's an example:

##################

dat <- read.table(file="http://dl.dropbox.com/u/3734701/data.csv",
header=TRUE, sep=",", stringsAsFactors=FALSE)
dat[ , "category"] <- factor(dat[ , "category"],
levels=c("Pre-BBXL","Post-BBXL"), labels=c("Pre-BBXL","Post-BBXL"))
str(dat)
names(dat)

#########
library(ggplot2)

ggplot(data=dat, aes(x=totalODdna, group=category, fill=category)) +
geom_histogram(binwidth = 0.25, colour = "black") +
facet_wrap(~category) +
xlim(0, 14) +
scale_fill_brewer("Set1")

##################

>
> On Jun 30, 6:29 pm, Joshua Wiley <jwiley.ps...@gmail.com> wrote:

<snip>

Reply all
Reply to author
Forward
0 new messages