Hello,
I'm using SGPlot to plot a bar chart from a dataset that gets updated once a week with a new observation for a total number of events that week.
The x-axis is getting crowded so I'd like to display the text label for every other axis tick, but keep all the bars. I've tried using the values= option but that removes the bars as well as the tick labels. I've also tried using the FITPOLICY=thin option, which is closer to what I want as it removes tick labels but keeps the data. But that doesn't keep enough of the x axis ticks and removes the most recent tick label which is most important.
Can anyone help me out?
proc sgplot data=set5 noborder noautolegend;
yaxis label = 'Number' valueattrs=(size=10pt) LABELATTRS=(size=10pt) values=(0 to 100 by 5);
xaxis label = 'Week' valueattrs=(size=7pt) LABELATTRS=(size=6pt) values=(1 to &max_week by 2);
vbar week / datalabel datalabelfitpolicy=none datalabelattrs=(size=8) fillattrs=(color='CX0066CC');
format week donweek.;
title1 h=12pt j=left "Figure 2";
keylegend/ title=" ";
run;
Two different things I would look at here. What is hopefully the easier solution is if your x-axis is being turned into a discrete axis (which it looks like). This has the behavior with VALUES to remove the values not in the list. From your X-axis statement it looks like you'd rather have a linear axis potentially. If you add TYPE=LINEAR to your x-axis statement it might just make the fix with your current VALUES statement:
proc sgplot data=sashelp.class;
vbar AGE / response=height;
xaxis type=linear values=(11 to 15 by 2);
run;
Otherwise if you need to keep it as a discrete axis, then it gets more tedious. There is another option called VALUESDISPLAY where you can specify the printed value for each tick on the axis so long as they are listed in the VALUES statement. If you have a lot of bars you would probably want to use macro language to do this:
proc sgplot data=sashelp.cars;
vbar type / response=msrp group=origin;
xaxis values=('Hybrid' 'SUV' 'Sedan' 'Truck' 'Wagon') valuesdisplay=('Hybrid' '' 'Sedan' '' 'Wagon');
run;
It is important to have a VBAR? When x-axis tick values get busy, an HBAR can be a good alternative as there is a lot more space for each tick value.
Or, you could split the x-axis tick values or display them slanted.
https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/grstatproc/p07m2vpyq75fgan14m6g5pphnwlr.htm
Two different things I would look at here. What is hopefully the easier solution is if your x-axis is being turned into a discrete axis (which it looks like). This has the behavior with VALUES to remove the values not in the list. From your X-axis statement it looks like you'd rather have a linear axis potentially. If you add TYPE=LINEAR to your x-axis statement it might just make the fix with your current VALUES statement:
proc sgplot data=sashelp.class;
vbar AGE / response=height;
xaxis type=linear values=(11 to 15 by 2);
run;
Otherwise if you need to keep it as a discrete axis, then it gets more tedious. There is another option called VALUESDISPLAY where you can specify the printed value for each tick on the axis so long as they are listed in the VALUES statement. If you have a lot of bars you would probably want to use macro language to do this:
proc sgplot data=sashelp.cars;
vbar type / response=msrp group=origin;
xaxis values=('Hybrid' 'SUV' 'Sedan' 'Truck' 'Wagon') valuesdisplay=('Hybrid' '' 'Sedan' '' 'Wagon');
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.