Hi everyone. I've used the GPL to graph the longitudinal curves of four different variables on the same graph. What I'd like to do is add a legend that would indicate which color is associated with which variable. However, since I didn't use a panel variable to differentiate the curves, I'm not quite sure how this can be done. Does anyone have any suggestions? Your help would definitely be appreciated! Below is some sample data and the actual GPA code I'm currently working with. --===Jamie DeCoster ----- data list free /daysin ClassOrgMn prsumCO prsumES prsumIS. DATASET NAME $DataSet WINDOW=FRONT. begin data 11.00 4.83 .00 .00
.00 46.00 4.50 .00 .00 .00 176.00 5.83 .00 .00 .00 224.00 5.11 .00 .00 .00 238.00 5.08 .00 .00 .00 33.00 5.83 .00 .00 .00 57.00 5.50 .00 .00 .00 81.00 5.67 .00 .00 .00 118.00 5.42 .00 .00 .00 176.00 5.75
.00 .00 .00 210.00 6.33 .00 .00 .00 244.00 5.58 .00 .00 .00 20.00 4.58 .00 .00 .00 69.00 5.00 .00 5.00 .00 126.00 5.33 6.00 5.00 .00 140.00 5.17 8.00 5.00 .00 188.00 5.00 11.00 5.00 6.00 203.00 3.83 11.00 5.00 9.00 231.00 4.92 11.00 5.00 12.00 239.00 5.25 11.00 5.00 15.00 266.00 5.92 11.00 5.00
21.00 280.00 4.33 11.00 5.00 24.00 7.00 6.00 .00 .00 .00 14.00 5.42 .00 .00 .00 138.00 5.50 9.00 5.00 1.00 182.00 5.42 9.00 8.00 4.00 200.00 6.11 9.00 8.00 7.00 230.00 5.67 9.00 8.00 10.00 251.00 4.92 9.00 8.00 13.00 271.00 6.50 9.00 8.00 16.00 46 .00 .00 .00 .00 56 .00 .00 .00 .00 35.00 4.33 .00
.00 .00 143.00 6.17 5.00 4.00 .00 151.00 4.67 5.00 4.00 .00 190.00 5.67 8.00 4.00 .00 208.00 5.33 11.00 4.00 .00 228.00 4.83 14.00 4.00 .00 253.00 5.83 14.00 4.00 6.00 264.00 5.50 14.00 4.00 9.00 end data. GGRAPH /GRAPHDATASET NAME="graphdataset" VARIABLES=daysin ClassOrgMn prsumCO prsumES prsumIS MISSING=LISTWISE REPORTMISSING=NO /GRAPHSPEC SOURCE=INLINE. BEGIN GPL SOURCE: s=userSource(id("graphdataset")) DATA: daysin=col(source(s), name("daysin")) DATA: ClassOrgMn=col(source(s), name("ClassOrgMn")) DATA: prsumCO=col(source(s), name("prsumCO")) DATA: prsumES=col(source(s), name("prsumES")) DATA: prsumIS=col(source(s),
name("prsumIS")) GUIDE: axis(dim(1), label("daysin")) GUIDE: axis(scale(y1), label("ClassOrgMn"), color(color.black)) GUIDE: axis(scale(y2), label("prsum"), color(color.black), opposite()) SCALE: y1 = linear(dim(2)) SCALE: y2 = linear(dim(2), min(-2)) ELEMENT: point(position(daysin*ClassOrgMn), color.exterior(color."F0F0F0"), scale(y1), size(size."1px")) ELEMENT: point(position(daysin*prsumCO),
color.exterior(color."F0F0F0"), scale(y2), size(size."1px")) ELEMENT: point(position(daysin*prsumES), color.exterior(color."F0F0F0"), scale(y2), size(size."1px")) ELEMENT: point(position(daysin*prsumIS), color.exterior(color."F0F0F0"), scale(y2), size(size."1px")) ELEMENT: line(scale(y1), position(smooth.loess(daysin*ClassOrgMn,proportion(0.50))),color(color.black), size(size."2px")) ELEMENT: line(scale(y2), position(smooth.loess(daysin*prsumCO,proportion(0.50))),color(color.blue), size(size."2px")) ELEMENT: line(scale(y2),
position(smooth.loess(daysin*prsumES,proportion(0.50))),color(color.green), size(size."2px")) ELEMENT: line(scale(y2), position(smooth.loess(daysin*prsumIS,proportion(0.50))),color(color.red), size(size."2px")) END GPL. |
You can do this by specifying a string to the color argument on the ELEMENT statement, as in: ELEMENT: line(scale(y1), position(smooth.loess(daysin*ClassOrgMn,proportion(0.50))),color("ClassOrgMn"), size(size."2px")) ELEMENT: line(scale(y2), position(smooth.loess(daysin*prsumCO,proportion(0.50))),color("prsumCO"), size(size."2px")) ELEMENT: line(scale(y2), position(smooth.loess(daysin*prsumES,proportion(0.50))),color("prsumES"), size(size."2px")) ELEMENT: line(scale(y2), position(smooth.loess(daysin*prsumIS,proportion(0.50))),color("prsumIS"), size(size."2px")) You’ll then get a legend entry for each string. Note that here, you can’t explicitly control the color of the line. The color is picked up from the color cycle defined in your preferences (Edit>Options). The big drawback to this is that you can’t explicitly label the generated legend. I tried this in Release 20 with GUIDE: legend(aesthetic(aesthetic.color), label("Legend Title") only to get an error from the visualization engine. YMMV with earlier versions. You might try to edit the chart and stick in a legend title and save it to a template. Given the fragility of the templates, this might not work. You are using double Y axes here but don’t color-code the axis line to the ELEMENT. Presumably your readers will know that ClassOrgMn is associated with the left axis and the remaining three line are color-coded to the right axis. From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Jamie DeCoster Hi everyone. I've used the GPL to graph the longitudinal curves of four different variables on the same graph. What I'd like to do is add a legend that would indicate which color is associated with which variable. However, since I didn't use a panel variable to differentiate the curves, I'm not quite sure how this can be done. Does anyone have any suggestions? Your help would definitely be appreciated! Below is some sample data and the actual GPA code I'm currently working with. --===Jamie DeCoster ----- data list free /daysin ClassOrgMn prsumCO prsumES prsumIS. DATASET NAME $DataSet WINDOW=FRONT. begin data 11.00 4.83 .00 .00 .00 46.00 4.50 .00 .00 .00 176.00 5.83 .00 .00 .00 224.00 5.11 .00 .00 .00 238.00 5.08 .00 .00 .00 33.00 5.83 .00 .00 .00 57.00 5.50 .00 .00 .00 81.00 5.67 .00 .00 .00 118.00 5.42 .00 .00 .00 176.00 5.75 .00 .00 .00 210.00 6.33 .00 .00 .00 244.00 5.58 .00 .00 .00 20.00 4.58 .00 .00 .00 69.00 5.00 .00 5.00 .00 126.00 5.33 6.00 5.00 .00 140.00 5.17 8.00 5.00 .00 188.00 5.00 11.00 5.00 6.00 203.00 3.83 11.00 5.00 9.00 231.00 4.92 11.00 5.00 12.00 239.00 5.25 11.00 5.00 15.00 266.00 5.92 11.00 5.00 21.00 280.00 4.33 11.00 5.00 24.00 7.00 6.00 .00 .00 .00 14.00 5.42 .00 .00 .00 138.00 5.50 9.00 5.00 1.00 182.00 5.42 9.00 8.00 4.00 200.00 6.11 9.00 8.00 7.00 230.00 5.67 9.00 8.00 10.00 251.00 4.92 9.00 8.00 13.00 271.00 6.50 9.00 8.00 16.00 46 .00 .00 .00 .00 56 .00 .00 .00 .00 35.00 4.33 .00 .00 .00 143.00 6.17 5.00 4.00 .00 151.00 4.67 5.00 4.00 .00 190.00 5.67 8.00 4.00 .00 208.00 5.33 11.00 4.00 .00 228.00 4.83 14.00 4.00 .00 253.00 5.83 14.00 4.00 6.00 264.00 5.50 14.00 4.00 9.00 end data. GGRAPH /GRAPHDATASET NAME="graphdataset" VARIABLES=daysin ClassOrgMn prsumCO prsumES prsumIS MISSING=LISTWISE REPORTMISSING=NO /GRAPHSPEC SOURCE=INLINE. BEGIN GPL SOURCE: s=userSource(id("graphdataset")) DATA: daysin=col(source(s), name("daysin")) DATA: ClassOrgMn=col(source(s), name("ClassOrgMn")) DATA: prsumCO=col(source(s), name("prsumCO")) DATA: prsumES=col(source(s), name("prsumES")) DATA: prsumIS=col(source(s), name("prsumIS")) GUIDE: axis(dim(1), label("daysin")) GUIDE: axis(scale(y1), label("ClassOrgMn"), color(color.black)) GUIDE: axis(scale(y2), label("prsum"), color(color.black), opposite()) SCALE: y1 = linear(dim(2)) SCALE: y2 = linear(dim(2), min(-2)) ELEMENT: point(position(daysin*ClassOrgMn), color.exterior(color."F0F0F0"), scale(y1), size(size."1px")) ELEMENT: point(position(daysin*prsumCO), color.exterior(color."F0F0F0"), scale(y2), size(size."1px")) ELEMENT: point(position(daysin*prsumES), color.exterior(color."F0F0F0"), scale(y2), size(size."1px")) ELEMENT: point(position(daysin*prsumIS), color.exterior(color."F0F0F0"), scale(y2), size(size."1px")) ELEMENT: line(scale(y1), position(smooth.loess(daysin*ClassOrgMn,proportion(0.50))),color(color.black), size(size."2px")) ELEMENT: line(scale(y2), position(smooth.loess(daysin*prsumCO,proportion(0.50))),color(color.blue), size(size."2px")) ELEMENT: line(scale(y2), position(smooth.loess(daysin*prsumES,proportion(0.50))),color(color.green), size(size."2px")) ELEMENT: line(scale(y2), position(smooth.loess(daysin*prsumIS,proportion(0.50))),color(color.red), size(size."2px")) END GPL. |
To start thank you for the reproducible example! To add to Viann's answer, this is an important distinction in the grammer of graphics between mapping a piece of information to an aesthetic versus just telling the chart to look like a particular aesthetic. Essentially you are telling the graph that some visual information (here the color of the points and the lines) distinguishes different types of data.
So in addition to just telling the GPL within the ELEMENT statement that "you should assign a different color aesthetic for [ClassOrgMn] vs [prsumCO]" we can simulataneously control how that mapping is defined. For instance, adding in the command in addition to Viann's code; SCALE: cat(aesthetic(aesthetic.color.interior), map(("ClassOrgMn", color.black), ("prsumCO", color.blue),("prsumES", color.green),("prsumIS", color.red))) Will come close to doing what you want. Although sometimes I think editing the cycle chart colors in the chart template might be easier than making such a long and parenthesis filled statement, unlike Viann says it is not necessary to resort to editing the chart template for this. There are a few complications though with such a complicated chart, including as Viann mentioned drawing of the legend (which I have experienced inconsistent behavior between versions 17, 18, 19 and 20 as well). One thing to note is that colors can apply either to interior "fills" or exterior "outlines" of elements in the chart. Line elements ONLY have interior fills. My experience in simulataneously using colors for both interior and exterior elements is not handled properly in the drawing of the legend. For an example, here is how I re-oriented yours and Viann's GPL statement. ************************************. GGRAPH /GRAPHDATASET NAME="graphdataset" VARIABLES=daysin ClassOrgMn prsumCO prsumES prsumIS MISSING=LISTWISE REPORTMISSING=NO /GRAPHSPEC SOURCE=INLINE. BEGIN GPL SOURCE: s=userSource(id("graphdataset")) DATA: daysin=col(source(s), name("daysin")) DATA: ClassOrgMn=col(source(s), name("ClassOrgMn")) DATA: prsumCO=col(source(s), name("prsumCO")) DATA: prsumES=col(source(s), name("prsumES")) DATA: prsumIS=col(source(s), name("prsumIS")) GUIDE: axis(dim(1), label("daysin")) GUIDE: axis(scale(y1), label("ClassOrgMn"), color(color.black)) GUIDE: axis(scale(y2), label("prsum"), color(color.black), opposite()) SCALE: y1 = linear(dim(2)) SCALE: y2 = linear(dim(2), min(-2)) SCALE: cat(aesthetic(aesthetic.color.interior), map(("ClassOrgMn", color.black), ("prsumCO", color.blue),("prsumES", color.green),("prsumIS", color.red))) GUIDE: legend(aesthetic(aesthetic.color.interior), label("Legend Title")) ELEMENT: point(position(daysin*ClassOrgMn), color.interior("ClassOrgMn"), scale(y1), size(size."3px")) ELEMENT: point(position(daysin*prsumCO), color.interior("prsumCO"), scale(y2), size(size."3px")) ELEMENT: point(position(daysin*prsumES), color.interior("prsumES"), scale(y2), size(size."3px")) ELEMENT: point(position(daysin*prsumIS), color.interior("prsumIS"), scale(y2), size(size."3px")) ELEMENT: line(scale(y1), position(smooth.loess(daysin*ClassOrgMn,proportion(0.50))),color.interior("ClassOrgMn"), size(size."2px")) ELEMENT: line(scale(y2), position(smooth.loess(daysin*prsumCO,proportion(0.50))),color.interior("prsumCO"), size(size."2px")) ELEMENT: line(scale(y2), position(smooth.loess(daysin*prsumES,proportion(0.50))),color.interior("prsumES"), size(size."2px")) ELEMENT: line(scale(y2), position(smooth.loess(daysin*prsumIS,proportion(0.50))),color.interior("prsumIS"), size(size."2px")) END GPL. ************************************. On my version of 17 that is on my computer right now, it produced 4 redundant legends (which could be clicked and deleted), but did not draw the circles (only the wavy smooth line). It did however title the legend (which honestly is superflous in most circumstances, people know what a legend is). So, to see if you can get a legend to draw properly might take more personal experimentation and/or post-hoc editing in a seperate vector environment where you can ungroup elements and delete them individually. FYI I've always had very good success exporting to emf and doing this (not as nice when exported to PDF, have not tried EPS very much). A second thing to note is that dual axis graphs are widely despised. This in conjunction with so many elements on the chart (yes even only 4 lines is many elements) I would suggest paneling the charts, but just utilize different y axis for ClassOrgMn and the prsum elements. Again the chart is complicated (and you have the unfortunate side effect here that the ClassOrgMn points are forced to be mapped to an aesthetic (I chose the same aesthetic as the prsumCO element), so it may take some more personal experimentation to figure out the best look for you chart, but IMO it doesn't work out too badly in this example you provide. Note the way I forced ClassOrgMn to be missing (so as to not double count data in the graph (not a problem for the points, but likely a problem for the smooth line), and also the "MISSING=VARIABLEWISE" in the graphdataset statement. Another (probably better) way to do this would be to use two seperate graphdataset commands specifying seperate datasets. ************************************. varstocases /make prsum from prsumCO prsumES prsumIS /index = Type (prsum). if Type <> "prsumCO" ClassOrgMn = $sysmis. exe. DATASET ACTIVATE $DataSet. * Chart Builder. GGRAPH /GRAPHDATASET NAME="graphdataset" VARIABLES=daysin ClassOrgMn prsum Type MISSING=VARIABLEWISE REPORTMISSING=NO /GRAPHSPEC SOURCE=INLINE. BEGIN GPL SOURCE: s=userSource(id("graphdataset")) DATA: daysin=col(source(s), name("daysin")) DATA: ClassOrgMn=col(source(s), name("ClassOrgMn")) DATA: prsum=col(source(s), name("prsum")) DATA: Type=col(source(s), name("Type"), unit.category()) GUIDE: axis(dim(1), label("daysin")) GUIDE: axis(dim(2), label("prsum")) GUIDE: legend(aesthetic(aesthetic.color.interior), label("Type")) ELEMENT: point(position(daysin*(ClassOrgMn/"ClassOrgMn" + prsum/"prsum")), color.interior(Type)) ELEMENT: line(position(smooth.loess(daysin*(ClassOrgMn/"ClassOrgMn" + prsum/"prsum"), proportion(0.50))), color.interior(Type)) END GPL. ************************************. Some post hoc editing, and moving the legend inside the bottom prsum panel (to distinguish that is pertains to only that panel), produces the attached chart. |
Free forum by Nabble | Edit this page |