non-numeric_argument
non-numeric_argument

Reputation: 658

How to align the x-axis in combined Stata bar graphs?

I am trying to stack multiple bar graphs on each other. I am using two constant variables in order to force Stata to print the first over option in colours and use the legend. Unfortunately, Stata does not automatically align the x-axis of the different graphs if I am using graph combine. Is there a proper way to left-align the x-axis? I have found a hack by adding blank spaces to the label of "con2" but this is not very exact and depends highly on the used font.

Here is a working example:

sysuse nlsw88, clear
graph drop _all
recode married south (1=100)
collapse married south, by(race)
list
gen con1 = 1
gen con2 = 1
graph bar married, horiz over(race) ///
    over(con1, lab(nolab)) over(con2, relabel(1 "Married")) ///
    legend(off) yscale(range(0 100)) ///
    name(plot1) fysize(42)
graph bar south, horiz over(race) ///
    over(con1, lab(nolab)) over(con2, relabel(1 "Lives in South")) ///
    yscale(range(0 100)) ///
    name(plot2)

graph combine plot1 plot2, cols(1) xcom ycom imargin(0 0 0 0)

Upvotes: 1

Views: 2589

Answers (1)

Nick Cox
Nick Cox

Reputation: 37208

You are drawing separate graphs and graph combine can only get so far in getting them aligned. The answer is to draw your separate graphs as separate panels of the same graph, which is easy with a restructuring of the data.

sysuse nlsw88, clear
recode married south (1=100)
collapse married south, by(race)
list
rename (married south) (what=)
reshape long what , i(race) string j(response)
replace response = "lives in South" if response == "south"
graph hbar what, over(race) over(response) asyvars

The device applies to multiple panels, not just two. You might need to do a little work on the axis labels, as here.

Upvotes: 1

Related Questions