Announcement

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

  • Problem with the aesthetics of a bar graph

    Hello there Stata users ;

    I do have this data at hand:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str47 guerre long effectifsdamricainsenserviceoudp
    "Guerre de l'Indépendance Americaine (1775-1783)"   217000
    "Guerre Anglo-Américaine de 1812"                   286730
    "Guerres Indiennes (1817-1898)"                     106000
    "Guerre Américano-Mexicaine (1846-1848)"             78718
    "Guerre de Sécession (1861-1865)"                  3263363
    "Guerre Hispano-Américaine (1898-1902)"             306760
    "Guerre Mondiale I (1917-1918)"                    4734991
    "Guerre Mondiale II (1941 –1945)"                 16112566
    "Guerre de Corée (1950-1953)"                      1789000
    "Guerre du Vietnam (1964-1975)"                    3403000
    "Guerre du Golfe Persique (1990-1991)"              694550
    "Irak / Afghanistan"                               2500000
    end
    As you can see, it is about representing that data in the form of a bar graph. When using the "graph hbar (sum) effectifsdamricainsenserviceoudp , over( guerre )", I get a graph with the Y axis categories pushed to the side, which means I can't show them and see them, they are offset to the side.

    Any help with this please? Thanks

  • #2
    It would help for you to upload a picture of your graph. The issue may also be scheme-specific. Could you show us the results of
    Code:
    query graphics

    Comment


    • #3
      The strings of "guerre" are very long, shorten them or display them with smaller fontsize.

      You should plot not the sum of the values, but use "(asis)", instead. By the way: The variable name is horrible, you should use a shorter name and use a variable label, instead.

      This works for me:
      Code:
      rename effect* inservice
      lab var inservice "Effectifs d'Americains en service ou DP"
      format %10.0fc inservice
      
      graph hbar (asis) inservice, over(guerre, lab(labsize(small))) ///
            ylabel(#8, angle(forty_five) format(%10.0fc))
      
      // Numeric variable "guerre_n" instead of string variable:
      cap which elabel           // see whether -elabel- is installed
      if _rc ssc install elabel  // install -elabel- if necessary
      
      elabel define guerre_n = encode(guerre), nosort
      encode guerre, gen(guerre_n) label(guerre_n)
            
      // Problem with labeled values, however: The label is restricted to 32 characters:
      graph hbar (asis) inservice, over(guerre_n, lab(labsize(small))) ///
            ylabel(#8, angle(forty_five) format(%10.0fc))
      Last edited by Dirk Enzmann; 03 Nov 2024, 10:19.

      Comment


      • #4
        I agree with Hemanshu Kumar In addition, there are some small errors and inconsistencies in your data, while I suggest that repetition of "Guerre" (war) or "Guerres" (wars) is not needed.


        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str48 guerre long effectifsdamricainsenserviceoudp
        "Guerre de l'Indépendance Americaine (1775-1783)"   217000
        "Guerre Anglo-Américaine de 1812"                   286730
        "Guerres Indiennes (1817-1898)"                     106000
        "Guerre Américano-Mexicaine (1846-1848)"             78718
        "Guerre de Sécession (1861-1865)"                  3263363
        "Guerre Hispano-Américaine (1898-1902)"             306760
        "Guerre Mondiale I (1917-1918)"                    4734991
        "Guerre Mondiale II (1941 –1945)"                 16112566
        "Guerre de Corée (1950-1953)"                      1789000
        "Guerre du Vietnam (1964-1975)"                    3403000
        "Guerre du Golfe Persique (1990-1991)"              694550
        "Irak / Afghanistan"                               2500000
        end
        
        set scheme stcolor 
        
        gen id = _n
        
        graph hbar (asis) effect, over(guerre, sort(id)) name(HB1, replace)
        
        clonevar axis=guerre
        replace axis = subinstr(axis, "Guerres", "", .)
        replace axis = subinstr(axis, "Guerre", "", .)
        replace axis = subinstr(axis, "de ", "", 1)
        replace axi s = subinstr(axis, "du ", "", 1)
        replace axis = subinstr(axis, " –", "-", 1)
        
        graph hbar (asis) effect, over(axis, sort(id)) name(HB2, replace) ysc(alt) yla(0 5e6 "5 m" 1e7 "10 m" 15e6 "15 m")
        Click image for larger version

Name:	guerres.png
Views:	1
Size:	59.1 KB
ID:	1766885

        Comment


        • #5
          Thanks Hemanshu Kumar and Dirk Enzmann and Nick Cox, the instructions helped me get to my goal!

          Comment

          Working...
          X