Announcement

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

  • Scalars, Commas, and Graphing

    I am making several boxplots comparing two groups on a variety of variables and would like to include the group averages in the graph labels. The code below is an example of how I'm doing this:

    format hvar %10.0fc
    summarize hvar if group == "A"
    scalar group_a = round(`r(mean)', 1)
    summarize hvar if group == "B"
    scalar group_b = round(`r(mean)', 1)
    graph box hvar, over(group, relabel(1 "Group A (Avg. = `=scalar(group_a)')" 2 "Group B (Avg. = `=scalar(group_b)')")) ytitle("HVAR")

    I can't figure out how to get the scalars to display in the graph with commas. Any advice or suggestions on how to do this would be so helpful.

    Thank you,
    Erika

  • #2
    As you don't show a graph I can't see what is going wrong for you, but here is some technique that may help.

    Code:
    sysuse auto, clear
    
    su mpg if !foreign
    local mean0 : di %2.1fc r(mean)
    
    su mpg if foreign
    local mean1 : di %2.1fc r(mean)
    
    graph box mpg, over(foreign) text(44 20 "`mean0'"  44 80 "`mean1'")  ysc(r(. 45)) yla(, ang(h)) ymla(44 "means", noticks ang(h) labsize(*1.8))
    Erika: Please register with your full real name. This is explained in the Advice under FAQ.

    Comment


    • #3
      How about something like this:

      Code:
      summarize hvar if group == "A"
      local group_a : display %10.0fc round(`r(mean)', 1)
      summarize hvar if group == "B"
      local group_b : display %10.0fc round(`r(mean)', 1)
      graph box hvar, over(group, relabel(1 "Group A (Avg. = `group_a')" 2 "Group B (Avg. = `group_b')")) ytitle("HVAR")
      Also, as per the FAQ and Statalist tradition, please consider changing you user name to include some or all of your real name. Use the Contact Us link to make this request to the administrators.

      Comment


      • #4
        Thanks for the response.

        When I run the following code . . .

        sysuse auto, clear
        summarize weight if foreign == 0
        scalar domestic = round(`r(mean)', 1)
        summarize weight if foreign == 1
        scalar foreign = round(`r(mean)', 1)
        graph box weight, over(foreign, relabel(1 "Domestic (Avg. = `=scalar(domestic)')" 2 "Foreign (Avg. = `=scalar(foreign)')")) ytitle("Weight")

        . . . I get the attached graph. I can't the circled numbers to display with commas. Thanks.

        Comment


        • #5
          Nothing in your instructions insists on using commas. You did not follow the advice given independently by Joe and myself to use locals, not scalars.

          Comment


          • #6
            Thank you for the response and clarification - I missed the local vs. scalar distinction in the replies. This works well.

            Best,
            Erika

            Comment

            Working...
            X