Announcement

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

  • Foreach using variable labels

    I am creating graphs from survey variables and use foreach to replicate the graphs over many variables. I would like to separate the graphs using subtitles with the variable labels. My script now have `var' as subtitle, and I plan to use the Graph Editor to change the subtitles from `var' to `var' label. But, I hope there is a way I can call the `var' label instead of the `var' name in the subtitle, so that I do not need to edit all the graphs. Hope someone has a fix for this.


    Code:
    foreach var of varlist new0q32N1-new0q32N12 {
    graph bar [aweight = utvalgsvekt], over(`var', label(angle(forty_five) labsize(small))) nofill blabel(bar, size(small) format(%9.2f)) ytitle("Prosent" " ") ytitle(, size(small)) yscale(range(0 70)) ylabel(, labels labsize(small) angle(horizontal) noticks) by(, title("Hvor positiv eller negativ er du til følgende tiltak? " " ", size(medium)) caption(, size(vsmall)) subtitle(`var', size(small))) name(`var'Alder, replace) by(AlderFordeling) subtitle(, size(small))
     }
    Best,
    Frode
    Click image for larger version

Name:	new0q32N7Alder.png
Views:	1
Size:	90.4 KB
ID:	1677988

  • #2
    Code:
    foreach var of varlist new0q32N1-new0q32N12 {
    local label : variable label `var'
    graph bar ..., subtitle(`label')
    }

    Comment


    • #3
      Within your loop, look up the variable label and use that in the subtitle()

      Code:
      local which : var label `var'  
      
      if `"`which'"' == "" local which `var'  
      
      ... subtitle(`"`which'"') ...
      That said, I think you can improve your graph.

      0. Repetitious labelling.

      1. The default scheme s2color is not widely appreciated.

      2. Text labels being aligned at 45 degrees is a bad sign.

      3. You can respect the meanings of your outcome scale Negative -- Neither -- Positive -- Don't know with some colours.

      4. If you're showing numbers by bars, why show them on the vertical axis too?

      I faked a data example -- for which I used labmask from the Stata Journal, which you won't need -- and tried a different graph using tabplot, also from the Stata Journal.


      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float var1 str5 var2 str12 var3
      21.46 "18-29" "Negativ"    
      31.93 "18-29" "Verken eiler"
      32.78 "18-29" "Positiv"    
      13.82 "18-29" "Vet ikke"    
      31.38 "30-39" "Negativ"    
      22.86 "30-39" "Verken eiler"
      32.71 "30-39" "Positiv"    
      13.05 "30-39" "Vet ikke"    
      36.11 "40-59" "Negativ"    
      20.87 "40-59" "Verken eiler"
      34.79 "40-59" "Positiv"    
       8.23 "40-59" "Vet ikke"    
      39.35 "60+"   "Negativ"    
      22.67 "60+"   "Verken eiler"
      31.29 "60+"   "Positiv"    
       6.69 "60+"   "Vet ikke"    
      end
      
      egen var4 = seq(), to(4)
      labmask var4, values(var3)
      
      set scheme s1color
      
      tabplot var4 var2 [iw=var1], separate(var4) subtitle("I don't know Norwegian really") ///
      bar1(color(red*0.5)) bar2(fcolor(none) lcolor(black)) bar3(color(blue*0.5)) bar4(color(teal))  showval ytitle(Prosent) xtitle(Alder)
      Click image for larger version

Name:	norwegian.png
Views:	1
Size:	25.5 KB
ID:	1677995




      Comment


      • #4
        Thanks to Nick and Øyvind for suggesting the use of local, which fixed my main problem. Also thanks for suggesting improvements to the graphs. I will test out some possibilities before I decide on a final version.

        Comment

        Working...
        X