Announcement

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

  • How to create a dumbell graph in Stata

    Dear all,

    I would like to ask for your help to create a horizontal graph showing two dots and a line connecting them, one dot for individuals in group 1 and another dot (in a different color) for individuals of group 2. This graph is like a "graph dot" but I think it is known as a dumbell graph.

    The rows in the graph would indicate different characteristics of these individuals, as represented by binary variables. For example, one row would indicate the share of men in groups 1 and 2, while another row would indicate the share of individuals in each group that have a given schooling level (i.e., less than primary complete, primary complete, attended secondary or secondary or more complete).

    My data set is already collapsed to show these statistics for groups 1 and 2. Below you can see an example of a Stata graph that is similar to what I want to do, with the difference that the blue dots would indicate group #1, red dots would indicate group #2 and each row would indicate each binary variables (e.g., Female, Male, Less than primary complete, Primary complete, Attended secondary, Secondary or more complete, Rural, and Urban). And ideally there would exist a line connecting the dots and not need of other dots as in the figure below (I mean: ideally for each row I would like to have the red and blue dots and a line connecting them to make the graph visually clean).

    Any help is greatly appreciated.

    Thanks.
    Click image for larger version

Name:	example_Graph.png
Views:	1
Size:	102.5 KB
ID:	1774559





    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str38 vars float(group1 group2)
    "Female"                                 32.9 50.6
    "Male"                                   67.1 49.4
    "educ_level==Less than primary complete" 27.3 21.2
    "educ_level==Primary complete"           72.3 78.4
    "educ_level==Attended secondary"         14.1 26.6
    "educ_level==Secondary or more complete"         58.6 46.4
    "Rural"                                  26.7 25.3
    "Urban"                                  73.3 74.7
    end
    Last edited by Otavio Conceicao; 18 Mar 2025, 10:46.

  • #2
    I think the usual spelling is dumbbell.

    Here is some technique. labmask is from the Stata Journal: compare https://journals.sagepub.com/doi/pdf...867X0800800208
    Click image for larger version

Name:	dumbbell.png
Views:	1
Size:	34.4 KB
ID:	1774561


    Code:
    set scheme stcolor 
    
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str38 vars float(group1 group2)
    "Female"                                 32.9 50.6
    "Male"                                   67.1 49.4
    "educ_level==Less than primary complete" 27.3 21.2
    "educ_level==Primary complete"           72.3 78.4
    "educ_level==Attended secondary"         14.1 26.6
    "educ_level==Secondary or more complete"         58.6 46.4
    "Rural"                                  26.7 25.3
    "Urban"                                  73.3 74.7
    end
    
    gen which = cond(strpos(vars, "="), substr(vars, 2 + strpos(vars, "="), .), vars)
    
    gen order = cond(_n <= 2, _n, cond(_n <= 6, _n + 1, _n + 2))
    
    labmask order, values(which)
    
    twoway rspike group1 group2 order, lc(black) horizontal yla( 1 2 4 5 6 7 9 10, valuelabel tlc(none)) ///
    ysc(reverse) ytitle("") xtitle(whatever this is) xsc(alt) ///
    || scatter order group1, mc(blue) || scatter order group2, mc(red) ///
    legend(order(2 "group 1" 3 "group 2") pos(6) row(1))

    Comment


    • #3
      Dear Nick Cox , many thanks!

      This is great. Would it be possible to have value labels to the left of the red dots and to the right of the blue ones and omit the horizontal axis to improve the visualization?

      I also wonder whether it is possible to remove the additional space in the graph that exist between "Female/Male" and the "schooling vars" to have equal spaces across rows in the graph.

      Thanks!

      Comment


      • #4
        All those changes are possible with standard
        Code:
         twoway
        choices. I don't agree that they are all desirable, but fire away.

        Comment

        Working...
        X