Announcement

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

  • In by cat_var bar graph, how to show var names only on the first graph y-axis

    Dear Statalists,

    Could you help me how i can show variable names of the bars only on the first y-axis of the categorical vars. As you can see from the figure below in my case it is hows on the y-axis of every category. How can I also position the variable names outside to the left of the bars? Here is my example:

    Code:
    sysuse auto
     graph hbar mpg displacement, by(foreign, total row(1)) ascategory blabel(bar) yvar(relabel(1 "`: var label mpg'" 2 "`: var label displacement'"))


    Click image for larger version

Name:	Graph.jpg
Views:	1
Size:	115.2 KB
ID:	1636052





  • #2
    Perhaps that could be fixed going further down that road, but I tried just custom code for tabplot from the Stata Journal.


    Code:
    sysuse auto, clear 
    
     graph hbar mpg displacement, by(foreign, total row(1)) ascategory blabel(bar) yvar(relabel(1 "`: var label mpg'" 2 "`: var label displacement'"))
    
     
     gen mean = . 
     
     gen row = ""
     egen col = seq(), to(3) 
     
     local i = 0 
     
     
     foreach v of var mpg displacement { 
     forval g = 0/2 { 
         local ++i 
         replace row = "`: var label `v''" in `i'
         if inlist(`g', 0, 1) { 
             summarize `v' if foreign == `g', meanonly 
             label def col `i' "`: label (foreign) `g''", modify 
         }     
         else { 
             summarize `v', meanonly 
             label def col `i' "Total", modify 
         } 
         
         replace mean = r(mean) in `i' 
    } 
    } 
    
    label val col col 
    tabplot row col [iw=mean],  xtitle("") ytitle("") bfcolor(none) showval(format(%2.1f))
    Click image for larger version

Name:	awkward.png
Views:	1
Size:	17.2 KB
ID:	1636068

    Comment


    • #3
      The following based on your example combines too many elements, but the main ideas are drawn from this thread https://www.statalist.org/forums/for...nly-one-legend and this thread https://www.statalist.org/forums/for...s-in-graph-box. You want to have your variable in long format so that you can use the -over()- option and thus manipulate the values and labels (with the intention of using the -nofill- option). You assign invisible characters to categories on the right-hand side categories. The total group is obtained by expanding the data. Then, you need an empty category to force graph bar to maintain equal bar widths across categories.



      Code:
      sysuse auto, clear
      rename (mpg disp) var=
      reshape long var, i(make) j(desc) string
      encode desc, gen(which)
      replace which=3 if which==1 & foreign
      replace which=4 if which==2 & foreign
      label def `: val lab which' 3 "`:di "`=uchar(28)'"'", add
      label def `: val lab which' 4 "`:di "`=uchar(28)'"'", add
      expand 2, g(new)
      replace which= 5 if new & inlist(which, 1,3)
      replace which= 6 if new & inlist(which, 2,4)
      replace foreign =2 if new
      label def `: val lab foreign' 2 "Total", add
      label def `: val lab which' 5 "`:di "`=uchar(28)'"'", add
      label def `: val lab which' 6 "`:di "`=uchar(28)'"'", add
      expand 2, gen(final)
      replace which=7 if final
      replace var=. if which==7
      graph hbar var, over(which) by(foreign, row(1) note("")) ascategory blabel(bar) nofill  ytitle("")
      Res.:
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	31.3 KB
ID:	1636133


      Last edited by Andrew Musau; 11 Nov 2021, 11:39.

      Comment


      • #4
        Dear Andrew and Nick,

        Your codes are so helpful thank you so much indeed for your time!!!

        Comment


        • #5
          Dears @Andrew Musau, @Nick Cox and all,

          I have one more related question.

          How can I add 95% confidence interval lines for the means of the above bar graph?


          Comment


          • #6
            You need to calculate the means and confidence intervals before you can plot them. See https://www.stata-journal.com/articl...article=gr0045 for some suggestions.

            Comment

            Working...
            X