Announcement

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

  • How can I Add a Word into the Title of the Graph with for Loop Command?

    Hi, there,
    Below is my code to generate 3 graphs in for loop command.
    I just want to export the graphs with the title "Minor Infraction", "Medium Infraction", and "Severe Infraction", respectively.

    clear
    input byte race outcome Minor Medium Severe
    1 1 .7051 .5185 .3046
    2 1 .7008 .4873 .1776
    3 1 .7222 .5551 .3358
    4 1 .7316 .5504 .2054
    1 2 .2429 .3566 .412
    2 2 .2459 .3708 .3545
    3 2 .2305 .3379 .4086
    4 2 .2236 .3404 .3743
    1 3 .0521 .1249 .2877
    2 3 .0533 .142 .4684
    3 3 .0473 .107 .2555
    4 3 .0448 .1092 .4203
    end

    // Set graph style
    grstyle init

    // Add race labels
    label define race_label 1 "Black" 2 "Hispanic" 3 "White" 4 "Other"
    label values race race_label

    // Add outcome labels
    label define outcome_label 1 "Low Action" 2 "Medium Action" 3 "High Action"
    label values outcome outcome_label

    // Define variable name array
    local vars "Minor Medium Severe"

    // Generate graphs in a loop
    foreach var of local vars {
    // Generate horizontal bar graph for the current variable
    graph hbar (mean) `var', over(race) asyvars over(outcome, label(angle(45) format(%9.0g))) ///
    ytitle("Predicted Probability of Disciplinary Action") title("`var'")

    // Export the graph as a PNG file
    graph export "graph_`var'.png", replace
    }

    // Clear graph style settings
    grstyle drop

    Thank you for your code.

  • #2
    Sorry, the question is very simple. I made something wrong. Please ignore this thread.

    Comment


    • #3
      My eyes are blurry. It just needs to add ytitle("Predicted Probability of Disciplinary Action") title("`var' Infraction") to the code and it will work normally.

      Comment

      Working...
      X