In Stata, how can I force the graph to stop displaying after a certain point on the x-axis?
For instance, say that I have:
sysuse auto2, clear
gen mid = (price + weight)/2
gen n = _n
twoway ///
(rcap price weight n, horizontal) /// //
(scatter n mid), ///
ylabel(, nolabels noticks nogrid) ///
legend(off) ///
xscale(range(0 7000)) ///
xlabel(0(1000)7000)
In this MWE, I am trying to force it to stop displaying after 7,000, but it doesn't work. I imagine that the xcale
and xlabel
might be getting overridden so that all of the rcap
's and scatter
elements can be drawn. What can I do to achieve my desired result?
I was surprised that I couldn't find a duplicate question on this. It's understandably a common question.
See documentation of axis scale options
which explains that no axis scale option choice truncates display.
You can try an if
condition.
In extreme situations when that doesn’t work clone your variable or variables and replace undesired values to missing. In many real examples a logarithmic or other nonlinear scale may be a better idea.
https://www.statalist.org/forums/forum/general-stata-discussion/general/1611713-xscale-with-line-graphs is fairly typical dialogue for this kind of question, which certainly is quite common on Statalist.