Solved
Contributor
Posts: 26

# How to draw box plot with nonlinear axis

[ Edited ]

Hi,

I'm trying to draw a box plot that looks like the photo.

I have the data, but not sure what codes to use. I think the x-axis is non-linear too.

Is there anyone who can give me an example code that would match the photo as much as possible?

I assume it may go something like....

``````proc boxplot data=have;
plot data*group;
run;

proc sgplot data=have;
hbox data / category=group;
run;``````

Thank you.

Accepted Solutions
Solution
‎09-26-2017 10:33 AM
SAS Super FREQ
Posts: 1,289

## Re: How to draw box plot with nonlinear axis

Mathematically, you cannot have values < 0 on a log axis.  Such values are undefined.

If you have data with values <= 0, then you will need to remove these from the data before using the data in the procedure.  You can set the x-axis min value to some small value (> 0), but the shape of the graph will change based on the min value you choose, whether it is 0.1, or 0.0001.  While I do not recommend this, you can replace the lowest value with "0", but that seems wrong.

All Replies
SAS Super FREQ
Posts: 1,289

## Re: How to draw box plot with nonlinear axis

You can set the x-axis to log.  However, all data must be > 0.

proc sgplot data=sashelp.class;
hbox height / category=sex;
xaxis type=log min=1 logbase=2;
run;

Contributor
Posts: 26

## Re: How to draw box plot with nonlinear axis

Thank you, but is there no other way to include the 0?

Lots of the data are 0, so I'd be losing lots of data, and the distribution will look different...

Posts: 3,275

## Re: How to draw box plot with nonlinear axis

It's impossible. You can't plot the log of zero because it doesn't exist. Maybe you'd like some other non-linear axis, but I don't think SAS can do that.

--
Paige Miller
SAS Super FREQ
Posts: 1,289

## Re: How to draw box plot with nonlinear axis

As Paige suggests, you could define your own transform, and transform the data yourself before using the SGPLOT procedure.  You can certainly "customize" the values displayed on the x-axis back to the untransformed values using the VALUES and VALUESDISPLAY options.

Solution
‎09-26-2017 10:33 AM
SAS Super FREQ
Posts: 1,289

## Re: How to draw box plot with nonlinear axis

Mathematically, you cannot have values < 0 on a log axis.  Such values are undefined.

If you have data with values <= 0, then you will need to remove these from the data before using the data in the procedure.  You can set the x-axis min value to some small value (> 0), but the shape of the graph will change based on the min value you choose, whether it is 0.1, or 0.0001.  While I do not recommend this, you can replace the lowest value with "0", but that seems wrong.

Contributor
Posts: 26

## Re: How to draw box plot with nonlinear axis

Hi Paigemiller and Sanjay,

Thank you. I think what I'll do is try create a panel of histograms to compare the distributions.

Whilst Sanjay's suggestion is tempting, zero number of amino acids means 0, rather than 0.0001, so I don't think it's an option for this example.

Thank you both for your help.

Super User
Posts: 13,941

## Re: How to draw box plot with nonlinear axis

I'm not sure if this is sufficient for your data but you can use a format for the x axis variable to make a log axis "lie to you" and show a 0.

```data junk;
do PlotGroup='Group 1','Group 2';
do loop = 1 to 5;
x = rand('uniform') * 10**loop;
output;
end;
end;
run;
proc format library=work;
value logx 1=0
other=[best6.];
run;
proc sgplot data=junk;
hbox x/category= PlotGroup;
xaxis type=log logbase=10 logstyle=linear

;
format x logx.;

run;```
☑ This topic is solved.