Hi,
I'm working to create a line graph that shows each patients recorded zone width at each time frame (visit). So, one graph per zone. I'm having trouble getting the legend to appear since it will take up a lot of space (due to the # of patients). Zone 1 has the most patients appearing in the legend so I'm starting with that. Once, it works, I will place this in a macro to rerun for the other zones and output into rtf.
Below is the code I'm using and the dataset is attached. I've tinkered with the maxlegendarea in the ods statement to no avail. Any idea on other options I have?
ods graphics on/ noborder reset MAXLEGENDAREA=100 ;
proc sgplot data=fig;
series x=rpt_order y= zone1 /group= patient;
xaxis display=(nolabel);
yaxis label = 'Width' ;
run;
My first approach, as it is pretty easy to do, is to set the zero values to missing for the plot data set. Without a Y value no point is placed to connect. If all the values are missing the patient won't appear in the plot. If if there is mix of zero and not zero then series plots by default will connect the points. If go this route you likely want to add markers to the plot for those points where there are single non-zero values for a patient and use the BREAK option if you do not want non-sequential x values connected when the intermediate Y value is missing (another reason for the markers).
Since your axis is actually time I find character values like 6 weeks mixed with Year 1 and Year2 odd. The horizontal distance between 6 weeks and Year 1 I would expect to be somewhat less than between Year 1 and Year 2.
Most of us will not download Microsoft Office file attachments as they are a security threat. You should provide (a portion of) your data as SAS data step code (instructions).
With regard to your problem that the legend is getting too big, your best choice is to turn off the legend as no one is going to be able to identify one patient in a plot that has dozens of lines.
Just how many patients are you attempting to display at one time? Like @PaigeMiller I won't open XLSX files from unknown sources so can't tell. Suggestion: run your plot with the option on the Proc SGPLOT statement of NOAUTOLEGEND. See if the graph makes any sense. Enough plots of similar values and likely not.
Legendareamax=100 would specify that 100 percent of the graph area could be occupied by the legend, i.e. no actual graph can display, just the legend.
You may need to increase the actual size of the display area that SGplot has by setting a larger value for Height and Width in your Ods graphics statement if you don't have enough space.
Have you done the plot with patients without the legend yet? You really need to do so as with most data I would expect of 76 patients that maybe 10 to 15 patients at most can actually show usable information. For one thing, at some point you may well have multiple patients with the same line/color appearance.
@Etoo12121 wrote:
I have 76 total patients.
Here is the graph without the legend. Some patients have zeros as their widths for each time frame so they have no corresponding lines
I think I found a way around it. Because there are too many patients to plot, I'm planning to exclude patients with zeros in every row from my final dataset. So looking at every row for each patient, if every zone1 row is 0 then I want my new field = exclude.
thoughts?
Something like:
data want;
TEST='Include';
do until (last.patient);
set have;
by patient;
if max(zone1)=0 and min(zone1)=0 then TEST = 'Exclude';
end;
do until (last.patient);
set have;
by patient;
output;
end;
run;
Here is the dataset if it's easier to read in. I'm required to include the legend so I can't use noautolegend
Patient | Zone1 | Zone2 | Zone3 | Visit |
21-0001 | 0 | 0 | 0 | Year 1 |
21-0002 | 0 | 0 | 0 | 6 Weeks |
21-0002 | 0 | 0 | 0 | Year 1 |
21-0003 | 0 | 0 | 0 | 6 Weeks |
21-0003 | 0 | 0 | 0 | Year 1 |
21-0003 | 0 | 0 | 0 | Year 2 |
21-0004 | 0 | 0 | 0 | 6 Weeks |
21-0004 | 0 | 0 | 0 | Year 1 |
21-0004 | 0 | 0 | 0 | Year 2 |
21-0005 | 0 | 0 | 0 | Year 1 |
21-0005 | 0 | 0 | 0 | Year 2 |
21-0006 | 0 | 0 | 0 | Year 1 |
21-0007 | 0 | 0 | 0 | 6 Weeks |
21-0007 | 0 | 0 | 0 | Year 1 |
21-0007 | 0 | 0 | 0 | Year 2 |
21-0008 | 0.6 | 0 | 0 | Year 1 |
21-0008 | 0.5 | 0 | 0 | Year 2 |
21-0009 | 0 | 0 | 0 | 6 Weeks |
21-0009 | 0.8 | 0 | 0 | Year 1 |
21-0010 | 0 | 0 | 0 | Year 2 |
21-0011 | 0 | 0 | 0 | Year 1 |
21-0011 | 0.5 | 0 | 0 | Year 2 |
21-0012 | 0.5 | 0 | 0 | Year 1 |
21-0013 | 0 | 0 | 0 | 6 Weeks |
21-0013 | 0 | 0 | 0 | Year 2 |
22-0001 | 0 | 0 | 0 | 6 Weeks |
22-0001 | 0 | 0 | 0 | Year 1 |
22-0001 | 0 | 0 | 0 | Year 2 |
22-0002 | 0 | 0 | 0 | 6 Weeks |
22-0002 | 1.1 | 0 | 0 | Year 1 |
22-0002 | 1 | 0 | 0 | Year 2 |
22-0003 | 0 | 0 | 0 | 6 Weeks |
22-0003 | 0 | 0 | 0 | Year 1 |
22-0003 | 0 | 0 | 0 | Year 2 |
22-0004 | 0 | 0 | 0 | 6 Weeks |
22-0004 | 0 | 0 | 0 | Year 1 |
22-0004 | 0 | 0 | 0 | Year 2 |
22-0005 | 0 | 0 | 0 | 6 Weeks |
22-0005 | 0 | 0 | 0 | Year 1 |
22-0005 | 0 | 0 | 0 | Year 2 |
22-0006 | 0 | 0 | 0 | 6 Weeks |
22-0006 | 0 | 0 | 0 | Year 2 |
22-0007 | 0 | 0 | 0 | Year 1 |
22-0008 | 0 | 0 | 0 | 6 Weeks |
22-0008 | 0 | 0 | 0 | Year 1 |
22-0008 | 0 | 0 | 0 | Year 2 |
22-0009 | 0 | 0 | 0 | 6 Weeks |
22-0009 | 0 | 0 | 0 | Year 1 |
22-0009 | 0 | 0 | 0 | Year 2 |
22-0010 | 0 | 0 | 0 | Year 1 |
22-0010 | 0 | 0 | 0 | Year 2 |
22-0011 | 0 | 0 | 0 | Year 2 |
22-0012 | 0 | 0 | 0 | Year 1 |
22-0012 | 0 | 0 | 0 | Year 2 |
22-0013 | 0 | 0 | 0 | 6 Weeks |
22-0013 | 0 | 0 | 0 | Year 1 |
22-0013 | 0 | 0 | 0 | Year 2 |
22-0014 | 0 | 0 | 0 | Year 1 |
22-0014 | 0 | 0 | 0 | Year 2 |
22-0015 | 0 | 0 | 0 | Year 2 |
22-0016 | 0 | 0 | 0 | 6 Weeks |
22-0016 | 0.8 | 0 | 0 | Year 1 |
22-0016 | 0.7 | 0.6 | 0.5 | Year 2 |
22-0017 | 0 | 0 | 0 | Year 1 |
22-0017 | 0.4 | 0 | 0 | Year 2 |
22-0018 | 0 | 0 | 0 | 6 Weeks |
22-0018 | 0 | 0 | 0 | Year 1 |
22-0018 | 0 | 0 | 0 | Year 2 |
22-0019 | 0 | 0 | 0 | 6 Weeks |
22-0019 | 0 | 0 | 0 | Year 1 |
22-0019 | 0 | 0 | 0 | Year 2 |
22-0020 | 0 | 0 | 0 | 6 Weeks |
22-0020 | 0.5 | 0 | 0 | Year 1 |
22-0021 | 0.6 | 0 | 0 | Year 1 |
22-0021 | 0.4 | 0 | 0 | Year 2 |
22-0022 | 0 | 0 | 0 | Year 1 |
22-0022 | 0 | 0 | 0 | Year 2 |
22-0023 | 0 | 0 | 0 | 6 Weeks |
22-0023 | 0.4 | 0 | 0 | Year 2 |
22-0024 | 0 | 0 | 0 | 6 Weeks |
22-0024 | 0 | 0 | 0 | Year 1 |
22-0024 | 0.5 | 0 | 0 | Year 2 |
22-0025 | 0 | 0 | 0 | 6 Weeks |
22-0025 | 0 | 0 | 0 | Year 2 |
22-0026 | 0 | 0 | 0 | 6 Weeks |
22-0027 | 0 | 0 | 0 | 6 Weeks |
22-0027 | 0 | 0 | 0 | Year 1 |
22-0027 | 0 | 0 | 0 | Year 2 |
22-0028 | 0 | 0 | 0 | 6 Weeks |
22-0028 | 0 | 0 | 0 | Year 1 |
22-0028 | 0 | 0 | 0 | Year 2 |
22-0029 | 0 | 0 | 0 | 6 Weeks |
22-0029 | 1.3 | 0 | 0 | Year 1 |
22-0029 | 0.8 | 0 | 0 | Year 2 |
22-0030 | 0 | 0 | 0 | 6 Weeks |
22-0030 | 0 | 0 | 0 | Year 1 |
22-0030 | 0 | 0 | 0 | Year 2 |
23-0002 | 0 | 0 | 0 | 6 Weeks |
23-0004 | 0 | 0 | 0 | 6 Weeks |
23-0004 | 0 | 0 | 0 | Year 1 |
23-0004 | 0.4 | 0 | 0 | Year 2 |
23-0005 | 0 | 0 | 0 | Year 1 |
23-0006 | 0 | 0 | 0 | 6 Weeks |
23-0006 | 1.1 | 0 | 0 | Year 1 |
23-0007 | 0 | 0 | 0 | 6 Weeks |
23-0007 | 0 | 0 | 0 | Year 1 |
23-0007 | 0 | 0 | 0 | Year 2 |
23-0008 | 0 | 0 | 0 | 6 Weeks |
23-0008 | 0 | 0 | 1.9 | Year 1 |
23-0009 | 0 | 0 | 0 | 6 Weeks |
23-0009 | 0 | 0 | 0 | Year 1 |
23-0009 | 0 | 0 | 0 | Year 2 |
24-0001 | 0 | 0 | 0 | Year 2 |
24-0002 | 0 | 0 | 0 | Year 2 |
24-0004 | 0 | 0 | 0 | 6 Weeks |
24-0004 | 0 | 0 | 0 | Year 1 |
24-0004 | 0.5 | 0 | 0 | Year 2 |
24-0006 | 0 | 0 | 0 | 6 Weeks |
24-0006 | 0 | 0 | 0 | Year 1 |
24-0006 | 0 | 0 | 0 | Year 2 |
24-0007 | 0 | 0 | 0 | 6 Weeks |
24-0007 | 0 | 0 | 0 | Year 1 |
24-0007 | 0 | 0 | 0 | Year 2 |
24-0008 | 1.3 | 0 | 0 | Year 1 |
24-0008 | 0.6 | 0 | 0 | Year 2 |
24-0009 | 0 | 0 | 0 | 6 Weeks |
24-0009 | 0 | 0 | 0 | Year 1 |
24-0009 | 0 | 0 | 0 | Year 2 |
24-0010 | 0 | 0 | 0 | 6 Weeks |
24-0010 | 0 | 0 | 0 | Year 1 |
24-0010 | 0 | 0 | 0 | Year 2 |
24-0011 | 0 | 0 | 0 | 6 Weeks |
24-0011 | 0 | 0 | 0 | Year 1 |
24-0011 | 0 | 0 | 0 | Year 2 |
24-0012 | 0.5 | 0 | 0 | Year 1 |
24-0012 | 0.6 | 0 | 0 | Year 2 |
24-0013 | 0 | 0 | 0 | 6 Weeks |
24-0013 | 1 | 0.7 | 0 | Year 2 |
24-0014 | 0.4 | 0 | 0 | Year 1 |
24-0014 | 0.4 | 0 | 0 | Year 2 |
24-0015 | 0.6 | 0 | 0 | Year 1 |
24-0015 | 0.5 | 0 | 0 | Year 2 |
24-0016 | 0 | 0 | 0 | 6 Weeks |
24-0016 | 0 | 0 | 0 | Year 1 |
24-0016 | 0 | 0 | 0 | Year 2 |
24-0017 | 0 | 0 | 0 | Year 1 |
24-0017 | 0 | 0 | 0 | Year 2 |
24-0018 | 0 | 0 | 0 | 6 Weeks |
24-0018 | 0 | 0 | 0 | Year 1 |
24-0018 | 0 | 0 | 0 | Year 2 |
24-0019 | 0.6 | 0 | 0 | Year 1 |
24-0019 | 0.6 | 0 | 0 | Year 2 |
24-0020 | 0 | 0 | 0 | 6 Weeks |
24-0020 | 0 | 0 | 0 | Year 1 |
24-0020 | 0 | 0 | 0 | Year 2 |
24-0021 | 0 | 0 | 0 | Year 1 |
24-0021 | 0.6 | 0 | 0 | Year 2 |
24-0022 | 0.6 | 0 | 0 | Year 2 |
24-0023 | 0 | 0 | 0 | Year 1 |
24-0023 | 0.5 | 0 | 0 | Year 2 |
24-0024 | 0.3 | 0 | 0 | Year 2 |
24-0025 | 0.3 | 0 | 0 | Year 1 |
24-0025 | 0.6 | 0 | 0 | Year 2 |
24-0026 | 0 | 0 | 0 | Year 1 |
24-0026 | 1.2 | 0 | 0 | Year 2 |
24-0027 | 0 | 0 | 0 | Year 1 |
24-0027 | 0 | 0 | 0 | Year 2 |
24-0028 | 0 | 0 | 0 | 6 Weeks |
24-0028 | 0 | 0 | 0 | Year 1 |
24-0028 | 0 | 0 | 0 | Year 2 |
My first approach, as it is pretty easy to do, is to set the zero values to missing for the plot data set. Without a Y value no point is placed to connect. If all the values are missing the patient won't appear in the plot. If if there is mix of zero and not zero then series plots by default will connect the points. If go this route you likely want to add markers to the plot for those points where there are single non-zero values for a patient and use the BREAK option if you do not want non-sequential x values connected when the intermediate Y value is missing (another reason for the markers).
Since your axis is actually time I find character values like 6 weeks mixed with Year 1 and Year2 odd. The horizontal distance between 6 weeks and Year 1 I would expect to be somewhat less than between Year 1 and Year 2.
I gave specific instructions on how to provide data so that we can use it. Please follow those instructions.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.