Announcement

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

  • Including local variable in graph title

    I need advice on code for graph titles. I'm making a series of pie charts describing types of events that occurred in 50 year intervals. The code below makes the graphs. I want each graphs' title to reflect the interval. The first interval is 1500 to 1549. The locals y and z contain the correct values as the loop iterates, but they do not appear in the title, which instead reads like " ... temp_y ... ". Any advice on how I could get the titles to display the number (e.g. 1500) rather than temp_y would be appreciated.

    Thanks

    Gary

    forval x=1550(50)1950 {
    gen temp_y = `x'-50
    local y temp_y
    gen temp_y1 = `x'-1
    local z temp_y1
    graph pie tpre`x'_* if decade==`x'-10, sort descending title("Event Types from `y' to `z'")
    drop temp_*
    }

  • #2
    The commands -local y temp_y- and -local z temp_z- do not put numeric values into local macros y and z. Rather they put the literal strings "temp_y" and "temp_z" in there, respectively. Also, while not causing a problem, there is no point in creating the variables temp_y and temp_y1 unless you have some use for them later. (Even then, since these "variables" are actually constants, it would probably be better to store them as scalars or local macros.)

    I think the following code will do what you want:

    Code:
    forvalues x = 1550(50)1950 {
        local y = `x' - 50
        local z = `x' - 1
        graph pie ... title("Event Types from `y' to `z'")
    }
    Notice the use of the = in the -local- commands here to force evaluation of numbers, rather than putting formula strings into the local macros.

    Note: not tested. Beware of typos.

    Comment


    • #3
      Thanks Clyde. It worked great.

      Comment


      • #4
        Did it really work well? By issuing 9 separate pie charts you aren't even guaranteed the same legends -- unless every type of event occurred in every period.

        Comparing pie charts is usually hard work. In principle the idea of a pie chart is widely understood; in practice a bar chart is usually easier to think about.

        If you give some example data, alternatives can be discussed.

        Comment


        • #5
          Hi Nick. Yes, it worked. We are doing pie charts in different chapters of books. Simple to understand, and readers retain information in head across chapters. Gary

          Comment

          Working...
          X