Skip to content

Legend in ggplot2

2 messages · Pavneet Arora, Federico Lasa

#
The problem is that you are not actually 'mapping' any variables to
the fill and colour aestethics so ggplot wont produce legends for
those. I'm not sure ggplots are appropiate for what you're trying to
do here but you can sure hack around it a bit, for instance try:

ggplot(tabu, aes(x=weeks, y=T))+
  scale_y_continuous(expand=c(0,0),

minor_breaks=seq(round(min(tabu$cusums,tabu$Tupper,tabu$Tlower)),

round(max(tabu$cusums,tabu$Tupper,tabu$Tlower)),
                                      1),
                     breaks=seq(round(min(tabu$cusums,tabu$Tupper,tabu$Tlower)),
                                round(max(tabu$cusums,tabu$Tupper,tabu$Tlower)),
                                2))+
  scale_x_discrete(expand=c(0,0),
                   breaks=seq(min(tabu$weeks),
                              max(tabu$weeks)))+
  geom_bar(data=tabu, aes(y=Tupper, fill="Tupper"),stat="identity")+
  geom_point(aes(y=cusums, colour="Cusum"),size=4,pch=15)+
  geom_bar(data=tabu, aes(y=Tlower, fill="Tlower"),stat="identity")+
  geom_hline(aes(yintercept=0),colour="gray20",size=1)+
  geom_hline(aes(yintercept=5),colour="darkorchid4",size=2,alpha=1/2)+
  geom_hline(aes(yintercept=-5),colour="darkorchid4",size=2,alpha=1/2)+
  geom_hline(aes(yintercept=0.5),colour="gold2",size=2,alpha=1/1.3)+
  geom_hline(aes(yintercept=-0.5),colour="gold2",size=2,alpha=1/1.3)+
  scale_fill_manual(name="Legend",
                  breaks=c("Tupper","Tlower"),
                  values=c("brown3","darkolivegreen4"),
                  labels=c("T","L"))+
  scale_colour_manual(name="Legend",
                        breaks=c("Cusum"),
                        values=c("dodgerblue1"),
                        labels=c("Cusum"))

and fill in the balnks

P.S. your plot is very strange.


On Thu, Aug 7, 2014 at 9:07 AM, Pavneet Arora
<pavneet.arora at uk.rsagroup.com> wrote: