There is a difference between specifying a scale inside the aes or
aes_string functions, and doing it in the arguments to the geom function.
Inside the aes function call it creates a mapping from your input data to
the layer variables, and you do not want to specify strings such as
"Cusum" but rather always specify variables such as Cusum. Outside the
aes
function among the geom parameters, you can create ad-hoc settings for
that geom that might specify colors as strings. However, AFAIK you cannot
build a legend in ggplot2 that describes those ad-hoc settings, so if you
want a legend that mentions them then you need to create your data to
contain e.g. all colour settings in one or more columns of a "long"
version of your data (it definitely works best to have them all in one
column). You can use the subset option to partition your "long" data
among
the different geoms even while having all the colour settings in the same
column. The "reshape2" package is useful for creating these special
display-ready data frames that stack variables together to fit the
graphic you are mapping it to.
I could not get your sample code to run, and I am daunted by the
complexity of your sample code. Perhaps the above tips will help, or you
can post a simpler example. One way or another, you will need to learn
the language of visualization that ggplot2 is designed around... perhaps
read Hadley Wickhams "ggplot" book. If that paradigm doesn't fit
your way
of thinking, then you might have more success using lattice plots.
On Wed, 13 Aug 2014, Pavneet Arora wrote:
> Data
> this is the data I used for the following problems:
>
> dput(sdf)
> 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
> ), ma = c(NA, 1.46, 1.3, 2.37, 0.5, 1.98, 2.14, 3.42, 2.26, 1.14,
> 1.31, 2.44, 0.960000000000001, 1.11, 0.68, 0.710000000000001,
> 1.25, 0.309999999999999, 0.31, 3, 2.1, 1.57, 2.96, 0.789999999999999,
> 0.9, 0.48, 0.699999999999999, 1.24, 0.309999999999999, 0.790000000000001
> ), Vupper = c(32.59, 32.09, 31.59, 31.09, 30.59, 30.09, 29.59,
> 29.09, 28.59, 28.09, 27.59, 27.09, 26.59, 26.09, 25.59, 25.09,
> 24.59, 24.09, 23.59, 23.09, 22.59, 22.09, 21.59, 21.09, 20.59,
> 20.09, 19.59, 19.09, 18.59, 18.09), Vlower = c(-6.41, -5.91,
> -5.41, -4.91, -4.41, -3.91, -3.41, -2.91, -2.41, -1.91, -1.41,
> -0.910000000000004, -0.410000000000004, 0.0899999999999963,
> 0.589999999999996,
> 1.09, 1.59, 2.09, 2.59, 3.09, 3.59, 4.09, 4.59, 5.09, 5.59, 6.09,
> 6.59, 7.09, 7.59, 8.09)), .Names = c("weeks", "values",
"deviation",
> "cusums", "ma", "Vupper",
"Vlower"), row.names = c(NA, -30L), class =
> "data.frame")
>
> Problem1:
> I am trying to create a chart using ggplot2. This plot contains 2
> geom_lines and 1geom_point.
> But for some reason the geom_point is not being picked up by the legend.
> Can someone help me explain why that maybe?
> This is the code I have used for the above data.
>
> vmaskPlot <- ggplot(sdf,aes(ymin=min(sdf$cusums,sdf$Vupper,sdf$Vlower),
> ymax=(max(sdf$cusums,sdf$Vupper,sdf$Vlower)),
> x=week))+
> labs(x=NULL,y=NULL)+
> # theme
(panel.grid.major=element_line(color="white",size=0.5),
> # panel.grid.minor=element_blank(),
> # panel.grid.major.x=element_blank(),
> # axis.line.y=element_line(color="black"),
> # panel.background=element_rect(fill="lightgray"))+
> # guide=guide_legend(direction="horizontal",
> # title.position="top",
> # label.position="bottom",
> # label.hjust=0.5,
> # label.vjust=0.5,
> # label.theme=element_text(angle=90))+
> scale_y_continuous()+
> scale_x_discrete(breaks=seq(min(sdf$week),
> max(sdf$week)))+
> geom_point(aes(y=cusums,colour="Cusum"),size=4,pch=15)+
> geom_line(aes(y=Vupper,colour="Vupper"),size=2.5,alpha=1/2)+
#alpha =
> determines the ? of transparency
> geom_line(aes(y=Vlower,colour="Vlower"),size=2.5,alpha=1/2)+
> geom_hline(aes(yintercept=0),colour="gray20",size=1)+
#geom_hline -
> draws a reference line at 0
> ggtitle("V-Mask Cusum")+
> theme(legend.position=c(0.9,0.9),
> plot.title=element_text(family="Times",
> face="bold",
> size=20))+
> scale_color_manual(name="Legend",
>
breaks=c("cusums","Vupper","Vlower"),
>
values=c("dodgerblue1","brown3","darkolivegreen4"),
> labels=c("CuSums","Upper V-Mask
","Lower V-Mask "))
>
> vmaskPlot
> Problem2:
> In this ggplot, the legend is coming in separate boxes ? is there any way
> I can combine them in same? Also where it says ?tabuCusum$cusums? ? I just
> want it to say ?CuSums?. I thought I had mentioned this in my code, so why
> is it not picking up?
> Furthermore, the code doesn?t seem to pick up the colours I suggested,
> except for the bars ? which are exactly like I want them to be.
> But I want the pink dots to dodgerblue1. The 2 horizontal lines close to
> y=0 were supposed to be gold2 colour and the 2 outer horizontal lines were
> supposed to be darkorchid colour as mentioned in the code.
> Does anyone know why it?s not picking up the colours mentioned?
> Following is my code:
> ggplot(sdf,aes(x=weeks,
> ymin=min(sdf$cusums,sdf$Tupper,sdf$Tlower)-3,
> ymax=max(sdf$cusums,sdf$Tupper,sdf$Tlower)+3))+
> labs(x=NULL,y=NULL)+
> scale_y_continuous(expand=c(0,0),
> minor_breaks=seq(round(min(sdf$cusums,sdf$Tupper,
> sdf$Tlower)-2),
> round(max(sdf$cusums,sdf$Tupper,
> sdf$Tlower)+2),
> 1),
> breaks=seq(round(min(sdf$cusums,sdf$Tupper,sdf$Tlower
> )-2),
> round(max(sdf$cusums,sdf$Tupper,sdf$Tlower
> )+2),
> 2))+
> scale_x_discrete(expand=c(0,0),
> breaks=seq(min(sdf$weeks),
> max(sdf$weeks)))+
>
geom_bar(data=sdf,aes(y=sdf$Tupper,fill="sdf$Tupper"),stat="identity")+
>
geom_bar(data=sdf,aes(y=sdf$Tlower,fill="sdf$Tlower"),stat="identity")+
> geom_point(aes(y=sdf$cusums,colour="sdf$cusums"),size=4,pch=15)+
> geom_hline(aes(yintercept=0,colour="Line1"),size=1)+
> geom_hline(aes(yintercept=5,colour="Line2"),size=2,alpha=1/1.3)+
> #Out-Of-Signal Lines
>
geom_hline(aes(yintercept=-5,colour="Line3"),size=2,alpha=1/1.3)+
> #Out-Of-Signal Lines
>
geom_hline(aes(yintercept=0.5,colour="Line4"),size=2,alpha=1/1.3)+ #K
>
geom_hline(aes(yintercept=-0.5,colour="Line5"),size=2,alpha=1/1.3)+ #K
> theme(legend.position=c(0.1,0.7),
> plot.title=element_text(family="Times",
> face="bold",
> size=20))+
> # guides(linetype = guide_legend(override.aes = list(colour =
>
c("gray20","darkorchid","darkorchid","gold2","gold2"))))+
> #
>
override.aes=list(colour=c("gray20","darkorchid","darkorchid","gold2","gold2"))
>
> # LEGEND FOR BAR CHART
> scale_fill_manual(name="Legend",
>
breaks=c("sdf$Tlower","sdf$Tupper","sdf$cusums",
>
"Line1","Line2","Line3","Line4","Line5"),
>
values=c("darkolivegreen4","brown3","dodgerblue1",
>
"gray20","darkorchid","darkorchid","gold2",
> "gold2"),
> labels=c("Lower Tabular Mask","Upper
Tabular Mask",
> "Cusum",
>
"Line1","Line2","Line3","Line4","Line5"))
>
>
***********************************************************************************************************************************************************************************************************************
> 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 at 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.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k