Stata - 图上的多个旋转图(包括轴两侧的分布)

时间:2014-08-16 18:46:03

标签: data-visualization matlab stata

我想生成一个包含以下两者的单个图:(1)散点图(2)Y轴和X轴左侧和X轴下方的Y和X变量的直方图或核密度函数。

我发现了一个在MATLAB中执行此操作的图表 - 我只想在Stata中生成类似内容:

enter image description here

该图表是使用the following MATLAB code生成的:

n = 1000;
rho = .7;
Z = mvnrnd([0 0], [1 rho; rho 1], n);
U = normcdf(Z);
X = [gaminv(U(:,1),2,1) tinv(U(:,2),5)];

[n1,ctr1] = hist(X(:,1),20);
[n2,ctr2] = hist(X(:,2),20);
subplot(2,2,2); plot(X(:,1),X(:,2),'.'); axis([0 12 -8 8]); h1 = gca;
title('1000 Simulated Dependent t and Gamma Values');
xlabel('X1 ~ Gamma(2,1)'); ylabel('X2 ~ t(5)');
subplot(2,2,4); bar(ctr1,-n1,1); axis([0 12 -max(n1)*1.1 0]); axis('off'); h2 = gca;
subplot(2,2,1); barh(ctr2,-n2,1); axis([-max(n2)*1.1 0 -8 8]); axis('off'); h3 = gca;
set(h1,'Position',[0.35 0.35 0.55 0.55]);
set(h2,'Position',[.35 .1 .55 .15]);
set(h3,'Position',[.1 .35 .15 .55]);
colormap([.8 .8 1]);

更新:“图表组合”的Stata13手动输入恰好就是这个例子(http://www.stata.com/manuals13/g-2graphcombine.pdf)。这是代码:

use http://www.stata-press.com/data/r13/lifeexp, clear
generate loggnp = log10(gnppc)
label var loggnp "Log base 10 of GNP per capita"
scatter lexp loggnp, ysca(alt) xsca(alt) xlabel(, grid gmax) fysize(25) saving(yx)
twoway histogram lexp, fraction xsca(alt reverse) horiz fxsize(25) saving(hy)
twoway histogram loggnp, fraction ysca(alt reverse) ylabel(,nogrid) xlabel(,grid gmax) saving(hx)
graph combine hy.gph yx.gph hx.gph, hole(3) imargin(0 0 0 0) graphregion(margin(l=22 r=22)) title("Life expectancy at birth vs. GNP per capita") note("Source: 1998 data from The World Bank Group")

1 个答案:

答案 0 :(得分:0)

这可能是更好的方法,但这是我接受挑战的快速尝试。

sysuse auto,clear
set obs 1000
twoway scatter mpg price, saving(sct,replace)   ///
xsc(r(0(5000)20000) off ) ysc(r(10(10)50) off)  ///
xti("") yti("") xlab(,nolab) ylab(,nolab)

kdensity mpg, n(1000) k(gauss) gen(x0 d0)
line x0 d0, xsc(rev off) ysc(alt) xlab(,nolab) xtick(,notick) saving(hist0, replace)
kdensity price, n(1000) k(gauss) gen(x1 d1)
line d1 x1, xsc(alt) ysc(rev off) ylab(,nolab) ytick(,notick) saving(hist1, replace)

graph combine hist0.gph sct.gph hist1.gph, cols(2) holes(3)

我也想知道是否有办法改进它。代码不是很整齐,我无法正确对齐线图和散点图而不删除散点图的标记和标签(xcommonycommon并没有真正为我做的工作)。

在Statalist上信用this post