I am trying to plot GDP for a set of countries and generate a legend which identifies each line as the correct country. My data set consists of three variable; the date, GDP per capita (gdp_pc), and a country identifier (ctry).
My current code to produce the graph is:
Code:
local lp forval ctry = 1/3 { local lp `lp' line gdp_pc date if ctry == `ctry' || } twoway `lp' , /// ytitle(GDP per capita) title("GDP")
My actual data set contains 15 lines and therefore I am looking for an automatic process rather than something like legend(order( 1 "1" 2 "2" 3 "3")).
Array
An extract of the data set is copied below:
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float date double gdp_pc long ctry 220 44378.13125 1 221 44393.629 1 222 44477.08575 1 223 44560.5425 1 224 44643.99925 1 225 44727.456 1 226 44915.3325 1 227 45103.209 1 228 45291.0855 1 229 45478.962 1 230 45695.428 1 231 45911.894 1 232 46128.36 1 233 46344.826 1 234 46486.987 1 235 46629.148 1 220 41472.44525 2 221 41596.352 2 222 41682.28375 2 223 41768.2155 2 224 41854.147249999995 2 225 41940.079 2 226 42079.40875 2 227 42218.7385 2 228 42358.06825 2 229 42497.398 2 230 42594.18625 2 231 42690.974500000004 2 232 42787.76275 2 233 42884.551 2 234 42966.942500000005 2 235 43049.334 2 220 17931.56725 3 221 18112.897 3 222 18325.571 3 223 18538.245 3 224 18750.918999999998 3 225 18963.593 3 226 19180.462249999997 3 227 19397.331499999993 3 228 19614.200749999996 3 229 19831.07 3 230 20019.049249999996 3 231 20207.028499999993 3 232 20395.007749999997 3 233 20582.987 3 234 20783.269 3 235 20983.551 3 end format %tq date label values ctry ctry label def ctry 1 "1. Austria", modify label def ctry 2 "2. Belgium", modify label def ctry 3 "3. Bulgaria", modify
Thank you for your suggestions,
Chris
0 Response to Loop variable values as labels in a forval line graph
Post a Comment