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

送分小仙女□ 提交于 2019-12-12 04:48:37

问题


I have a list of countries, gdp, and 2 variables for employment

cntry
gdppc
ie_dummy
fe_dummy

Data is initially ordered alphabetically according to cntry, so i first

gsort -gdppc

Sort it by descending GDP, highest to lowest. Now when I generate my graph

graph bar ie_dummy fe_dummy, over(cntry) stack

The x-axis which country is still alphabetically ordered, not reflecting the order of the data.

Any ideas?


回答1:


Wherever this idea originates (MS Excel, maybe), the lesson here is that you don't sort the elements of the graph sorting the data set itself. help graph bar points to a corresponding sort option, used within the graphing command. A simple example is:

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)

In this example, the sorting is done based on the variable total, which is the sum of the public and private shares of spending on education.



来源:https://stackoverflow.com/questions/29378155/why-is-x-axis-in-graph-always-alphabetical-how-can-i-order-x-axis-according-to

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!