Announcement

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

  • bidirectional bar chart with different measurement units

    Hi Statalist,
    I want to draw a graph similar to the following plot. I would appreciate any insights.
    Thanks,

    Click image for larger version

Name:	gr.png
Views:	1
Size:	82.1 KB
ID:	1639778

  • #2
    Response deleted.
    Last edited by William Lisowski; 06 Dec 2021, 19:00. Reason: I misunderstood the question

    Comment


    • #3
      .

      Comment


      • #4
        Dear Saber,

        What you are looking for is the user community contributed package floatplot, created by Nick Cox, which can be installed from the ssc server, like:
        Code:
        ssc install floatplot , replace
        h floatplot
        However, to create your example will require substantial work as it is the combination of three floatplots, each having its own scale.
        I suppose that after you create these you could 'paste' them together with an image editor.
        Not the best solution but workable.
        Note that you will have to 'size' the images, jpg most likely, that you export from Stata rather 'large' before editing to get a fine grained resolution required for print quality.
        For some visual examples what you can accomplish with floatplot have a look at this report about higher educated working professionals in the Netherland which is available here. Never mind that it is in Dutch, the illustrations speak for themselves.
        Last edited by ericmelse; 06 Dec 2021, 22:11.
        http://publicationslist.org/eric.melse

        Comment


        • #5
          I am grateful for the mention, but I can't see myself that floatplot is needed here. There is no stacking of bars.

          Bars based at zero and showing variously positive and negative values are easy enough.

          Perhaps multidot from SSC might help, but the words "similar to" are sufficiently elastic that precise guidance must await a real(istic) data example showing data structure, variable names, and data themselves.

          Comment


          • #6
            Thanks for your replies.

            What I want is to produce the exact same graph however with different variables, and consequently with different measurement units. I have three variables: x, y, and z that I want to generate this graph for. I also have 2 categorical variables, i.e., a and b, where both take 0 and 1.

            As shown in the graph above, I want to calculate the differences of x, y, and z by a, as follows:

            Let dx = the average of x when a=1 minus the average of x when a=0.

            and so on and so forth for y and z.

            Then I want to plot all dx, dy, and dz grouped by b exactly similar to the above graph. Note that x, y, and z have different measurement units.

            Here is a subsample of my dataset, just in case!

            b a x y z
            0 0 8 6.1 110
            0 0 6 5.7 98
            0 1 11 5.7 81
            0 0 7 6.1 102
            0 1 9 5.3 84
            0 1 8 7.7 184
            0 0 6 8.1 177
            0 0 9 7 130
            1 0 6 6.5 113
            1 1 8 6.6 122
            1 1 6 7.3 170
            1 0 3 6.6 125
            1 1 7 6.8 120
            1 0 5 7.4 140
            1 1 7 6.7 160

            Comment


            • #7
              I doubt that I can follow this. I get this far, with


              differences between the means of x, y, z for a = 0 and a = 1

              all for b = 0 and b = 1.

              I get that to be 6 results.


              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input byte(b a x) float y int z
              0 0  8 6.1 110
              0 0  6 5.7  98
              0 1 11 5.7  81
              0 0  7 6.1 102
              0 1  9 5.3  84
              0 1  8 7.7 184
              0 0  6 8.1 177
              0 0  9   7 130
              1 0  6 6.5 113
              1 1  8 6.6 122
              1 1  6 7.3 170
              1 0  3 6.6 125
              1 1  7 6.8 120
              1 0  5 7.4 140
              1 1  7 6.7 160
              end
              
              
              foreach v in x y z {
                  egen mean_`v' = mean(`v'), by(a b)
                  egen mean_0 = mean(cond(a == 0, `v', .)), by(b)
                  egen mean_1 = mean(cond(a == 1, `v', .)), by(b)
                  gen wanted_`v' = mean_0 - mean_1
                  drop mean_0 mean_1
              }
              
              sort b a
              list b a  *x *y *z , sepby(b a)
              
              
                  +--------------------------------------------------------------------------------------------+
                   | b   a    x     mean_x    wanted_x     y     mean_y    wanted_y     z     mean_z   wanted_z |
                   |--------------------------------------------------------------------------------------------|
                1. | 0   0    9        7.2   -2.133333     7        6.6    .3666668   130      123.4   7.066666 |
                2. | 0   0    8        7.2   -2.133333   6.1        6.6    .3666668   110      123.4   7.066666 |
                3. | 0   0    6        7.2   -2.133333   5.7        6.6    .3666668    98      123.4   7.066666 |
                4. | 0   0    7        7.2   -2.133333   6.1        6.6    .3666668   102      123.4   7.066666 |
                5. | 0   0    6        7.2   -2.133333   8.1        6.6    .3666668   177      123.4   7.066666 |
                   |--------------------------------------------------------------------------------------------|
                6. | 0   1    8   9.333333   -2.133333   7.7   6.233333    .3666668   184   116.3333   7.066666 |
                7. | 0   1   11   9.333333   -2.133333   5.7   6.233333    .3666668    81   116.3333   7.066666 |
                8. | 0   1    9   9.333333   -2.133333   5.3   6.233333    .3666668    84   116.3333   7.066666 |
                   |--------------------------------------------------------------------------------------------|
                9. | 1   0    3   4.666667   -2.333333   6.6   6.833333   -.0166664   125        126        -17 |
               10. | 1   0    6   4.666667   -2.333333   6.5   6.833333   -.0166664   113        126        -17 |
               11. | 1   0    5   4.666667   -2.333333   7.4   6.833333   -.0166664   140        126        -17 |
                   |--------------------------------------------------------------------------------------------|
               12. | 1   1    7          7   -2.333333   6.7       6.85   -.0166664   160        143        -17 |
               13. | 1   1    6          7   -2.333333   7.3       6.85   -.0166664   170        143        -17 |
               14. | 1   1    7          7   -2.333333   6.8       6.85   -.0166664   120        143        -17 |
               15. | 1   1    8          7   -2.333333   6.6       6.85   -.0166664   122        143        -17 |
                   +--------------------------------------------------------------------------------------------+

              Comment


              • #8
                Thanks for this. I wanted to produce the above graph using these results.

                Comment


                • #9
                  Here's some graph technique.

                  Code:
                  bysort b : keep if _n == 1
                  keep b wanted*
                  reshape long wanted_, i(b) j(which) string
                  separate wanted_, by(which) veryshortlabel 
                  twoway bar wanted_?  b, by(which, note("") yrescale row(1)   legend(off))  yla(#4) base(0) barw(0.5 .. ) xla(0 1) ytitle(informative text here)
                  Click image for larger version

Name:	attempt1.png
Views:	1
Size:	21.9 KB
ID:	1639911

                  Comment

                  Working...
                  X