Announcement

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

  • Graph Loop: Generating subtitles based on unique variable values (and not variable labels)

    Hi,
    I am trying to write a loop which generates subtitles in graphs based on the unique area name for each region.
    The data is attached below, and while I have read how to put variables labels in graphs as subtitles, I am not sure how to generate graph subtitles to reflect the unique value in variable area_name.

    So
    Code:
     foreach area in 22380 16974 45104 {
            twoway  (line ratio_h_pct10 Year if area=="`area'", sort) ///
                    (line ratio_h_pct25 Year if area=="`area'", sort), ///
                      title (Ratio of HOURLY Wages to Binding Wages) ///
                    subtitle("area_name if area=="`area" ") 
         }

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str5 area str72 area_name int Year float(ratio_h_pct10 ratio_h_pct25)
    "22380" "Flagstaff, AZ"                                                  2005 1.2194175 1.5417476
    "22380" "Flagstaff, AZ"                                                  2006 1.2504854 1.6038835
    "22380" "Flagstaff, AZ"                                                  2007 1.0637037 1.2651852
    "22380" "Flagstaff, AZ"                                                  2008 1.0797101  1.273913
    "22380" "Flagstaff, AZ"                                                  2009 1.0593103  1.257931
    "22380" "Flagstaff, AZ"                                                  2010 1.1103449 1.3393103
    "22380" "Flagstaff, AZ"                                                  2011 1.1333333  1.355102
    "22380" "Flagstaff, AZ"                                                  2012 1.1019608 1.2810458
    "22380" "Flagstaff, AZ"                                                  2013 1.0948718 1.2641026
    "22380" "Flagstaff, AZ"                                                  2014 1.0898734  1.255696
    "22380" "Flagstaff, AZ"                                                  2015 1.0869565 1.2720497
    "22380" "Flagstaff, AZ"                                                  2016 1.1118013 1.3378882
    "16974" "Chicago-Naperville-Arlington Heights, IL Metropolitan Division" 2005 1.1169231 1.5276923
    "16974" "Chicago-Naperville-Arlington Heights, IL Metropolitan Division" 2006 1.1523077 1.5676923
    "16974" "Chicago-Naperville-Arlington Heights, IL Metropolitan Division" 2007  1.193846  1.616923
    "16974" "Chicago-Naperville-Arlington Heights, IL Metropolitan Division" 2008     1.088 1.4466667
    "16974" "Chicago-Naperville-Arlington Heights, IL Metropolitan Division" 2009 1.1006452 1.4141935
    "16974" "Chicago-Naperville-Arlington Heights, IL Metropolitan Division" 2010     1.115    1.3725
    "16974" "Chicago-Naperville-Arlington Heights, IL Metropolitan Division" 2011 1.0945455  1.339394
    "16974" "Chicago-Naperville-Arlington Heights, IL Metropolitan Division" 2012 1.0969697  1.339394
    "16974" "Chicago-Naperville-Arlington Heights, IL Metropolitan Division" 2013 1.1006061  1.350303
    "16974" "Chicago-Naperville-Arlington Heights, IL Metropolitan Division" 2014 1.1054546 1.3648485
    "16974" "Chicago-Naperville-Arlington Heights, IL Metropolitan Division" 2015      1.12 1.3975757
    "16974" "Chicago-Naperville-Arlington Heights, IL Metropolitan Division" 2016 1.1430303 1.4351515
    "45104" "Tacoma-Lakewood, WA Metropolitan Division"                      2005 1.1306123  1.447619
    "42644" "Seattle-Bellevue-Everett, WA Metropolitan Division"             2005 1.2136054 1.6285714
    "45104" "Tacoma-Lakewood, WA Metropolitan Division"                      2006 1.1245085 1.4456095
    "42644" "Seattle-Bellevue-Everett, WA Metropolitan Division"             2006 1.2162516  1.634338
    "42644" "Seattle-Bellevue-Everett, WA Metropolitan Division"             2007  1.225725 1.6443884
    "45104" "Tacoma-Lakewood, WA Metropolitan Division"                      2007 1.1336696 1.4602774
    "42644" "Seattle-Bellevue-Everett, WA Metropolitan Division"             2008  1.234201 1.6505576
    "45104" "Tacoma-Lakewood, WA Metropolitan Division"                      2008 1.1548947 1.4783148
    "42644" "Seattle-Bellevue-Everett, WA Metropolitan Division"             2009 1.1789473 1.5906433
    "45104" "Tacoma-Lakewood, WA Metropolitan Division"                      2009  1.102924  1.405848
    "42644" "Seattle-Bellevue-Everett, WA Metropolitan Division"             2010 1.1883041 1.6128654
    "45104" "Tacoma-Lakewood, WA Metropolitan Division"                      2010 1.1052631 1.4304093
    "45104" "Tacoma-Lakewood, WA Metropolitan Division"                      2011 1.0934256 1.4256055
    "42644" "Seattle-Bellevue-Everett, WA Metropolitan Division"             2011  1.194925 1.6435986
    "42644" "Seattle-Bellevue-Everett, WA Metropolitan Division"             2012 1.1493363 1.5807523
    "45104" "Tacoma-Lakewood, WA Metropolitan Division"                      2012 1.0586283 1.3661505
    "45104" "Tacoma-Lakewood, WA Metropolitan Division"                      2013 1.0598477  1.343852
    "42644" "Seattle-Bellevue-Everett, WA Metropolitan Division"             2013 1.1349293  1.562568
    "42644" "Seattle-Bellevue-Everett, WA Metropolitan Division"             2014  1.139485  1.531116
    "45104" "Tacoma-Lakewood, WA Metropolitan Division"                      2014 1.0826181 1.3508584
    "42644" "Seattle-Bellevue-Everett, WA Metropolitan Division"             2015 1.1700106  1.555438
    "45104" "Tacoma-Lakewood, WA Metropolitan Division"                      2015 1.1108764  1.375924
    "45104" "Tacoma-Lakewood, WA Metropolitan Division"                      2016 1.1478353 1.4192185
    "42644" "Seattle-Bellevue-Everett, WA Metropolitan Division"             2016 1.2238648 1.6124604
    end

  • #2
    Your example data shows a 1-1 correspondence between values of area and values of area_name. Since you want the area_name, the simplest approach is to loop over that variable, rather than the area variable:

    Code:
    levelsof area_name, local(areas)
    
    foreach a of local areas {
            twoway  (line ratio_h_pct10 Year if area_name=="`a'", sort) ///
                    (line ratio_h_pct25 Year if area_name=="`a'", sort), ///
                      title (Ratio of HOURLY Wages to Binding Wages) ///
                    subtitle("`a'")
         }

    Comment


    • #3
      Hi Clyde,
      I am trying to generate graphs for a very small subset of the all the regions in the data. My raw data is huge, and thus I was trying to generate graphs only for specific areas (and not all regions in the data).

      Comment


      • #4
        OK, and presumably, because the area names are so long and unwieldy, you don't want to list them out; you prefer to refer to them by the "area" variable but stick the area_name value into the graph subtitle. Then you could do this:

        Code:
        //    VERIFY EACH VALUE OF AREA HAS ONLY ONE ASSOCIATED AREA NAME
        by area (area_name), sort: assert area_name[1] == area_name[_N]
        
        //    LIST OF AREAS THAT ARE TO BE GRAPHED
        local areas_to_graph 22380 16974 45104
        
        //    LOOP OVER AREAS
        foreach a of local areas_to_graph {
                // GRAB THE NAME OF THE CURRENT AREA
                levelsof area_name if area == "`a'", local(name)
                // MAKE THE GRAPH
                twoway  (line ratio_h_pct10 Year if area=="`a'", sort) ///
                        (line ratio_h_pct25 Year if area=="`a'", sort), ///
                          title (Ratio of HOURLY Wages to Binding Wages) ///
                        subtitle(`name')
        }

        Comment


        • #5
          Thank you Clyde !

          Comment

          Working...
          X