How can I put the x axis on top in Stata? - stata

I'm sorry if this question has been asked before, I couldn't find it anywhere.
I just want to "move" the x axis from the bottom of the graph to the top. So "10" on the x axis should be next to "10" on the y axis (I hope this makes sense).
this is the graph
Here's my code:
twoway scatter h a, xscale(log) yscale(log) ///
xscale(r(10 100000)) ///
yscale(r(0.0000001 0.1)) ///
xlabel(10 100 1000 "1'000" 10000 "10'000" 100000 "100'000", grid labsize(small)) ///
ylabel(1 "10" 0.1 "100" 0.01 "1'000" 0.001 "10'000" 0.0001 "100'000" 0.00001 "1 Mio." 0.000001 "10 Mio." 0.0000001 "100 Mio.", angle(0) labsize(small)) ///
xtitle("{bf:Gesamtschaden}" "Mio. CHF", ///
size(small)) ///
ytitle("{bf:Häufigkeit}" "1 x in ... Jahren", ///
size(small)) ///
Many thanks in advance!

You can use axis suboption alt to alternate the default location of an axis (i.e. if default is bottom, alt will move it to the top)
twoway scatter h a, xscale(log alt) yscale(log) ///
xscale(r(10 100000)) ///
yscale(r(0.0000001 0.1)) ///
xlabel(10 100 1000 "1'000" 10000 "10'000" 100000 "100'000", grid labsize(small)) ///
ylabel(1 "10" 0.1 "100" 0.01 "1'000" 0.001 "10'000" 0.0001 "100'000" 0.00001 "1 Mio." 0.000001 "10 Mio." 0.0000001 "100 Mio.", angle(0) labsize(small)) ///
xtitle("{bf:Gesamtschaden}" "Mio. CHF", ///
size(small)) ///
ytitle("{bf:Häufigkeit}" "1 x in ... Jahren", ///
size(small)) ///

I agree with #langtang. Here is a reproducible example, with some tweaks to make it slightly interesting.
webuse grunfeld, clear
scatter invest mvalue, ysc(log) xsc(log alt) yla(5000 1000 500 100 50 10 5 1, ang(h)) xla(5000 2000 1000 500 200 100 50) ms(none) mla(company) mlabpos(0)

Related

Modifying the scale of X axis in graph

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.
clear
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.

Spacing for y-axis labels in coefplot

I am working with the community-contributed command coefplot in Stata.
I have a large number of estimated coefficients, which I would like to plot on the same graph.
As such, I would like to reduce the spacing between coefficients.
Consider the following toy example using Stata's auto toy dataset:
quietly sysuse auto, clear
quietly regress price mpg trunk length turn
coefplot, drop(_cons) xline(0)
How can the spacing between Mileage (mpg) and Trunk space (cu. ft.) be decreased?
Some white space around the graph is an unavoidable limitation because of how Stata's graphics system works. With that said, an alternative way around this (which does not tinker with the aspect ratio of the graph) is to increase the range of the y-axis.
For example:
forvalues i = 1 / 4 {
coefplot, drop(_cons) xline(0) yscale(range(-`i' `=6+`i''))
}
A different but related approach, is to turn off the y labels entirely and use marker labels instead:
forvalues i = 1 / 4 {
coefplot, drop(_cons) ///
xline(0) ///
yscale(range(-`i' `=6+`i'')) ///
yscale(off) ///
mlabels(mpg = 12 "Mileage" ///
trunk = 12 "Trunk space (cu. ft.)" ///
length = 12 "Length (in.)" ///
turn = 12 "Turn Circle (ft.)")
}
In both approaches, the starting and ending positions (i.e. the amount of space above and below the labels) can be set by tweaking the values specified within the range() suboption.
Note that the grid lines can be turned off by using the option grid(none).
In addition, by combining the at(matrix()) option and yscale(range()) one can allow for unequal reductions in the distance of the coefficients:
matrix A = (0.2,0.21,0.22,0.225,0.255)
coefplot, drop(_cons) ///
xline(0) ///
yscale(range(0.18 0.26)) ///
yscale(off) ///
mlabels(mpg = 12 "Mileage" ///
trunk = 12 "Trunk space (cu. ft.)" ///
length = 12 "Length (in.)" ///
turn = 12 "Turn Circle (ft.)") ///
at(matrix(A)) ///
horizontal

Slider bar math help needed see inside

Hey all i stink at math here's what i want to do in QML:
I have a slider bar that I want the min left edge to be 30 and the max right edge to be 100
what math puts 30 on the left end but gives all the percentages in between 30-100 to reflect 0-100% on the screen?
Thanks,
Chris
Given a value between 30 and 100:
percent = 100 * (value - 30) / 70
Or more generally:
percent = 100 * (value - min) / (max - min)

Stablize a value in c++

I have a variable which is a Modulus of Congruence x=y(mod 360),which means y varies from 0 - 360 and if the value is greater than 360 it again comes to 0. For example x=5 for y = 365 .
I wrote this function to stabilize y , so if the difference between x and previousx is greater than 5 then i get x otherwise previousx .
float stabilize(float x,float previous){
if(fabs(x-previousx)<5)
{
return previousx;
}
else
{
return x;
}
}
This works fine between 0 to 360 But this fails on the boundary condition of 360 and 0 .How can i stabilize the value when y is a value near 0 such as 0.3 and previous y is near 360 such as 359. So the difference calculated here is 359 - .3 = 358.7 . but i want it to be the modulo 360 difference which is 1.3 .
What about something like if(fabs(x-previousx)<5 || fabs(x-previousx)>355)? Given that the input data is mod 360, if the difference is big enough it means that both values are close enough to the border.
You could subtract the original numbers and take modulo 360 of the fabs(result):
for example:
359 - 720.3 = -361.3
fabs(-361.3) = 361.3
361.3 % 360 = 1.3

Percentage calculation around 0.5 (0.4 = -20% and 0.6 = +20%)

I'm in a strange situation where I have a value of 0.5 and I want to convert the values from 0.5 to 1 to be a percentage and from 0.5 to 0 to be a negative percentage.
As it says in the title 0.4 should be -20%, 0.3 should be -40% and 0.1 should be -80%.
I'm sure this is a simple problem, but my mind is just refusing to figure it out :)
Can anyone help? :)
What we want to do is to scale the range (0; 1) to (-100; 100):
percentage = (value - 0.5) * 200;
The subtraction transforms the value so that it's in the range (-0.5; 0.5), and the multiplication scales it to the range of (-100; 100).
percent = ((value - 0.5) / 0.5) * 100
This will generate from -100 to 100. You want to subtract your zero value (0.5) from the given value, and divide by the range that should give 100% (also 0.5 in your example). Then multiply by 100 to convert to percentage.
Normalize it, and you're done:
// Assuming x is in the range (0,1)
x *= 2.0; // x is in the range (0,2)
x -= 1.0; // (-1,1)
x *= 100; // (-100,100)