I would like to add a reference line to a line graph.
The line should be vertical in a specific value of the variable "start date" (pls see image attached) the value is changing per graph. Thanks for your help, Sigalit |
/* Simulate data */.
INPUT PROGRAM. LOOP #x=1 to 2. LOOP #y=1 to 12. COMPUTE ID=(#x-1)*12+#y. COMPUTE Group=#x. COMPUTE Date=DATE.DMY(1,#y,2014). END CASE. END LOOP. END LOOP. END FILE. END INPUT PROGRAM. FORMATS Date (adate). SET SEED=10. COMPUTE Temperature=RV.UNIFORM(0,1)*100. COMPUTE StartDate=DATE.DMY(1,7,2014). FORMATS Date StartDate (SDATE10). * Chart Builder. GGRAPH /GRAPHDATASET NAME="graphdataset" VARIABLES=Date MEAN(Temperature)[name="MEAN_Temperature"] Group StartDate MISSING=LISTWISE REPORTMISSING=NO /GRAPHSPEC SOURCE=INLINE. BEGIN GPL SOURCE: s=userSource(id("graphdataset")) DATA: Date=col(source(s), name("Date")) DATA: StartDate=col(source(s), name("StartDate")) DATA: MEAN_Temperature=col(source(s), name("MEAN_Temperature")) DATA: Group=col(source(s), name("Group"), unit.category()) TRANS: top = eval(100) GUIDE: axis(dim(1), label("Date")) GUIDE: axis(dim(2), label("Mean Temperature")) GUIDE: legend(aesthetic(aesthetic.color.interior), label("Group")) ELEMENT: line(position(Date*MEAN_Temperature), color.interior(Group)) ELEMENT: interval(position(StartDate*top), color.exterior(color.red), color.interior(color.red), size(size."1")) END GPL. |
I tried to add a label for the reference line as the OP had but experienced some strange behavior.
I added the following ELEMENT (I have left the point with a solid outline but would eventually have that invisible, 100% transparency): ELEMENT: point(position(StartDate*top), size(size."9"), label("Start Date")) But found the point was being labelled 4 times, rather than just once. I tried to manually delete all but one of the labels manually through Chart Editor but even if selecting a single label and hitting delete, it deletes all 4 labels? How closely could a label like that be applied (including red font and label angle?) to the chart? If not, could it be included as a second legend? INPUT PROGRAM. LOOP #x=1 to 2. LOOP #y=1 to 12. COMPUTE ID=(#x-1)*12+#y. COMPUTE Group=#x. COMPUTE Date=DATE.DMY(1,#y,2014). END CASE. END LOOP. END LOOP. END FILE. END INPUT PROGRAM. EXECUTE. FORMATS Date (adate). SET SEED=10. NUMERIC Temperature (F8.0). COMPUTE Temperature=RV.UNIFORM(0,1)*100. COMPUTE StartDate=DATE.DMY(1,7,2014). FORMATS Date StartDate (SDATE10). * Chart Builder. GGRAPH /GRAPHDATASET NAME="graphdataset" VARIABLES=Date MEAN(Temperature)[name="MEAN_Temperature"] Group StartDate MISSING=VARIABLEWISE REPORTMISSING=NO /GRAPHSPEC SOURCE=INLINE. BEGIN GPL SOURCE: s=userSource(id("graphdataset")) DATA: Date=col(source(s), name("Date")) DATA: StartDate=col(source(s), name("StartDate")) DATA: MEAN_Temperature=col(source(s), name("MEAN_Temperature")) DATA: Group=col(source(s), name("Group"), unit.category()) TRANS: bottom = eval(0) TRANS: top = eval(100) GUIDE: axis(dim(1), label("Date")) GUIDE: axis(dim(2), label("Mean Temperature")) GUIDE: legend(aesthetic(aesthetic.color.interior), label("Group")) ELEMENT: line(position(Date*MEAN_Temperature), color.interior(Group)) ELEMENT: interval(position(StartDate*top), color.exterior(color.red), color.interior(color.red), size(size."1")) ELEMENT: point(position(StartDate*top), size(size."9"), label("Start Date")) END GPL. |
When you use the TRANS statements it basically replicates labels for every point in the dataset, so there are actually more than 4 points there, it is just SPSS does not superimpose all of the labels. Here is an example hack I have done to get the labels closer to where I want them in the end, only have a label for one point in the dataset and then make sure to specify MISSING=VARIABLEWISE.
***********************************************. STRING Lab (A20). DO IF Group = 1 AND Date = StartDate. COMPUTE Lab = "Start Date". COMPUTE Top = 100. END IF. GGRAPH /GRAPHDATASET NAME="graphdataset" VARIABLES=Date Temperature Group StartDate Lab Top MISSING=VARIABLEWISE REPORTMISSING=NO /GRAPHSPEC SOURCE=INLINE. BEGIN GPL SOURCE: s=userSource(id("graphdataset")) DATA: Date=col(source(s), name("Date")) DATA: StartDate=col(source(s), name("StartDate")) DATA: Temperature=col(source(s), name("Temperature")) DATA: Group=col(source(s), name("Group"), unit.category()) DATA: Lab=col(source(s), name("Lab"), unit.category()) DATA: Top=col(source(s), name("Top")) GUIDE: axis(dim(1), label("Date")) GUIDE: axis(dim(2), label("Mean Temperature")) GUIDE: legend(aesthetic(aesthetic.color.interior), label("Group")) ELEMENT: line(position(Date*Temperature), color.interior(Group)) ELEMENT: interval(position(StartDate*Top), color.exterior(color.red), color.interior(color.red), size(size."1")) ELEMENT: point(position(StartDate*Top), label(Lab) ) END GPL. ***********************************************. Also see https://andrewpwheeler.wordpress.com/2015/01/14/labeling-tricks-in-spss-plots/, you can get more precise in the placement when using ELEMENT: polygon instead of point. To get the end behavior the OP wants though will probably task post-hoc editing of the label, but that is possible right within SPSS in the chart editor. (You can always just label the interval element and then edit that or make a template.) What I want to know is how to specify a GUIDE: form.line with a date on the X axis. I would have thought below works, and it produces a chart but with no line for me. **********************************************. GGRAPH /GRAPHDATASET NAME="graphdataset" VARIABLES=Date MEAN(Temperature)[name="MEAN_Temperature"] Group StartDate MISSING=VARIABLEWISE REPORTMISSING=NO /GRAPHSPEC SOURCE=INLINE. BEGIN GPL SOURCE: s=userSource(id("graphdataset")) DATA: Date=col(source(s), name("Date"), unit.time(), format("M/d/yyyy")) DATA: StartDate=col(source(s), name("StartDate")) DATA: MEAN_Temperature=col(source(s), name("MEAN_Temperature")) DATA: Group=col(source(s), name("Group"), unit.category()) GUIDE: axis(dim(1), label("Date")) GUIDE: axis(dim(2), label("Mean Temperature")) GUIDE: legend(aesthetic(aesthetic.color.interior), label("Group")) GUIDE: form.line(position("7/1/2014",*), color(color.red), label("Start Date")) ELEMENT: line(position(Date*MEAN_Temperature), color.interior(Group)) END GPL. **********************************************. |
I also tried some variations to get form.line to work also, specifically:
GUIDE: form.line(position(DATE.DMY(1,7,2014),*), color(color.red), label("Start Date")) which didn't work either. The only way I could get it to work was to format Date as numeric and enter numeric representation of Date in the form.line but that isn't a workable workaround, with date values being presented as numeric representation rather than conventional date format. |
I wouldn't expect SPSS date functions to work within inline GPL, they are different.
I tried some more variations using "TRANS: eval" - and the string to date function, but below when both ELEMENT statements are inserted I get an error "Specification requires scales of two different types to be merged, which is not possible". If you do one or the other the correct output occurs. It would be nice if the "stringtodate" function was available within the "GUIDE: form.line(". ******************************************************. GGRAPH /GRAPHDATASET NAME="graphdataset" VARIABLES=Date MEAN(Temperature)[name="MEAN_Temperature"] Group StartDate MISSING=VARIABLEWISE REPORTMISSING=NO /GRAPHSPEC SOURCE=INLINE. BEGIN GPL SOURCE: s=userSource(id("graphdataset")) DATA: Date=col(source(s), name("Date"), unit.time(), format("M/d/yyyy")) DATA: StartDate=col(source(s), name("StartDate")) DATA: MEAN_Temperature=col(source(s), name("MEAN_Temperature")) DATA: Group=col(source(s), name("Group"), unit.category()) TRANS: SLab = eval(stringtodate("1/7/2014")) TRANS: top = eval(100) GUIDE: axis(dim(1), label("Date"), format.date()) GUIDE: axis(dim(2), label("Mean Temperature")) GUIDE: legend(aesthetic(aesthetic.color.interior), label("Group")) ELEMENT: line(position(Date*MEAN_Temperature), color.interior(Group)) END GPL. *ELEMENT: interval(position(SLab*top), color.exterior(color.red), color.interior(color.red), size(size."1")) ******************************************************. |
Free forum by Nabble | Edit this page |