Richard Herron
Richard Herron

Reputation: 10092

Space efficient combination of three graphs with common x-axis in Stata

I am looking for something like the following by() graph, but instead of three panels for three groups, I want three panels with three different y variables.

webuse grunfeld, clear
keep if inrange(company, 1, 3)
twoway line mvalue year, by(company, cols(1) compact)

I like the compactness and one common x-axis for all three panels. Can I this with different y variables?

I can combine the following three panels with graph combine. But this duplicates the x-axis and wastes a lot of space that could be better spent on y range.

webuse grunfeld, clear
keep if (company == 1)
tsline mvalue, name(mvalue, replace)
tsline kstock, name(kstock, replace)
tsline invest, name(invest, replace)
graph combine mvalue kstock invest, cols(1) name(combined, replace)

I started stripping away x-axis titles and labels, but this affects the relative sizes of each panel. Is there a more straightforward solution here?

webuse grunfeld, clear
keep if (company == 1)
tsline mvalue, name(mvalue, replace) xtitle("") xlabel(none)
tsline kstock, name(kstock, replace) xtitle("") xlabel(none)
tsline invest, name(invest, replace)
graph combine mvalue kstock invest, cols(1) name(combined, replace)

Upvotes: 0

Views: 2491

Answers (3)

Richard Herron
Richard Herron

Reputation: 10092

A third approach is to reshape long data, then use variable labels in by().

webuse grunfeld, clear
keep if (company == 1)

keep year mvalue kstock invest
foreach v of varlist mvalue kstock invest {
    rename `v' value`v'
}
reshape long value, i(year) j(var) string

label define var 1 "mvalue" 2 "kstock" 3 "invest"
rename var var0
encode var0, generate(var) label(var)
xtset var year
tsline value, by(var, cols(1))

enter image description here

Upvotes: 0

dimitriy
dimitriy

Reputation: 9460

Try this:

webuse grunfeld, clear
keep if (company == 1)
tsline mvalue, name(mvalue, replace) xscale(off)
tsline kstock, name(kstock, replace) xscale(off)
tsline invest, name(invest, replace) 
graph combine mvalue kstock invest, cols(1) name(combined, replace) xcommon imargin(b=1 t=1)

The xscale(off) option suppresses the x-axis, and the imargin() cuts down the space at the top and bottom of each graph to make them smaller, and xcommon makes the scale the same.

This yields:

enter image description here

Upvotes: 3

Nick Cox
Nick Cox

Reputation: 37208

I wrote a program sparkline primarily motivated by display of multiple time series.

webuse grunfeld, clear
set scheme s1color 
ssc inst sparkline 
sparkline invest mvalue kstock year if company == 1 

enter image description here

Upvotes: 1

Related Questions