Announcement

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

  • How do I plot horizontal bar chart with numerical bar labels for panel data

    Click image for larger version

Name:	Screenshot_20221211-222010.jpg
Views:	3
Size:	49.9 KB
ID:	1693000 How do I plot an horizontal bar chart with value labels like the bar chart attached below for panel data.

  • #2
    Here is an example:

    Code:
    // open example data
    set scheme s1mono
    frame reset
    sysuse nlsw88, clear
    
    // do some initial preparation
    gen byte urban = c_city + smsa
    label define urban 2 "central city" ///
                       1 "suburban"     ///
                       0 "rural"
    label value urban urban
    label variable urban "urbanicity"
    
    // copy the data to another frame because the changes we are going to make now
    // are only relevant for a graph and we want to go back afterwards
    frame copy default tograph
    frame change tograph
    
    // make a dataset with mean wages for every type of place of residence
    collapse (mean) wage, by(urban)
    
    // determine how the mean wage is going to be displayed
    format wage %9.2f
    
    // make the graph
    twoway bar wage urban,                         ///
        barw(.8) xlabel(0/2, val) xtitle("")       ///
        ylabel(0(2)10, format(%9.0g) angle(0)) ||  ///
           scatter wage urban,                     ///
        msymbol(i) mlabel(wage) mlabpos(12) legend(off)
    
    // go back to the original data    
    frame change default
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	47.7 KB
ID:	1693031
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Maarten Buis gives excellent advice based on one guess about what you want. He is right that panel data usually need some reduction before a graph can be shown.

      This is based on another guess.

      If your data are those in #1 -- or like those in #1 -- this may help.

      If neither answert helps, then we need to know much more about your data.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str7 countries float remittance
      "Algeria"    2
      "Egypt"   20.4
      "Gaza"     2.3
      "Iran"     1.3
      "Jordan"   3.8
      "Lebanon"  7.5
      "Morocco"  6.7
      "Syria"    1.6
      "Tunisia"  2.3
      "Yemen"    3.4
      end
      label var remittance "remittance ???? (billion $)" 
      
      set scheme s1color 
      graph hbar (asis) remittance, over(countries, sort(1) descending) bar(1, fcolor(blue*0.2) lcolor(blue)) ysc(alt) blabel(total) ysc(r(. 22))
      Click image for larger version

Name:	remittance.png
Views:	1
Size:	20.1 KB
ID:	1693057

      Various tips here grow out of personal taste and experience as well as general graphical technique.

      1. A horizontal graph may be rightly preferred so that category (here country) names are easily readable.

      2. A default alphabetical order is rarely superior for this kind and size of dataset to sorting on magnitude.

      3. Almost any scheme is preferable to s2color.

      4. I find default colours even with my personal default to be too strong. A paler fill colour with a strong outline works well for me. Naturally, choose your own colour.

      5. Whenever a graph has table flavour, as it certainly does here, the unorthodox choice of horizontal axis at the top looks good to me. For more discussion, see https://www.stata-journal.com/articl...article=gr0053

      6. You may need to stretch the magnitude axis to accommodate bar labels comfortably. That often needs two passes over graphics.

      7. It is certainly good practice for data like these to explain the date or dates they refer to, as ???? alludes.

      8. It is arguable that the axis labels -- here 0(5)20 -- are redundant if bar labels are shown, but make your choice there.


      Comment


      • #4
        Thank you so much much, Nick Cox actually answered the question, that's exactly what I wanted, in addition, I wanted to add a vertical line of sample average serving as a benchmark or threshold on the X axis. How do I add that

        Comment


        • #5
          Thank you so much much, Nick Cox actually answered the question, that's exactly what I wanted, in addition, I wanted to add a vertical line of sample average serving as a benchmark or threshold on the X axis. How do I add that

          Comment


          • #6
            What you want is on the y axis, as with graph hbar the magnitude axis is considered the y axis -- as is explained in the help -- as that way you can flip between graph bar and graph hbar with just one letter change.

            Here the average across countries with quite different populations seems of doubtful use or interest, but you can show it easily enough:


            Code:
            su remittance, meanonly 
            
            graph hbar (asis) remittance, over(countries, sort(1) descending) bar(1, fcolor(blue*0.2) lcolor(blue)) ysc(alt) blabel(total) ysc(r(. 22)) yli(`r(mean)')

            Comment


            • #7
              Thanks but after the inclusion of the mean, the graph remain the same with the one gotten from the previous code

              Comment


              • #8
                This is what I get. There is a line at 5.13, which is the mean in billion dollars.


                Click image for larger version

Name:	remittance2.png
Views:	1
Size:	20.2 KB
ID:	1693165


                Comment


                • #9
                  Nick Cox , thanks, I have been able to plot it like yours. However, I am using multiple countries this time and the on the left side of the graph seems clustered, pls Is there a way I could adjust the axis so the countries will be clearly seen and the horizontal Axis can also be extended a bit to show the graph Fully..

                  Comment


                  • #10
                    What exactly is the new problem, as you show neither example data nor any such graph?

                    If you are plotting many more countries then more clutter is more or less inevitable. You can play with the plot size, the aspect ratio, or the label text size.

                    Comment


                    • #11
                      Another possibility is to split the graph into two panels.

                      Comment


                      • #12
                        Here's the New graph
                        Attached Files

                        Comment


                        • #13
                          We now see the problem and #10 and #11 suggested solutions.

                          Comment

                          Working...
                          X