Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using labels of variable as a title of line plot with tsline

    In this code I am trying to use labels of variable Sector_ID as titles of individual plots, kindly guide: ************************************************** **

    local graphnames
    levelsof Sector_ID, local(categories)
    foreach c of local categories {
    tsline EE if Sector_ID == `c', name(graph`c', replace) ///
    title("`c'") ///
    ytitle("") xtitle("") ///
    local graphnames "`graphnames' graph`c'"
    }

  • #2
    Code:
    webuse grunfeld, clear
    keep if company==1
    lab var invest "Gross investment in 1947 dollars"
    lab var mvalue  "Market value as of Dec. 31 in 1947 dollars"
    lab var kstock   "Stock of plant and equipment in 1947 dollars"
    
    foreach var of varlist invest mvalue kstock{
        tw line `var' year, sort xtitle("") ytitle("") title("`:var lab `var''") saving(gr_`var', replace)
    }
    gr combine gr_invest.gph gr_mvalue.gph gr_kstock.gph
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	31.8 KB
ID:	1744777

    Comment


    • #3
      I guess that Tariq Masood wants to see string values (if Sector_ID is string) or value labels (if Sector_ID is numeric).

      Tariq's code should run without error if the identifier is a numeric variable, but you won't see value labels.

      So, Tariq, which is it? This is why we ask for data examples. Your question has two quite different solutions depending which type of variable you have.

      (In any case why loop at all?

      Code:
      tsline EE, by(Sector_ID)
      is a cleaner way to get those graphs.)

      Comment


      • #4
        Dear Nick Cox and Andrew Musau I am pasting data and commands here,
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input byte(Time_ID Sector_ID) float EE
        1 1 .3770609
        2 1 .37964424
        3 1 .382227
        4 1 .3848091
        5 1 .3873904
        6 1 .3899709
        7 1 .3925503
        8 1 .3951286
        9 1 .3977056
        10 1 .4002813
        11 1 .4028554
        12 1 .405428
        13 1 .4079989
        14 1 .41056785
        15 1 .4131349
        16 1 .4156999
        17 1 .4182628
        18 1 .4208234
        19 1 .4233815
        20 1 .4259372
        21 1 .4284903
        22 1 .4310407
        23 1 .4335883
        24 1 .436133
        25 1 .4386747
        26 1 .44121325
        27 1 .4437486
        28 1 .4462806
        29 1 .4488093
        30 1 .4513345
        31 1 .453856
        32 1 .4563739
        33 1 .458888
        34 1 .4613982
        35 1 .4639045
        36 1 .4664067
        37 1 .4689048
        38 1 .4713986
        1 2 .0017546547
        2 2 .0019141063
        3 2 .002081986
        4 2 .002258339
        5 2 .002443201
        6 2 .002636599
        7 2 .002838551
        8 2 .003049071
        9 2 .003268168
        10 2 .0034958455
        11 2 .003732107
        12 2 .0039769537
        13 2 .0042303856
        14 2 .0044924053
        15 2 .004763014
        16 2 .005042217
        17 2 .00533002
        18 2 .005626434
        19 2 .005931473
        20 2 .006245153
        21 2 .006567495
        22 2 .006898525
        23 2 .007238273
        24 2 .007586773
        25 2 .007944067
        26 2 .008310194
        27 2 .008685206
        28 2 .009069155
        29 2 .0094621
        30 2 .0098641
        31 2 .010275223
        32 2 .010695542
        33 2 .011125128
        34 2 .01156406
        35 2 .01201242
        36 2 .012470296
        37 2 .012937773
        38 2 .013414945
        1 3 .014721413
        2 3 .015329368
        3 3 .015948879
        4 3 .016579906
        5 3 .017222416
        6 3 .01787639
        7 3 .018541818
        8 3 .0192187
        9 3 .01990703
        10 3 .02060683
        11 3 .021318117
        12 3 .02204092
        13 3 .022775274
        14 3 .02352122
        15 3 .0242788
        16 3 .025048066
        17 3 .025829077
        18 3 .02662189
        19 3 .027426576
        20 3 .0282432
        21 3 .02907183
        22 3 .02991255
        23 3 .030765435
        24 3 .031630557
        end
        label values Sector_ID Sector_ID
        label def Sector_ID 1 "Agriculture", modify
        label def Sector_ID 2 "Mining", modify
        label def Sector_ID 3 "Food & Bev", modify
        label var Time_ID "Time_ID"
        label var Sector_ID "Sector_ID"

        xtset Sector_ID Time_ID

        // Create a local macro to store the graph names
        local graphnames ""

        // Loop over the unique values of 'cat'
        levelsof Sector_ID, local(categories)
        foreach c of local categories {
        // Create a time series line graph for each category
        tsline EE if Sector_ID == `c', name(graph`c', replace) ///
        title("`c'") ///
        ytitle("") xtitle("") ///
        // Add the graph name to the local macro
        local graphnames "`graphnames' graph`c'"
        }

        // Combine the graphs into a single graph
        graph combine `graphnames', rows(1) cols(3) ///
        title("Dynamics of Total Factor Energy Efficiency")


        By running this, I am getting a combined graph with titles "1", "2" and "3" while I want them as "Agriculture", "Mining" and "Food & Bev" which are labels of variable Sector_id.
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	48.5 KB
ID:	1744802




        by
        tsline EE, by(Sector_ID) the problem is all Y axes of all three categories have the same axis values, which hide the sectoral variability in the graph.

        Comment


        • #5
          Thanks for the example and code. You should want to look up the value label but leave the numbers as graph names (as spaces and & can't be part of Stata names)

          Not so to the last problem, as an option exists for precisely this situation. See the last command below, which gets you to a good place directly.


          Code:
          clear
          input byte(Time_ID Sector_ID) float EE
          1 1 .3770609
          2 1 .37964424
          3 1 .382227
          4 1 .3848091
          5 1 .3873904
          6 1 .3899709
          7 1 .3925503
          8 1 .3951286
          9 1 .3977056
          10 1 .4002813
          11 1 .4028554
          12 1 .405428
          13 1 .4079989
          14 1 .41056785
          15 1 .4131349
          16 1 .4156999
          17 1 .4182628
          18 1 .4208234
          19 1 .4233815
          20 1 .4259372
          21 1 .4284903
          22 1 .4310407
          23 1 .4335883
          24 1 .436133
          25 1 .4386747
          26 1 .44121325
          27 1 .4437486
          28 1 .4462806
          29 1 .4488093
          30 1 .4513345
          31 1 .453856
          32 1 .4563739
          33 1 .458888
          34 1 .4613982
          35 1 .4639045
          36 1 .4664067
          37 1 .4689048
          38 1 .4713986
          1 2 .0017546547
          2 2 .0019141063
          3 2 .002081986
          4 2 .002258339
          5 2 .002443201
          6 2 .002636599
          7 2 .002838551
          8 2 .003049071
          9 2 .003268168
          10 2 .0034958455
          11 2 .003732107
          12 2 .0039769537
          13 2 .0042303856
          14 2 .0044924053
          15 2 .004763014
          16 2 .005042217
          17 2 .00533002
          18 2 .005626434
          19 2 .005931473
          20 2 .006245153
          21 2 .006567495
          22 2 .006898525
          23 2 .007238273
          24 2 .007586773
          25 2 .007944067
          26 2 .008310194
          27 2 .008685206
          28 2 .009069155
          29 2 .0094621
          30 2 .0098641
          31 2 .010275223
          32 2 .010695542
          33 2 .011125128
          34 2 .01156406
          35 2 .01201242
          36 2 .012470296
          37 2 .012937773
          38 2 .013414945
          1 3 .014721413
          2 3 .015329368
          3 3 .015948879
          4 3 .016579906
          5 3 .017222416
          6 3 .01787639
          7 3 .018541818
          8 3 .0192187
          9 3 .01990703
          10 3 .02060683
          11 3 .021318117
          12 3 .02204092
          13 3 .022775274
          14 3 .02352122
          15 3 .0242788
          16 3 .025048066
          17 3 .025829077
          18 3 .02662189
          19 3 .027426576
          20 3 .0282432
          21 3 .02907183
          22 3 .02991255
          23 3 .030765435
          24 3 .031630557
          end
          label values Sector_ID Sector_ID
          label def Sector_ID 1 "Agriculture", modify
          label def Sector_ID 2 "Mining", modify
          label def Sector_ID 3 "Food & Bev", modify
          label var Time_ID "Time_ID"
          label var Sector_ID "Sector_ID"
          
          xtset Sector_ID Time_ID
          
          // Create a local macro to store the graph names
          local graphnames ""
          
          // Loop over the unique values of 'cat'
          levelsof Sector_ID, local(categories)
          foreach c of local categories {
          local ctext : label (Sector_ID) `c'
          // Create a time series line graph for each category
          tsline EE if Sector_ID == `c', name(graph`c', replace) ///
          title("`ctext'") ///
          ytitle("") xtitle("") ///
          // Add the graph name to the local macro
          local graphnames "`graphnames' graph`c'"
          }
          
          // Combine the graphs into a single graph
          graph combine `graphnames', rows(1) cols(3) ///
          title("Dynamics of Total Factor Energy Efficiency") name(graphALL1, replace)
          
          
          tsline EE, by(Sector_ID, yrescale row(1) note("")) name(graphALL2, replace)

          Comment


          • #6
            Thanks Nick. Now it perfectly works.

            Comment

            Working...
            X