使用组合图时控制图形大小

时间:2018-05-25 14:57:07

标签: graphics stata

我正在尝试使用graph combine在Stata中合并四个图表。

结果如下图所示:

figure

所有四个数字应该相同,但由于水平ytitle,前两个数字会被压缩。有没有办法控制graph combine重新调整数字的方式?

我尝试了ysizexsize,但这似乎被graph combine覆盖了。

您可以在下面找到生成图的代码:

sysuse auto, clear
graph drop _all
# delimit ;

* First 2 figures; 
twoway (line weight mpg if foreign == 1, 
        sort ytitle("Some longer ytitle",  orientation(horizontal)) 
        title("Foreign", box bexpand) yla(, ang(h))  xtitle("")
        xlabel(,noticks) name(A1, replace ) graphregion(color(gs16)));
twoway (line weight mpg if foreign == 1, sort 
        ytitle("short", orientation(horizontal)) yla(, ang(h))  xtitle("")
        xlabel(,noticks) name(A2, replace ) graphregion(color(gs16)));
graph combine A1 A2, cols(1) name(A, replace)   imargin(b=0 t=0); 

* Second 2 figures; 
twoway (line weight mpg if foreign == 0, sort ytitle("") 
        title("Domestic", box bexpand)  xtitle("") xlabel(,noticks) 
        name(B1, replace ) graphregion(color(gs16)) );
twoway  (line  weight mpg if foreign == 0, sort ytitle("")  xtitle("")
        xlabel(,noticks) name(B2, replace ) graphregion(color(gs16)));
graph combine B1 B2, cols(1) name(B, replace)   imargin(b=0 t=0); 

* Combining the two
graph combine A B ;

1 个答案:

答案 0 :(得分:2)

您需要将每个ytitle的方向更改为垂直方向,并且只按所需顺序组合图形一次。

以下将根据您的要求为您提供相同尺寸的数字:

sysuse auto, clear
graph drop _all
# delimit ;

* First 2 figures;

twoway (line weight mpg if foreign == 1, 
        sort ytitle("Some longer ytitle",  orientation(vertical)) 
        title("Foreign", box bexpand) yla(, ang(h))  xtitle("")
        xlabel(,noticks) name(A1, replace ) graphregion(color(gs16)));

twoway (line weight mpg if foreign == 1, sort 
        ytitle("short", orientation(vertical)) yla(, ang(h))  xtitle("")
        xlabel(,noticks) name(A2, replace ) graphregion(color(gs16)));

* Second 2 figures;

twoway (line weight mpg if foreign == 0, sort ytitle("") 
        title("Domestic", box bexpand)  xtitle("") xlabel(,noticks) 
        name(B1, replace ) graphregion(color(gs16)) );

twoway (line  weight mpg if foreign == 0, sort ytitle("")  xtitle("")
        xlabel(,noticks) name(B2, replace ) graphregion(color(gs16)));

* Combining the 4 graphs;

graph combine A1 B1 A2 B2;

enter image description here

我还建议第一列中的图表以垂直角度旋转其yaxis刻度标签,使其与第二列中的图形匹配:

enter image description here

请注意,通过减小两个轴的刻度值标签的大小,您可以更加突出ytitle。您可能需要调整ytitleyaxis刻度标签之间的间距。

修改

你可以"蛮力" Stata做你喜欢的事,但你永远不会得到你想要的。这是因为变量ytitle长度会影响整个图形区域。

快速解决方案如下:

sysuse auto, clear
graph drop _all
# delimit ;

* First 2 figures;

twoway (line weight mpg if foreign == 1, 
        sort ytitle("Some longer ytitle",  orientation(h)) 
        title("Foreign", box bexpand) yla(, ang(h))  xtitle("")
        xlabel(,noticks) name(A1, replace ) graphregion(color(gs16)));

twoway (line weight mpg if foreign == 1, sort 
        ytitle("                     short", orientation(h)) yla(, ang(h))  xtitle("")
        xlabel(,noticks) name(A2, replace ) graphregion(color(gs16)));

* Second 2 figures;

twoway (line weight mpg if foreign == 0, sort ytitle("") 
        title("Domestic", box bexpand)  xtitle("") xlabel(,noticks) 
        name(B1, replace ) graphregion(color(gs16)) );

twoway (line  weight mpg if foreign == 0, sort ytitle("")  xtitle("")
        xlabel(,noticks) name(B2, replace ) graphregion(color(gs16)));

* Combining the 4 graphs;

graph combine A1 B1 A2 B2, xsize(7);

请注意代码中的更改,以粗体

表示

enter image description here

你也可以玩这些值,看看你是否可以改进一下:

enter image description here

在后两个数字的graphregion选项中指定右边距也可以改善:

twoway (line weight mpg if foreign == 0, sort ytitle("") 
        title("Domestic", box bexpand)  xtitle("") xlabel(,noticks) 
        name(B1, replace ) graphregion(color(gs16) margin(r=22)));

twoway (line  weight mpg if foreign == 0, sort ytitle("")  xtitle("")
        xlabel(,noticks) name(B2, replace ) graphregion(color(gs16) margin(r=22)));

enter image description here