Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
925 views
in Technique[技术] by (71.8m points)

stata - Two y axis bar graph with string x axis

I am using Stata and seeking to make a bar graph that has two separate y-axes with the x-axis being different countries.

I have two variables (tlsize = total loan and trblgdp = total loan / GDP). My assumption was to use this code

twoway (bar tlsize country) (bar trblgdp country, yaxis(2))

This said it won't work because country is a string variable, yet that is exactly what I want.

I also tried

 graph bar (mean) trblgdp (mean) tlsize, over(country)

This code works but I am unsure how to add a second y-axis.

question from:https://stackoverflow.com/questions/65926684/two-y-axis-bar-graph-with-string-x-axis

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the absence of a data example, as requested, a silly toy example must serve.

Possibilities include:

  1. As the error message is already telling you, twoway bar requires a numeric variable for the x axis, so you would need to produce one using say encode. But then you would still need to work at the graph to ensure that the bars for larger quantities don't occlude those for smaller quantities, for example by offsetting bars.

  2. graph bar supports a string variable for the categorical axis, but you'd have to work out yourself some way of showing very different quantities on the y axis.

In practice, (vertical) bar charts for data like yours are often problematic:

a. The default ordering of names alphabetically is rarely helpful or desirable. graph bar makes this easier by allowing sorting on any variable; otherwise it requires some work to get to a better solution.

b. The names you want to show as axis labels are often long enough to cause a mess, and although there are work-arounds for this (vertical or slanted labelling; abbreviation; small fonts) none is attractive. (Using a legend instead often works badly too.) In the case of countries there is another work-around of using standard two or three-letter abbreviations, which are fairly widely understood.

For these and other reasons, horizontal bar charts are often preferable.

multidot from SSC is an attempt to address all these problems for showing two or more outcomes on different scales for a set of categories. As the syntax and graph example show, multidot defaults to a dot chart but can be recast to a horizontal bar chart. More at this thread.

clear
input str42 country y1 y2 
"Afghanistan" 4  1000
"Belgium" 2      3000
"Cameroon"  1    2000 
"Dominican Republic" 3  4000
end 

label var y1 "something interesting"
label var y2 "another response"

ssc install multidot 
multidot y1 y2, over(country) recast(bar) scheme(s1color) ytitle("")

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...