# Plotting 2 x-axes which correctly "line up" using PROC SGPLOT

#### Disvengeance

##### New Member
I'm familiar with how to plot two difference x-axes in PROC SGPLOT, but it seems that, by default, for a given observation, values on one x-axis don't line up with values on the other x-axis. For example, in the attached figure, the plots of x1 and x2 should have the same "range" across the graph, since every observation in the data I used to make the figure has values for both x1 and x2. Can anyone offer a suggestion on how to get the plot of y vs x1 and y vs x2 to line up correctly? If this is not clear I'll try to clarify.

#### hlsmith

##### Less is more. Stay pure. Stay poor.
Probably have to define the range and ticks. It goes something like:

1 to 8 by 1;

You would need to define it for both axes. Upload your working code and I will jam it in and we can see if it works.

#### Disvengeance

##### New Member
This is the code I used to make the figure I attached above:

Code:
proc sgplot data=data;
title 'plot';
loess x=X1 y=Y / nomarkers lineattrs=(color=black);
loess x=X2 y=Y / x2axis nomarkers lineattrs=(color=black);
label Y='Y';
run;

Last edited:

#### Disvengeance

##### New Member
I improved it a little by defining the min and maximums, but the plots still don't line up perfectly
Code:
proc sgplot data=data;
title 'plot';
loess x=X1 y=Y / nomarkers lineattrs=(color=black);
loess x=X2 y=Y / x2axis nomarkers lineattrs=(color=black);
xaxis label='X1' min=0.0102389 max=2.2346154;
x2axis label='X2' min=0.10 max=2.45;
label Y='Y';
run;

#### Disvengeance

##### New Member
Here is the plot the above code procudes:

#### hlsmith

##### Less is more. Stay pure. Stay poor.

Code:
[SIZE=2][FONT=Courier New][COLOR=#000080][SIZE=2][FONT=Courier New][COLOR=#000080][B][SIZE=2][FONT=Courier New][COLOR=#000080]proc [/COLOR][/FONT][/SIZE][/B][/COLOR][/FONT][/SIZE][/COLOR][/FONT][/SIZE][B][FONT=Courier New][SIZE=2][COLOR=#000080][FONT=Courier New][SIZE=2][COLOR=#000080][FONT=Courier New][SIZE=2][COLOR=#000080]sgplot [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/B][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]data[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]=your_set;[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]/*insert your specifics here*/[/COLOR][/SIZE][/FONT]
[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]xaxis [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]values[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]=(0 [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]to 3 [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]by 0.5[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]);[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]yaxis [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]grid[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
[/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]x2axis [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]values[FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000]=(0 [/COLOR][/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]to 3 [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]by 0.5[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][COLOR=#000000]);[/COLOR][/SIZE][/FONT][/SIZE][/FONT][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
[B][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]run[/SIZE][/FONT][/SIZE][/FONT][/SIZE][/FONT][/B][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2];[/SIZE][/FONT]
[/SIZE][/FONT]

#### Disvengeance

##### New Member
I tried adding that code (I set the max to 2.5 instead of 3), but the plot still don't seem to line up correctly, I had assumed that having the plots line up would be the default but I suppose not.

#### Disvengeance

##### New Member
Just got it, thanks for the help. Apparently setting the (0 to 3 by 0.5) to be the minimum and maximum range for the data works, even though using the min= and max= options does not give the same effect, which I find weird.

Code:
proc sgplot data=test;
title 'plot';
loess x=X1 y=Y/ degree=2 nomarkers lineattrs=(color=black);
loess x=X2 y=Y/ degree=2 x2axis nomarkers lineattrs=(color=red);
label Y='Y';
label X1='X1';
label X2='X2';
xaxis values=(0.0102389 to 2.2346154 by 0.1);
yaxis grid;
x2axis values=(0.10 to 2.45 by 0.1);
run;

#### hlsmith

##### Less is more. Stay pure. Stay poor.
I must have been misinterpreting you question. I thought you just wanted the two X axes to be formatted the same as in post #7, though as seen in post #8, they seem to different but correct in your eyes. Are they on different scales? What is the context for the graph?

#### Disvengeance

##### New Member
The two variables have different scales, but each observation in the data has a value for both variables, so I wanted to plot the relationship between both variables and Y simultaneously. In this case, one line shouldn't end before the other, since for any given value of x1 there must also be a corresponding value of x2.