BookmarkSubscribeRSS Feed
AHW
Calcite | Level 5 AHW
Calcite | Level 5

Hi everyone,

 

I'm trying to make a caterpillar style plot using a dataset sorted by median distance. Each point is classified into a group depending on whether or not its Q1/Q3 extends past the overall median.

 

When I plot the data, the three groups are plotted on top of each other as opposed to in their original sorted order. Is there any way to fix this?

 

Here is a picture of the graph:

Capture.PNG

 

And here is the code used to classify and plot the data:

data MEDIAN_CLASS;
	set SUMM_SORT (obs=200);
	if _n_=1 then set MEDIAN_VALUE;
	if DISTANCE_Q3<OVERALL_MEDIAN then OUTLIER=0;
	else if DISTANCE_Q1>OVERALL_MEDIAN then OUTLIER=2;
	else OUTLIER=1;
run;

ods graphics on /imagemap=on width=11in;
proc sgplot data=MEDIAN_CLASS noautolegend;
	styleattrs datacontrastcolors=(darkgreen darkblue darkred);
	title "Distance from ";
	refline OVERALL_MEDIAN / lineattrs=(thickness=2);
	scatter x=AGENCY_ID y=DISTANCE_median /group=OUTLIER
		yerrorlower=DISTANCE_Q1 yerrorupper=DISTANCE_Q3 errorbarattrs=(thickness=2) dataskin=sheen;
	xaxis type=discrete discreteorder=data label="Agency"
		labelattrs=(size=12) display=none;
	yaxis type=log label='Distance from(km)' grid labelattrs=(size=12)
		valueattrs=(size=10);
quit;
ods graphics off;

Thanks in advance for the help!

5 REPLIES 5
Rick_SAS
SAS Super FREQ

It sounds like you are saying that the green group is displayed first, then the blue group (which might obstruct some of the green), and then the red group (which might obstruct some of the green or blue groups).

 

Yes, that is how groups are plotted. If you don't want that order, you can use another way to assign colors to the markers and error bars. You didn't post sample code, but try something like this:

SCATTER ... / colormodel=(darkgreen darkblue darkred) colorresponse=OUTLIER;

 

I'm not sure if that will color the error bars or not. 

 

Some sample data would be helpful.

Jay54
Meteorite | Level 14

I could not run your code since the data set SUMM_SORT is not included.  You can get some separation of the groups by setting GROUPDISPLAY=cluster.  However, since you have a lot of observations, it may not be much.  You could also try setting TRANSPARENCY.

AHW
Calcite | Level 5 AHW
Calcite | Level 5

Thanks for the responses!

 

So from how I understand it, it's expected for the different groups to overlap on the same axis. Is there any way to color the three groups in a way that preserves the order of the original sorted datasets? For example, if I have the data below:

example.PNG

 

I would want the agencies to be plotted from 1 to 5, left to right, instead of having the Class 1 agencies on top of the Class 0 agencies. Essentially, something like the left graph below:

desired.PNG

ballardw
Super User

@AHW wrote:

Thanks for the responses!

 

So from how I understand it, it's expected for the different groups to overlap on the same axis. Is there any way to color the three groups in a way that preserves the order of the original sorted datasets? For example, if I have the data below:

example.PNG

 

I would want the agencies to be plotted from 1 to 5, left to right, instead of having the Class 1 agencies on top of the Class 0 agencies. Essentially, something like the left graph below:

desired.PNG


To get the behavior on the left you need your X variable to have different values for the groups.

To get the behavior you have on the right you havethe same value for the XAXIS variable for different groups. So what are the values of the X variable?

 

show actual code that generates the graph on the right with the "data" you are showing above.

AHW
Calcite | Level 5 AHW
Calcite | Level 5

Thanks for the help everyone! I ended up just adding a counter variable and then using that as the x-axis scale. The only disadvantage with this is that hovering over the data in the report shows the counter rather than the id, but that's not a big deal.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 2499 views
  • 1 like
  • 4 in conversation