2015-05-25 330 views
1

我想堆疊在一起的多個條形圖。我使用兩個常量變量來強制Stata以顏色打印第一個over選項並使用圖例。不幸的是,如果我使用graph combine,Stata不會自動對齊不同圖形的x軸。有沒有正確的方法來左對齊X軸? 我發現通過在「con2」標籤中添加空格,但這不是非常確切,並且高度依賴於使用的字體。如何在組合的Stata條形圖中對齊x軸?

這裏是一個工作示例:

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) 

回答

1

您正在繪製單獨的圖形和graph combine只能得到在得到他們一致爲止。答案是將您的單獨圖形繪製爲同一圖形的單獨面板,對數據進行重構很容易。

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 

該設備適用於多個面板,而不僅僅是兩個。就像這裏,您可能需要在軸標籤上做一些工作。

+0

非常感謝您的快速響應!這個黑客爲我節省了很多時間(和代碼行)。 –

+1

http://www.stata-journal.com/article.html?article=gr0058在其他情況下顯示相同的基本思想。 –

+0

另請參見http://stackoverflow.com/questions/14342210/in-stata-how-can-i-combine-box-plots-of-different-widths –