Announcement

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

  • Numeric variable format "lost" in loop where it appears in graph subtitle

    Hi everyone, I have the following loop, with aim to create and save a series of pie charts.
    In the subtitle, I want to recall and show the value of the costofhealthydiet variable (numeric, within many decimals), captured by the `healthydiet' local. This works, but the format of the costofhealthydiet variable is lost, and in the subtitle displays the full value of costofhealthydiet, with all the decimals instead of only 2 (as done by the command "format %3.2f costofhealthydiet"), which is what I'd like. If I use the format line inside the loop, it doesn't work.

    Can somebody help with keeping the 2 decimal format also in the graph subtitle?
    Thanks a lot!

    Here's the code:
    format %3.2f costofhealthydiet
    forvalues i = 1/46 {
    local section_label : label (section) `i'
    local healthydiet = costofhealthydiet
    graph pie costperdayadjusted if section == `i', over(item_name) sort(foodgroup) ///
    title("Food items selected in `section_label''s HDB", size(medium)) ///
    subtitle("Cost share over a full basket cost of `healthydiet'", size(vsmall)) ///
    note("The cost share is calculated based on the (adjusted) cost of each food item selected in the Healthy Diet Basket. The HDB is calculated at the section level, for all outlet types.", size(vsmall)) ///
    plabel(_all percent, size(tiny)) legend(size(vsmall) symxsize(*0.5))
    graph save "$data_outputs/graphs/pie charts/GPH/section`i'.gph", replace
    graph export "$data_outputs/graphs/pie charts/PNG/section`i'.png", replace
    }

  • #2
    in plabel, add format(3.2%f) or whatever you want the format to be.

    Comment


    • #3
      for the subtitle:

      local healthydiet: di %3.2f costofhealthydiet

      Comment


      • #4
        Note that george Ford's fix doesn't fix what may or may not be a problem: regardless of the state of the loop

        Code:
        local healthydiet: di %3.2f costofhealthydiet
        will be interpreted with reference to the first observation in the dataset, namely as if it were

        Code:
        local healthydiet: di %3.2f costofhealthydiet[1]
        Now if that variable holds a constant across the dataset, there will be no problem, but you don't need that code: there is just a number 1.23 or whatever to be shown on all graphs.

        Other way round, if you want to show some value specific to each of 46 groups, then you need different code.

        Comment


        • #5
          or if you want the mean for the group, you could

          Code:
          qui mean costofhealthydiet if section==`i'
          local healthydiet: di %3.2f r(mean)

          Comment


          • #6
            Dear George and Nick, thank you so much for your help! Apologies for not having specified this before, the variable "costofahealthydiet" does have one different value per section, so 46 different ones, and I'd like the respective one to show up in each graph.

            The code "local healthydiet: di %3.2f costofhealthydiet" works in terms of format but, as predicted, only shows the same first observation for all the sections.
            I tried with using "qui mean ..." as suggested by George, but the number somehow disappears in the subtitle and only displays a " . ", and something similar happens when I add "format(3.2%f)" to the plabel option: only a "%" is shown on the graph and no number.
            Attached Files
            Last edited by Elisa Langella; 24 Mar 2023, 03:13.

            Comment


            • #7
              The format needs to be as George Ford typed it %3.2f.

              Otherwise mean doesn't leave r(mean) in its wake. I think that


              Code:
              summarize costofhealthydiet if section==`i', meanonly
              local healthydiet: di %3.2f r(mean)
              should do what you want -- and that a bar chart would be a lot easier to read!

              Comment


              • #8
                It all works now, thanks a lot!!

                Comment

                Working...
                X