为什么图中的 x 轴总是按字母顺序排列?如何根据我的数据对 x 轴进行排序?

Why is x-axis in graph always alphabetical? How can I order x-axis according to my data?

我有一个国家列表、gdp 和 2 个就业变量

cntry
gdppc
ie_dummy
fe_dummy

数据最初是根据 cntry 按字母顺序排列的,所以我先

gsort -gdppc

按 GDP 降序排列,从高到低。 现在当我生成图表时

graph bar ie_dummy fe_dummy, over(cntry) stack

X 轴仍按字母顺序排列国家/地区,不反映数据的顺序。

有什么想法吗?

无论这个想法起源于何处(可能是 MS Excel),这里的教训是您不会对图表的元素进行排序,而是对数据集本身进行排序。 help graph bar 指向相应的 sort 选项,在绘图命令中使用。一个简单的例子是:

clear
set more off

sysuse educ99gdp

generate total = private + public

graph hbar (asis) public private, ///
    over(country, sort(total) descending) stack ///
    title( "Spending on tertiary education as % of GDP, 1999", span pos(11) ) ///
    subtitle(" ") ///
    note("Source:  OECD, Education at a Glance 2002", span)

在此示例中,排序是根据变量 total 完成的,该变量是 publicprivate 教育支出份额的总和。