Ellie.B
Ellie.B

Reputation: 25

Title of second y-axis in stata

I am trying to create a graph with two y axes (below code), but the title of the right-side y-axis does not appear. Can anyone please help me with that?

sysuse auto, clear

generate kpl=mpg*0.425144 

twoway (scatter mpg weight, color(navy) yaxis(1)) (scatter kpl weight, color(navy) yaxis(2) ylabel(4.25 8.5 12.75 17, axis(2)) ytitle(Kilometres per Litre, axis(2))),  by(foreign, legend(off) note(Graphs by Car origin))

graph

enter image description here

Upvotes: 1

Views: 1834

Answers (1)

Nick Cox
Nick Cox

Reputation: 37258

I think I understand what you want but I would approach it quite differently.

If you want a second scale of km per litre to compare with miles per gallon, that is just the same data points explained differently, just as you could show Celsius and Fahrenheit temperatures on different axes or calculate proportions and show percents, or vice versa.

Another variable holding km per litre makes this more difficult, not easier, as the values differ by the corresponding conversion factor.

Here I use mylabels from SSC, which must be installed before you can use it.

Naturally you don't need to show zero, but the identity 0 miles per gallon = 0 km per litre may make the point easier to follow.

sysuse auto, clear

set scheme s1color 

mylabels 0(4)16, myscale(@/0.425144) local(yla)

scatter mpg weight, yaxis(1 2) yla(`yla', axis(2) ang(h)) yla(0(10)40, axis(1) ang(h)) ytitle(km per litre, axis(2)) ms(Oh)

enter image description here

Upvotes: 1

Related Questions