Announcement

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

  • Add vertical reference lines in horizontal bars

    I am using -graph hbar- to create horizontal bars on means by groups. Does anyone know how to add vertical reference lines in the chart?

    In the example chart generated by the following code, there would be four reference lines, one on the first 2 bars (N Eng and Mid Atl) for NE, one for N Central (ENC and WNC), one for South (S Atl, ESC, and WSC), and one for West. The values of the reference lines are 28 for NE, 22 for N Central, 46 for South, and 46 for West. These are means by region. Thanks in advance!

    use https://www.stata-press.com/data/r17/citytemp, clear

    graph hbar (mean) tempjan, over(division) over(region) nofill ytitle("Degrees Fahrenheit") ///
    title("Average January temperature") ///
    subtitle("by region and division of the United States") ///
    note("Source: U.S. Census Bureau, U.S. Dept. of Commerce")

  • #2
    The -yline()- option does this. You have to specify the y axis value or values, e.g.,

    Code:
    yline(29 32 40)

    Comment


    • #3
      Thank you Andrew! The lines show up across groups. Is there anyway each group could have its own reference line as shown the the attached example? Thanks!

      Click image for larger version

Name:	Capture1.PNG
Views:	1
Size:	72.3 KB
ID:	1650507

      Comment


      • #4
        You can do it by creating the graph using twoway and using -dropline()-. In graph hbar, you can use code from the graph editor. You need to repeat the following sequence of commands for each line and change the values highlighted in red below to specify the start and end points of the line. It will take some tinkering.

        Code:
        use https://www.stata-press.com/data/r17/citytemp, clear
        
        graph hbar (mean) tempjan, over(division) over(region) nofill ytitle("Degrees Fahrenheit") ///
        title("Average January temperature") ///
        subtitle("by region and division of the United States") ///
        note("Source: U.S. Census Bureau, U.S. Dept. of Commerce")
        
        gr_edit .plotregion1.AddLine added_lines editor 24 103 24 82
        gr_edit .plotregion1.added_lines_new = 1
        gr_edit .plotregion1.added_lines_rec = 1
        gr_edit .plotregion1.added_lines[1].style.editstyle  linestyle( width( sztype(relative) val(.45) allow_pct(1)) color(red) pattern(solid) align(inside)) headstyle( symbol(circle) linestyle( width( sztype(relative) val(.2) allow_pct(1)) color(black) pattern(solid) align(inside)) fillcolor(black) size( sztype(relative) val(1.52778) allow_pct(1)) angle(stdarrow) symangle(zero) backsymbol(none) backline( width( sztype(relative) val(.2) allow_pct(1)) color(black) pattern(solid) align(inside)) backcolor(black) backsize( sztype(relative) val(0) allow_pct(1)) backangle(stdarrow) backsymangle(zero)) headpos(neither) editcopy
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	72.7 KB
ID:	1650513

        Comment


        • #5
          Thanks Andrew! I will look into creating the graph using twoway and using -dropline()-.

          Comment


          • #6
            Thank you, Andrea. Also very helpful for our current projects!

            Comment

            Working...
            X