Data visualization with SAS programming

proc sgplot graph legend with bygroup

Accepted Solution Solved
Reply
Contributor
Posts: 66
Accepted Solution

proc sgplot graph legend with bygroup

Hi,

I am trying to create a simple series plot and assign custom label to each series plot so it is properly shown in chart legend.  However, my chart legend only shows the "by value" instead of the label that should go with the series plot.  Any help would be appreciated.

Output: I should have four series plots for each by value- in each chart, the legend for all 4 series plots are the same and equal to the by value.

code:

/* I tried to add a label to each column in the hope that it would get picked up in the chart legend but no luck.... Smiley Sad */

proc datasets library = Work nolist ;

    modify Load_factor_sgip ;

    label Res = 'Residential'

          Com = 'Commercial'

          Ind = 'Industrial'

          Agr = 'Agriculture & Water Pumping'

        ;

run ;

quit ;

options nobyline orientation = landscape ;

ods graphics on / height = 6 in width = 10.5 in ;

    ods rtf file = 'C:\Documents and Settings\Load_factor_charts.rtf'

        style = statistical nogtitle ;

        ods listing close ; /* close reporting to output window */

        ods results = off ; /* close output to results window */

        title "Sector Load factors for: #byval1" ;

        proc sgplot data = Load_factor_sgip ;

            by PA ;

            series x = Year y = Res / group = PA markers

                lineattrs = (pattern = solid thickness = 2)

               legendlabel = 'Residential'

            ;

            series x = Year y = Com / group = PA markers

                lineattrs = (pattern = solid thickness = 2)

               legendlabel = 'Commercial'

            ;

            series x = Year y = Ind / group = PA markers

                lineattrs = (pattern = solid thickness = 2)

                   legendlabel = 'Industrial'

            ;

            series x = Year y = Agr / group = PA markers

                lineattrs = (pattern = solid thickness = 2)

               legendlabel = 'Agriculture'

            ;

            format Res percent10.2 Com percent10.2

                   Ind percent10.2 Agr percent10.2

            ;

            yaxis label = 'Load Factor' grid ;

            xaxis values = (1990 to 2022 by 4) display = (nolabel) ;

            keylegend / location = inside across = 1 ;

        run ;

        ods listing ;

        ods results = on ;

    ods rtf close ;

ods graphics off ;


Accepted Solutions
Solution
‎01-23-2013 08:39 PM
SAS Super FREQ
Posts: 1,044

Re: proc sgplot graph legend with bygroup

The LegendLabel is used only when the series plot does not use the GROUP option.  In such a case each series statement creates one series plot and you can identify it with the label you want.

However, when GROUP option is used, a series is drawn for each unique value of the group variable.  So one series statement can produce multiple series in the graph.  In this case, each series is identified by the group value itself.  LegendLabel is ignored.  You can use a format to change the value displayed.


View solution in original post


All Replies
Solution
‎01-23-2013 08:39 PM
SAS Super FREQ
Posts: 1,044

Re: proc sgplot graph legend with bygroup

The LegendLabel is used only when the series plot does not use the GROUP option.  In such a case each series statement creates one series plot and you can identify it with the label you want.

However, when GROUP option is used, a series is drawn for each unique value of the group variable.  So one series statement can produce multiple series in the graph.  In this case, each series is identified by the group value itself.  LegendLabel is ignored.  You can use a format to change the value displayed.


SAS Super FREQ
Posts: 864

Re: proc sgplot graph legend with bygroup

To build onto what Sanjay said, You might be able to get the desired result by simply removing the GROUP variable. The attributes for the series plots will automatically cycle within each BY-group. The legend labels will also be used. Give that a try and see if that works for you.

Thanks!

Dan

Contributor
Posts: 66

Re: proc sgplot graph legend with bygroup

Ah man now I see my mistake.  Hate to waste everyones time on something like this but thanks for all the help Sanjay and Dan.

Post a Question
Discussion Stats
  • 3 replies
  • 261 views
  • 3 likes
  • 3 in conversation