Data visualization with SAS programming

How to annotate boxplot (sgplot)?

Accepted Solution Solved
Reply
Contributor
Posts: 67
Accepted Solution

How to annotate boxplot (sgplot)?

Hi,

 

I have a simple boxplot:

proc sgplot data=sashelp.cars;
	vbox invoice/ category=type;
run; 

Now, I want to annote this by adding some text (actually it's a number) above each box. I did somenthing like this:

 

data anno;
	input function $ x1 $ y1 label;
	datalines;
	text Hybrid 150000 10
	text SUV 150000 20
	text Sedan 150000 30
	text Sports 150000 15
	text Truck 150000 5
	text Wagon 150000 10
	;
run;

proc sgplot data=sashelp.cars sganno=anno;
	vbox invoice/ category=type;
run; 

As a result I get the following error:

WARNING: The X1 option is the incorrect data type. The option will be ignored.
WARNING: The X1 option is the incorrect data type. The option will be ignored.
WARNING: The X1 option is the incorrect data type. The option will be ignored.
WARNING: The X1 option is the incorrect data type. The option will be ignored.
WARNING: The X1 option is the incorrect data type. The option will be ignored.
WARNING: The X1 option is the incorrect data type. The option will be ignored.

 

Could you tell me what's wrong and how to do it right?

 

Chris


Accepted Solutions
Solution
2 weeks ago
SAS Super FREQ
Posts: 1,265

Re: How to annotate boxplot (sgplot)?

[ Edited ]

This seems to put the same category value at the top of each box (at y=150,000).  If user wants to put the numeric values on top of each box (category), I believe the x variable needs to match the character axis, using XC1 as the column in the anno data set.  Personally, I would use a TEXT plot  to draw these values (instead of annotation) as now VBOX allows overlays of basic plots.

View solution in original post


All Replies
SAS Super FREQ
Posts: 1,265

Re: How to annotate boxplot (sgplot)?

Posted in reply to chris2377

I believe the default DRAWSPACE is GraphPercent.  You need to change that to DataValue.  Since x-axis is character, you will need to use XC1 as the column for a character value.  Or, you can use the TEXT plot statement (9.4M3) or SCATTER plot with MARKERCHAR to do the same without annotation.

SAS Super FREQ
Posts: 4,110

Re: How to annotate boxplot (sgplot)?

Posted in reply to chris2377

The data set that you are using for annotation is incorrect. You have switched the 'label' and x1 fields and you have not specified the coordinate systems. See if you can modify the following program to do what you want:

 


data anno;
retain function "text" 
       x1Space 'WallPercent'
       y1Space 'DataValue'
       x1 . y1 150000;
input label $ x1;
datalines;
Hybrid 8.3
SUV    25 
Sedan  41.7
Sports 58.3 
Truck  75 
Wagon  91.7
;


proc sgplot data=sashelp.cars sganno=anno;
	vbox invoice/ category=type;
run; 
Solution
2 weeks ago
SAS Super FREQ
Posts: 1,265

Re: How to annotate boxplot (sgplot)?

[ Edited ]

This seems to put the same category value at the top of each box (at y=150,000).  If user wants to put the numeric values on top of each box (category), I believe the x variable needs to match the character axis, using XC1 as the column in the anno data set.  Personally, I would use a TEXT plot  to draw these values (instead of annotation) as now VBOX allows overlays of basic plots.

Contributor
Posts: 67

Re: How to annotate boxplot (sgplot)?

Posted in reply to Sanjay_SAS

@Sanjay_SAS

Thanks a lot! I didn't think of overlaying the two plots. 

 

But I'll still try to achieve the same output using annotate. @Rick_SAS, thanks for the tips!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 143 views
  • 0 likes
  • 3 in conversation