Announcement

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

  • How to create three histograms on top of each other

    Hi everyone,

    I would like to display the distribution of my dependent variable at three levels of another variable. I managed to create three histograms next to each other, however it might be nicer to have the three histograms displayed on top of each other. Is there a way to do that? This is the code for my histograms next to each other:
    Code:
     histogram fs_wage if int_flag_wage2==0 & int_flag_survey_dur1_34==0 & p_earnlevels34==0, barwidth(.8) start (-5) plotregion(margin(b=0)) ytitle(`"Percent"') xtitle(`"Justice evaluation of earnings at Q1"') name(hist_Q1, replace)discrete percent ylabel(0(10)40)
    
    histogram fs_wage if int_flag_wage2==0 & int_flag_survey_dur1_34==0 & p_earnlevels34==1, barwidth(.8) start (-5) plotregion(margin(b=0)) ytitle(`"Percent"') xtitle(`"Justice evaluation of earnings at Q2"') name(hist_Q2, replace) discrete percent ylabel(0(10)40)
    
    histogram fs_wage if int_flag_wage2==0 & int_flag_survey_dur1_34==0 & p_earnlevels34==2, barwidth(.8) start (-5) plotregion(margin(b=0)) ytitle(`"Percent"') xtitle(`"Justice evaluation of earnings at Q3"') name(hist_Q3, replace)discrete percent ylabel(0(10)40)
    
    graph combine hist_Q1 hist_Q2 hist_Q3, row(1)
    This is the data I'm using:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte fs_wage double p_earnlevels34 float(int_flag_wage2 int_flag_survey_dur1_34)
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    -5 0 0 0
    end
    label values fs_wage labels125
    label values p_earnlevels34 p_earnlevels34
    label def p_earnlevels34 0 "0. low", modify


  • #2
    Thanks for providing a data example, but all observations you show are identical, so it's not a good guide for what to do.

    What would a negative wage mean here?

    You can superimpose histograms, the main detail being to arrange some transparency so that bars added later do not occlude bars added earlier. It's also usually important to make binning consistent.

    Comment


    • #3
      something like this:

      Code:
      twoway hist x1 , color(red%20) || hist x2 , color(blue%20) || hist x3, color(green%20)

      Comment


      • #4
        Here is another "on top", in case if you meant 1 column 3 rows:

        Code:
        sysuse nlsw88, clear
        histogram wage, by(race, col(1))

        Comment


        • #5
          #2 should be helpful, but not by combining red and green!

          Comment


          • #6
            Hi Nick,

            thanks for your reply. I'm very sorry, below you find a better data example. The dependent variable is fairness judgements of wages on a scale from -5: unfairly low to 5: unfairly high. How can I superimpose histograms?

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input byte fs_wage double p_earnlevels34 float(int_flag_survey_dur1_34 int_flag_wage2)
             1 2 0 0
             0 1 0 0
            -2 2 0 0
            -1 0 0 0
             0 1 0 0
             0 2 0 0
             0 2 0 0
            -1 0 0 0
            -1 0 0 0
             0 1 0 0
            -1 2 0 0
            -1 1 0 0
            -2 0 0 0
            -2 0 0 0
             0 1 0 0
            -2 1 0 0
            -3 0 0 0
             0 1 0 0
             0 2 0 0
             0 1 0 0
            -2 2 0 0
             0 1 0 0
            -1 0 0 0
             0 2 0 0
             0 0 0 0
            -1 2 0 0
            -3 1 0 0
            -5 0 0 0
            -3 2 0 0
            -2 2 0 0
             3 2 0 0
            -2 2 0 0
            -2 0 0 0
             0 1 0 0
             2 1 0 0
             1 2 0 0
            -2 0 0 0
            -3 2 0 0
             0 1 0 0
             0 0 0 0
             0 1 0 0
             0 1 0 0
             0 1 0 0
             0 2 0 0
             0 0 0 0
            -1 1 0 0
             0 0 0 0
             1 2 0 0
             0 0 0 0
            -1 2 0 0
             1 2 0 0
             0 1 0 0
             0 1 0 0
            -1 0 0 0
            -1 1 0 0
             0 2 0 0
             1 2 0 0
             0 0 0 0
             0 1 0 0
            -1 0 0 0
             0 1 0 0
            -1 2 0 0
             0 0 0 0
            -1 2 0 0
             2 2 0 0
             0 2 0 0
            -3 0 0 0
             0 1 0 0
             0 0 0 0
            -2 1 0 0
             0 2 0 0
            -2 0 0 0
             0 1 0 0
            -2 0 0 0
             0 2 0 0
             0 1 0 0
             0 2 0 0
            -1 1 0 0
            -1 0 0 0
            -5 0 0 0
             0 0 0 0
            -1 1 0 0
            -2 2 0 0
             0 2 0 0
            -5 0 0 0
             0 1 0 0
            -5 0 0 0
             0 1 0 0
             0 2 0 0
            -3 2 0 0
             1 2 0 0
            -1 0 0 0
            -1 1 0 0
            -1 0 0 0
             0 1 0 0
            -2 2 0 0
            -1 1 0 0
            -2 0 0 0
             1 2 0 0
            -1 0 0 0
            end
            label values fs_wage labels125
            label def labels125 1 "1. -5 ungerechterweise zu niedrig", modify
            label def labels125 2 "2. -4", modify
            label def labels125 3 "3. -3", modify
            label values p_earnlevels34 p_earnlevels34
            label def p_earnlevels34 0 "0. low", modify
            label def p_earnlevels34 1 "1. average", modify
            label def p_earnlevels34 2 "2. high", modify

            Comment


            • #7
              Thanks for your better example.

              George Ford gave you code for your main idea, but my own ideas about what works relatively well are closer to those of Ken Chui.

              Here a barwidth of 0.9 is a cosmetic choice and so a matter of taste. I also used tabplot from the Stata Journal.

              I don't spell out in the code a restriction to

              Code:
              int_flag_wage2==0 & int_flag_survey_dur1_34==0
              which is true of your data example but very likely needs to be specified for your full dataset.

              Graphs such as superimposed histograms tend to be clear only when distributions don't overlap too much.

              Code:
               
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input byte fs_wage double p_earnlevels34 float(int_flag_survey_dur1_34 int_flag_wage2)
               1 2 0 0
               0 1 0 0
              -2 2 0 0
              -1 0 0 0
               0 1 0 0
               0 2 0 0
               0 2 0 0
              -1 0 0 0
              -1 0 0 0
               0 1 0 0
              -1 2 0 0
              -1 1 0 0
              -2 0 0 0
              -2 0 0 0
               0 1 0 0
              -2 1 0 0
              -3 0 0 0
               0 1 0 0
               0 2 0 0
               0 1 0 0
              -2 2 0 0
               0 1 0 0
              -1 0 0 0
               0 2 0 0
               0 0 0 0
              -1 2 0 0
              -3 1 0 0
              -5 0 0 0
              -3 2 0 0
              -2 2 0 0
               3 2 0 0
              -2 2 0 0
              -2 0 0 0
               0 1 0 0
               2 1 0 0
               1 2 0 0
              -2 0 0 0
              -3 2 0 0
               0 1 0 0
               0 0 0 0
               0 1 0 0
               0 1 0 0
               0 1 0 0
               0 2 0 0
               0 0 0 0
              -1 1 0 0
               0 0 0 0
               1 2 0 0
               0 0 0 0
              -1 2 0 0
               1 2 0 0
               0 1 0 0
               0 1 0 0
              -1 0 0 0
              -1 1 0 0
               0 2 0 0
               1 2 0 0
               0 0 0 0
               0 1 0 0
              -1 0 0 0
               0 1 0 0
              -1 2 0 0
               0 0 0 0
              -1 2 0 0
               2 2 0 0
               0 2 0 0
              -3 0 0 0
               0 1 0 0
               0 0 0 0
              -2 1 0 0
               0 2 0 0
              -2 0 0 0
               0 1 0 0
              -2 0 0 0
               0 2 0 0
               0 1 0 0
               0 2 0 0
              -1 1 0 0
              -1 0 0 0
              -5 0 0 0
               0 0 0 0
              -1 1 0 0
              -2 2 0 0
               0 2 0 0
              -5 0 0 0
               0 1 0 0
              -5 0 0 0
               0 1 0 0
               0 2 0 0
              -3 2 0 0
               1 2 0 0
              -1 0 0 0
              -1 1 0 0
              -1 0 0 0
               0 1 0 0
              -2 2 0 0
              -1 1 0 0
              -2 0 0 0
               1 2 0 0
              -1 0 0 0
              end
              * label values fs_wage labels125
              label def labels125 1 "1. -5 ungerechterweise zu niedrig", modify
              label def labels125 2 "2. -4", modify
              label def labels125 3 "3. -3", modify
              label values p_earnlevels34 p_earnlevels34
              label def p_earnlevels34 0 "0. low", modify
              label def p_earnlevels34 1 "1. average", modify
              label def p_earnlevels34 2 "2. high", modify
              
              twoway histogram fs_wage if  p_earnlevels34 == 0, discrete barw(0.9) lcolor(stc1) fcolor(stc1%10) percent ///
              || histogram fs_wage if  p_earnlevels34 == 1, discrete barw(0.9) lcolor(stc2) fcolor(stc2%10) percent ///
              || histogram fs_wage if  p_earnlevels34 == 2, discrete barw(0.9) lcolor(stc3) fcolor(stc3%10) percent ///
              xla(-5/5) legend(order(1 "0. low" 2 "1. average" 3 "2.high") row(1) pos(12)) name(G1, replace)
              
              tabplot p_earnlevels34 fs_wage, percent(p_earnlevels34) showval col(stc1) barw(0.9) xla(-5/5) xasis  name(G2, replace)
              Click image for larger version

Name:	emily1.png
Views:	1
Size:	20.2 KB
ID:	1771850

              Click image for larger version

Name:	emily2.png
Views:	1
Size:	23.7 KB
ID:	1771851

              Comment


              • #8
                I was in a Christmas mood, Nick. It recently snowed in Alabama.

                Comment

                Working...
                X