问题
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