Multiple Histograms with Legend

120 views
Skip to first unread message

Lorenzo Isella

unread,
Jan 26, 2013, 10:39:45 AM1/26/13
to ggplot2
Dear All,
Apologies for not providing some example code here, but if I was able to
do so, then I would not post at all.
Please have a look at the histogram with the number of sales you can see at

http://bit.ly/Y6xOtc

Now, the question is the following: is it possible to create a plot along
those lines in ggplot2?
I mean, representing several histograms in the same plot and using a
different color for each bar?
Any suggestion is welcome.

Lorenzo

Ista Zahn

unread,
Jan 26, 2013, 11:14:04 AM1/26/13
to Lorenzo Isella, ggplot2
Hi,

On Sat, Jan 26, 2013 at 10:39 AM, Lorenzo Isella
<lorenzo...@gmail.com> wrote:
> Dear All,
> Apologies for not providing some example code here, but if I was able to do
> so, then I would not post at all.
> Please have a look at the histogram with the number of sales you can see at
>
> http://bit.ly/Y6xOtc

Those are not histograms. Histograms show the (possibly transformed)
counts in each of a number of bins. The graphs you linked to show
sales by group.

>
> Now, the question is the following: is it possible to create a plot along
> those lines in ggplot2?
> I mean, representing several histograms in the same plot and using a
> different color for each bar?
> Any suggestion is welcome.

Read the documentation for geom_bar, and look at the examples at
http://docs.ggplot2.org/current/geom_histogram.html

Best,
Ista

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

Lorenzo Isella

unread,
Jan 26, 2013, 12:11:59 PM1/26/13
to Ista Zahn, ggplot2
Hi,
I think that I got the job done.
In any case, for completeness, this time I am posting the small snippet I
created.
Cheers

Lorenzo


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

library("ggplot2")
library(tikzDevice)
library(gridExtra)
library(grid)
library(plyr)


set.seed(1234)

## see also http://bit.ly/14mbLlH


year <- c(rep("2004",runif(1,50,350)),
rep("2005",runif(1,50,350)),
rep("2006",runif(1,50,350)),
rep("2007",runif(1,50,350)),
rep("2008",runif(1,50,350)),
rep("2009",runif(1,50,350)),
rep("2010",runif(1,50,350)),
rep("2011",runif(1,50,350)),
rep("2012",runif(1,50,350))
)

len <- length(year)

len3 <- round(len/3)

location <- c(rep("Old town",len3),
rep("Mid town", len3),
rep("Lower keys", (len-2*len3))
)

location <- sample(location)

mydata <- cbind(as.data.frame(year),
as.data.frame(location))



lbls <- levels(mydata$year)


gpl <- ggplot(mydata, aes(location, fill=year ) ) +
geom_hline(yintercept = seq(0,120,by=20), col="black", size=0.5)+

geom_bar(position=position_dodge(width=0.9,height=0))+
opts( panel.background=theme_rect(fill='white', size=1.5))+
opts(panel.grid.minor = theme_blank())+
opts(panel.grid.major = theme_blank())+
opts(axis.ticks = theme_segment(colour = "black",
size=1),axis.ticks.length = unit(0.15, "cm"))+
scale_fill_manual("Year", breaks=lbls, labels=lbls,
values=c(1,2,3,4,5,6,7,8,"brown"))+

opts(title = expression(paste("Number of Sales")))+
opts(plot.title = theme_text(size = 25))+

scale_y_continuous(limits=c(0,120),breaks=seq(0,120, by=20))+


xlab("")+
ylab("")+
opts(axis.title.x = theme_text(size = 2))+
opts(axis.title.y = theme_text(size = 2, angle=90))+
## opts(axis.text.x = theme_text(size=15, colour="gray",vjust=1))+
## opts(axis.text.y = theme_text(size=15, colour="gray", hjust=1))
opts(axis.text.x = theme_text(size=15, colour="black",vjust=1))+
opts(axis.text.y = theme_text(size=15, colour="black", hjust=1))


tikz('market_visualization_tik.tex', standAlone = TRUE, width=8,height=5)
print(gpl)
dev.off()

tools::texi2dvi('market_visualization_tik.tex',pdf=T)

Lorenzo Isella

unread,
Jan 26, 2013, 12:18:00 PM1/26/13
to Ista Zahn, ggplot2
On Sat, 26 Jan 2013 17:14:04 +0100, Ista Zahn <ista...@gmail.com> wrote:

>
> Those are not histograms. Histograms show the (possibly transformed)
> counts in each of a number of bins. The graphs you linked to show
> sales by group.

I am not a statistician, but this looks to me like a histogram, where
every bin is labeled by a year and a location.
Cheers

Lorenzo
Reply all
Reply to author
Forward
0 new messages