Code Bug Fix: Modifying the scale of X axis in graph

Original Source Link

I’m trying to plot a graph in Stata.
This is my code:

scatter logpgp95 avexpr || lfit logpgp95 avexpr, ylabel(4(2)10) xscale(range(4 10))

It gives me a graph like this:


I want a graph with X-axis starting at 4 and continuing to 10 and the 4 should be where the 2 is.

Something like this(as an example):


If I use the xlabel code only:

scatter logpgp95 avexpr || lfit logpgp95 avexpr, ylabel(4(2)10) xlabel(4(2)10)

I get this:


The problem is that I don’t want 4 to be so far from the starting point.

My guess is that you have a value below 2 for avexpr which has a missing value for logpgp95 for the same observation. In this case the graph will still take that value as a minimum for the x axis but doesn’t plot it as it doesn’t have its y-value.

Try this:

scatter logpgp95 avexpr if !missing(logpgp95, avexpr) || lfit logpgp95 avexpr, ylabel(4(2)10)

Your problem is not reproducible.

I simulated some data with x axis range from 3.5 to 10. Even without asking the x axis labels appear as 4 6 8 10 and there is no enormous space to the left.

set obs 100 
set seed 2803 
range x 3.5 10 
gen y = x + rnormal()
scatter y x || lfit y x 

I have to guess that your real code differs from what we can see. Other way round, we need your data to check what is happening.

Tagged :

Leave a Reply

Your email address will not be published. Required fields are marked *