Announcement

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

  • Vertical line hbar graph

    Dear Stata users,
    I am trying to find a way to inetgrate a vertical line in a hbar graph. This graph represents the growth of variable 1, 2 and 3 for four different groups, by year, and I would like to add a vertical line, for each year, at the level of the growth of variable 4 (which takes the same value for each group since it's the growth rate of the population). Here is the code I use, without the vertical line:

    Code:
    graph hbar (asis) var1_growth var2_growth var3_growth, over(groups) ///
    by(year, title("XXX") ///
    graphregion(color(white)) note("") ylabel(,angle(0) axis(1)) ///
    stack legend(order(1 "var1" 2 "var2" 3 "var3") pos(6) col(3))
    Could someone help with adding the line?
    Thanks a lot in advance!
    Best regards,
    Quentin
    Last edited by Quentin Richard; 08 May 2024, 01:59.

  • #2
    Data example please! Graph example please! Please note https://www.statalist.org/forums/help#stata

    This is perhaps surprisingly tricky, unless I am missing something.

    Here is some technique.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(year groups var1 var2 var3 whatever)
    2022 1 1 2 3   2.5
    2023 1 2 3 4   3.5
    2022 2 3 4 5   2.5
    2023 2 4 5 6   3.5
    2022 3 5 6 7   2.5 
    2023 3 6 7 8   3.5 
    2022 4 7 8 9   2.5
    2023 4 8 9 10  3.5 
    end
    
    reshape long var , i(year groups) j(which)
    
    gen axis = which + 4 * (groups - 1) 
    
    separate var, by(which) veryshortlabel
    
    label var var1 "better"
    label var var2 "labels"
    label var var3 "needed"
    
    twoway  ///
    || bar var1 axis, by(year, note("")) ///
    || bar var2 axis ///
    || bar var3 axis, xla(2 "group 1" 6 "group 2" 10 "group 3" 14 "group 4", tlc(none)) xtitle("") ///
    || line whatever axis, lc(black)
    Click image for larger version

Name:	vargroupyear.png
Views:	1
Size:	37.8 KB
ID:	1752649

    Comment


    • #3
      You can naturally make this horizontal. Whether that is a good idea depends, I guess, on whatever are (sensible) variable or value labels for the variables and categories.

      Comment


      • #4
        Dear Nick,
        Thanks a lot for your answer. Sorry for not being clear and not providing the whole stuff.

        My data are structured as follows (for 2 selected years, numbers are fictive)
        Code:
        input float (year group var1 var2 var3 var 4)
        2017   1    0.06       0.01      -0.02        0.03
        2017   2    0.02       -0.04      0.04        0.03
        2017   3    0.03     -0.07       0.01         0.03
        2017   4    -0.03    -0.03      0.02          0.03
        2020   1    0.06     -0.01      -0.01         0.05
        2020   2     0.03     -0.08     0.04         0.05
        2020   3     0.02    -0.12      0.04         0.05
        2020   4    -0.04    -0.09       0.01        0.05
        I would like my graph to look like this one, but with a vertical line at the value of var4 for each year. In particular, I would like var1, var2 and var3 to be cumulated.

        Best regards,
        Quentin

        Click image for larger version

Name:	randomgraph.png
Views:	1
Size:	33.0 KB
ID:	1752660

        Comment

        Working...
        X