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.ar...@uk.rsagroup.com> wrote: > Hi All > > Following is my dataset. > dput(tabu) > structure(list(weeks = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, > 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, > 29, 30), values = c(9.45, 7.99, 9.29, 11.66, 12.16, 10.18, 8.04, > 11.46, 9.2, 10.34, 9.03, 11.47, 10.51, 9.4, 10.08, 9.37, 10.62, > 10.31, 10, 13, 10.9, 9.33, 12.29, 11.5, 10.6, 11.08, 10.38, 11.62, > 11.31, 10.52), deviation = c(-0.550000000000001, -2.01, > -0.710000000000001, > 1.66, 2.16, 0.18, -1.96, 1.46, -0.800000000000001, 0.34, > -0.970000000000001, > 1.47, 0.51, -0.6, 0.0800000000000001, -0.630000000000001, > 0.619999999999999, > 0.31, 0, 3, 0.9, -0.67, 2.29, 1.5, 0.6, 1.08, 0.380000000000001, > 1.62, 1.31, 0.52), cusums = c(-0.550000000000001, -2.56, -3.27, > -1.61, 0.549999999999999, 0.729999999999999, -1.23, 0.229999999999999, > -0.570000000000002, -0.230000000000002, -1.2, 0.269999999999998, > 0.779999999999998, 0.179999999999998, 0.259999999999998, > -0.370000000000003, > 0.249999999999996, 0.559999999999997, 0.559999999999997, 3.56, > 4.46, 3.79, 6.08, 7.58, 8.18, 9.26, 9.64, 11.26, 12.57, 13.09 > ), Tupper = c(0, 0, 0, 1.16, 2.82, 2.5, 0.0399999999999991, 1, > 0, 0, 0, 0.970000000000001, 0.98, 0, 0, 0, 0.119999999999999, > 0, 0, 2.5, 2.9, 1.73, 3.52, 4.52, 4.62, 5.2, 5.08, 6.2, 7.01, > 7.03), Tlower = c(-0.0500000000000007, -1.56, -1.77, 0, 0, 0, > -1.46, 0, -0.300000000000001, 0, -0.470000000000001, 0, 0, > -0.0999999999999996, > 0, -0.130000000000001, 0, 0, 0, 0, 0, -0.17, 0, 0, 0, 0, 0, 0, > 0, 0)), .Names = c("weeks", "values", "deviation", "cusums", > "Tupper", "Tlower"), row.names = c(NA, -30L), class = "data.frame") > > I have created a plot using ggplot, whereby it makes both barchart and > plots points, like following: > ggplot(tabu,aes(x=week, > ymin=min(tabu$cusums,tabu$Tupper,tabu$Tlower), > ymax=max(tabu$cusums,tabu$Tupper,tabu$Tlower)))+ > labs(x=NULL,y=NULL)+ > 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$week), > max(tabu$week)))+ > geom_bar(aes(y=tabu$Tupper),stat="identity",fill="brown3")+ > geom_bar(aes(y=tabu$Tlower),stat="identity",fill="darkolivegreen4")+ > geom_point(aes(y=tabu$cusums),size=4,pch=15,colour="dodgerblue1")+ > geom_hline(aes(yintercept=0),colour="gray20",size=1)+ #geom_hline - > draws a reference line at 0 > geom_hline(aes(yintercept=5),colour="darkorchid4",size=2,alpha=1/2)+ > #Out-Of-Signal Lines > geom_hline(aes(yintercept=-5),colour="darkorchid4",size=2,alpha=1/2)+ > #Out-Of-Signal Lines > geom_hline(aes(yintercept=0.5),colour="gold2",size=2,alpha=1/1.3)+ #K > geom_hline(aes(yintercept=-0.5),colour="gold2",size=2,alpha=1/1.3)+ #K > scale_color_manual(name="Legend", > breaks=c("Tupper","Tlower","CuSum","±K","±H"), > values=c("brown3","darkolivegreen4","dodgerblue1","gold2","darkorchid4"), > labels=c("T","L","C","±K","±H")) > > However, I am having trouble getting a legend. I know its supposed to do > something with melt function in reshape package. But I can?t get it too > work! Can someone please help me with this. > *********************************************************************************************************************************************************************************************************************** > MORE TH>N is a trading style of Royal & Sun Alliance Insurance plc (No. > 93792). Registered in England and Wales at St. Mark’s Court, Chart Way, > Horsham, West Sussex, RH12 1XL. > > Authorised by the Prudential Regulation Authority and regulated by the > Financial Conduct Authority and the Prudential Regulation Authority. > ************************************************************************************************************************************************************************************************************************ > > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@r-project.org 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. > ______________________________________________ R-help@r-project.org 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.