Bert Breitenfelder
Bert Breitenfelder

Reputation: 13

Controlling graph sizes when using combining graphs

I am trying to combine four graphs in Stata using graph combine.

The result is shown in the following figure:

figure

All four figures should be of equal size but because of the horizontal ytitle, the first two are compressed. Is there a way to control how graph combine re-sizes the figures?

I have tried ysize and xsize but this seems to be overwritten by graph combine.

Below you can find the code that generates the figure:

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 ;

Upvotes: 0

Views: 9333

Answers (1)

user8682794
user8682794

Reputation:

You need to change the orientation for each ytitle to vertical and combine the graphs only once in the desired order.

The following will give you figures of equal size as per your request:

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

I would also recommend for the graphs in the first column to rotate their yaxis tick labels in a vertical angle so they match those in the graphs in the second column:

enter image description here

Notice that by decreasing the size of the tick value labels for both axes, you can give more prominence to the ytitle. You may need to adjust the spacing between the ytitle and the yaxis tick labels though.


EDIT:

You can "brute force" Stata to do what you like but you will never get exactly what you want. This is because of the variable ytitle length, which affects the entire graph area.

A quick solution is the following:

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);

Notice the changes in the code, which are indicated in bold.

enter image description here

You can also play around with the values and see if you can improve things a bit:

enter image description here

Specifying a right margin in the graphregion option of the second 2 figures also improves things:

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

Upvotes: 1

Related Questions