Announcement

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

  • bar graph position

    Hi
    Anybody can help me with how to fix the graph position. I used the below code, but please see the result of the graph in the attachment


    graph hbar (mean) group_0 group_1 if h1_source_==1, over(catv, relabel(1`""Don't receive" "information""') sort(1) descending) ///
    ylabel(0(10)100) ytitle("Percentage") ///
    blabel(bar, format(%2.1f) size(2)) ///
    legend(label(1 "Control") label(2 "Treatment") pos(6)) ///
    title("Reliable source information for COVID-19", span pos(14) size(4) placement(top))
    Attached Files
    Last edited by Thein Zaw; 20 Nov 2021, 00:26.

  • #2
    See the recent thread https://www.statalist.org/forums/for...-in-graph-hbar

    Showing us the value labels as text would help in giving advice.

    Code:
    preserve 
    collapse (mean) group_0 group_1 if h1_source_==1, by(catv) 
    dataex 
    restore
    would allow you to give us a reproducible example.

    Comment


    • #3
      Thanks, Nick Cox,
      I looked the recent thread post, and make adjustments in the script using relabel to make wrap the text if the text is long, and also axis(outergap(*10) as below
      Code:
      graph hbar (mean) group_0 group_1 if h4_protect_==1, /// 
                over(h4var,  sort(1) descending label(labsize(2)) ///
                relabel(15 `""Taking bath once come" "from outside""') ///
                axis(outergap(*10))) ///
                ylabel(0(10)70) ytitle("Percentage",size(3)) ///
                blabel(bar, format(%2.1f) size(2)) ///
                legend(label(1 "Control") label(2 "Treatment") pos(6)) ///
                title("Preventive measures against Covid-19", span pos(14) size(3)  placement(top))
      The result of labelling is not what I want as below, in the attachment; I would like to get all label text is present, but some are still hiding.


      Here is that dataset,
      ----------------------- copy starting from the next line -----------------------
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte h4var float(group_0 group_1)
       1 52.79045 47.20955
       2  51.5625  48.4375
       3 48.90427 51.09573
       4 57.98319 42.01681
       5 52.66641 47.33359
       6 47.41379 52.58621
       7 52.12598 47.87402
       9 56.98925 43.01075
      10 45.45454 54.54546
      14 52.63158 47.36842
      15 58.05243 41.94757
      16 54.44444 45.55556
      end
      label values h4var h4var
      label def h4var 1 "Wearing a mask over nose and mouth", modify
      label def h4var 2 "Staying 6 feet away from  others", modify
      label def h4var 3 "Avoiding crowds", modify
      label def h4var 4 "Avoiding poorly ventilated spaces", modify
      label def h4var 5 "Washing the hands often", modify
      label def h4var 6 "Covering coughs and sneezes", modify
      label def h4var 7 "Cleaning and disinfecting", modify
      label def h4var 9 "Not allowing visitors at home", modify
      label def h4var 10 "Eating well balanced diet", modify
      label def h4var 14 "Changing clothes once came from outside", modify
      label def h4var 15 "Taking bath once come from outside", modify
      label def h4var 16 "Worning surgical head cap", modify
      ------------------ copy up to and including the previous line ------------------


      Attached Files

      Comment


      • #4
        Thanks for the data example. I changed the question!

        1. It is evident that group_0 and group_1 are just complements in 100. So, choose one.

        2. Choice of a bar chart implies that it's comparisons with zero that are crucial. I doubt it: it's comparisons of values with each other that seem more interesting.

        3. I played with graph dot instead but couldn't find easy work-arounds for an apparent limit of 32 characters, for some reason.

        Here is another approach.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input byte h4var float(group_0 group_1)
         1 52.79045 47.20955
         2  51.5625  48.4375
         3 48.90427 51.09573
         4 57.98319 42.01681
         5 52.66641 47.33359
         6 47.41379 52.58621
         7 52.12598 47.87402
         9 56.98925 43.01075
        10 45.45454 54.54546
        14 52.63158 47.36842
        15 58.05243 41.94757
        16 54.44444 45.55556
        end
        label values h4var h4var
        label def h4var 1 "Wearing a mask over nose and mouth", modify
        label def h4var 2 "Staying 6 feet away from  others", modify
        label def h4var 3 "Avoiding crowds", modify
        label def h4var 4 "Avoiding poorly ventilated spaces", modify
        label def h4var 5 "Washing the hands often", modify
        label def h4var 6 "Covering coughs and sneezes", modify
        label def h4var 7 "Cleaning and disinfecting", modify
        label def h4var 9 "Not allowing visitors at home", modify
        label def h4var 10 "Eating well balanced diet", modify
        label def h4var 14 "Changing clothes once came from outside", modify
        label def h4var 15 "Taking bath once come from outside", modify
        // note: edit "Worning" to "Wearing"
        label def h4var 16 "Wearing surgical head cap", modify
        
        * install from Stata Journal 
        myaxis axis=h4var, sort(mean group_0)
        
        set scheme s1color 
        
        label var group_0 "Treatment"
        
        gen toshow = strofreal(group_0, "%2.1f") 
        
        scatter axis group_0, xsc(alt) yla(1/12, grid valuelabel ang(h) noticks) name(different, replace) ytitle("") mla(toshow)
        Click image for larger version

Name:	treatment.png
Views:	1
Size:	49.3 KB
ID:	1637351


        Naturally these are just suggestions. Note a typo fix.

        For myaxis see https://www.stata-journal.com/articl...article=st0654 or https://www.statalist.org/forums/for...e-or-graph-use

        For the idea that for graphs with table flavour the x axis at the top can work well see https://www.stata-journal.com/articl...article=gr0053

        Comment


        • #5
          Only a typo fix for #3 using -splitvallabels- (from SSC).

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input byte h4var float(group_0 group_1)
           1 52.79045 47.20955
           2  51.5625  48.4375
           3 48.90427 51.09573
           4 57.98319 42.01681
           5 52.66641 47.33359
           6 47.41379 52.58621
           7 52.12598 47.87402
           9 56.98925 43.01075
          10 45.45454 54.54546
          14 52.63158 47.36842
          15 58.05243 41.94757
          16 54.44444 45.55556
          end
          label def h4var 1 "Wearing a mask over nose and mouth", modify
          label def h4var 2 "Staying 6 feet away from others", modify
          label def h4var 3 "Avoiding crowds", modify
          label def h4var 4 "Avoiding poorly ventilated spaces", modify
          label def h4var 5 "Washing the hands often", modify
          label def h4var 6 "Covering coughs and sneezes", modify
          label def h4var 7 "Cleaning and disinfecting", modify
          label def h4var 9 "Not allowing visitors at home", modify
          label def h4var 10 "Eating well balanced diet", modify
          label def h4var 14 "Changing clothes once came from outside", modify
          label def h4var 15 "Taking bath once come from outside", modify
          label def h4var 16 "Worning surgical head cap", modify
          label values h4var h4var
          
          splitvallabels h4var, length(40) r
          
          graph hbar (mean) group_0 group_1, ///
                    over(h4var, relabel(`r(relabel)') sort(1) descending) ///
                    ytitle("Percentage") ///
                    ylabel(0(10)65) blabel(bar, format(%2.1f)) ///
                    legend(label(1 "Control") label(2 "Treatment") span) ///
                    title("Preventive measures against Covid-19", span placement(top))
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	269.7 KB
ID:	1637360

          Comment

          Working...
          X