Announcement

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

  • Bar Graphs are not displaying value labels when using the foreach command

    I am using Stata/IC 14.2 for Windows, and am using the foreach command to graph a horizontal bar graph for a series of unique values (stratified by a numeric variable), See code below.The code runs, but I am wishing to preserve the label from my numeric variable (called "Abbreviation") on the graph, rather than the value itself. Any advice on how to alter my code to display the value label?

    The variable called "Abbreviation" is formatted as a numeric float, and has values from 0 to 32.

    Code:
     
    foreach value in 0 1 2 3 7 8 9 10 11 15 17 19 21 26 27 28 30 32{
      local which: label (Abbreviation) `value'
      graph hbar (mean) nasal if Abbreviation==`value', ylabel(, angle(0)) over(quarter)  ytitle("Percent") ylabel(0 "0" .2 "20".4 "40" .6 "60" .8 "80" 1 "100") title ("TITLE GOES HERE" "`value'")
      graph export "nasal `value'.wmf", replace
     }

  • #2
    The thread title is a bit misleading: essentially the graph command is not doing what you didn't tell it to do, and foreach is just the framework and entirely innocent.

    But everything looks good from here except that your title option would be better as

    Code:
    title("`which'" "`value'")
    and indeed if you show the label then you shouldn't need the value too.

    You can get all the graphs at once with some variation on

    Code:
    clonevar nasal2 = nasal
    replace nasal2 = 100 * nasal2
    graph hbar (mean) nasal2, by(Abbreviation) ylabel(, angle(0)) over(quarter) ytitle("Percent") ylabel(0(20)100)

    Under the framework of by() the value labels should be shown automatically.

    If that doesn't work, example data sufficient to show the problem would be a good idea.
    Last edited by Nick Cox; 23 Dec 2016, 09:29.

    Comment


    • #3
      Thank you - your suggestion worked well.

      Comment

      Working...
      X