BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RoddyJ
Obsidian | Level 7

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;
1 ACCEPTED SOLUTION

Accepted Solutions
JeffMeyers
Barite | Level 11

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;

image.png

 

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;

image.png

 

View solution in original post

2 REPLIES 2
Jay54
Meteorite | Level 14

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

 

JeffMeyers
Barite | Level 11

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;

image.png

 

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;

image.png

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1823 views
  • 6 likes
  • 3 in conversation