在Stata中,我怎样才能结合不同宽度的盒子? [英] In Stata, how can I combine box plots of different widths?

查看:180
本文介绍了在Stata中,我怎样才能结合不同宽度的盒子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将不同大小的类别中的几个箱形图结合起来。
下面是一个说明问题的例子:

  sysuse auto 

图框mpg,by (rep78,rows(1))name(g1,replace)
图框mpg,由(foreign,rows(1))名称(g2,替换)

图形组合g1 g2, ycom r(2)

这给了我以下结果。





所有的作品根据手册,但我有这个输出的两个问题。
首先 - 美学。就个人而言,我认为横跨相同宽度的情节看起来会更好。其次,更重要的是 - 在更复杂的图形上,类别,轴等的字体大小。也按比例分配。因此,即使我指定了所有图形的中间尺寸的轴标签 - 其中一些会略大或小。



我想知道是否有选项以编程方式强制第二行箱形图的宽度与第一行大小相同。

解决方案

这是你想要的吗? ?它基于一个技巧,但技巧是相当普遍的。

  sysuse auto,清除
展开2
gen what = cond(_n <= 74,rep78 ,6 +国外)
标签def什么6国内7外国
标签val什么什么
图框mpg,由(什么,注意(修复记录和外国)排(2)洞(8 9 10))

逻辑是:


  1. 两个分类变量是纵向组合的。这确保了每个盒子的情节将是相同的大小。通过指定漏洞,我们说服图框将图形放在两行上。


我猜你的标签尺寸问题会在1解决后消失。



为了获得更大的灵活性,您可能需要放弃图框并使用 twoway 代替。 2009年,我在杂志中给出了详细的讨论:您可以直接前往 http://www.stata-journal.com/sjpdf.html?articlenum=gr0039


I'm trying to combine several box plots across categories of different size. Here is an example illustrating problem:

sysuse auto

graph box mpg, by(rep78, rows(1)) name(g1, replace )
graph box mpg, by(foreign, rows(1)) name(g2, replace )

graph combine g1 g2 , ycom r(2)

This gives me the following results.

All works according to the manual so for but I have two problems with this output. Firstly - aesthetics. Personally, I think plot with the same width across rows would look better.

Secondly, and more importantly - on more complex graphs the font size for categories, axes, etc. is also sized proportionally. So even if I specify, let's say - medium size of axis label on all graphs - some of them will be slightly bigger or smaller.

I was wondering if there is an option to programmatically force width of second row of box plots to have the same size as the first one.

解决方案

Is this you want? It is based on a trick, but the trick is quite general.

sysuse auto, clear
expand 2
gen what = cond(_n <= 74, rep78, 6 + foreign)
label def what 6 Domestic 7 Foreign
label val what what
graph box mpg, by(what, note("Repair record and Foreign") row(2) holes(8 9 10))

The logic is that

  1. The two categorical variables are combined lengthwise. That ensures that each box plot will be the same size.

  2. By specifying holes, we persuade graph box to put graphs on two rows.

I guess that your label size problem will disappear once 1 is solved.

For even more flexibility, you may need to abandon graph box and use twoway instead. A detailed discussion was given by me in the Stata Journal in 2009: you can go straight to http://www.stata-journal.com/sjpdf.html?articlenum=gr0039

这篇关于在Stata中,我怎样才能结合不同宽度的盒子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆