Announcement

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

  • How to put bars side by side on marginsplot of mean in STATA

    Hello everyone,

    First of all I would like to express my gratitude to the members of this forum for supporting me in different ways to progress in my PhD project.

    ​​​​​​I would appreciate it if you kindly help me a bit more.

    I was running mean of one of my dependent variable PMH by two categorical variables (GEO_PRV and L_Stress; five-category geography and two-category life stress). Once I run the stata command for mean and than run marginsplot and select recast(bar) option, SATA creates bar graps. However, the bar life stress bar apprears on the high life stress bar. It is the same for each of the five regions. I have been looking for command to set bars side by side, but cannot.

    If you know how I can regirnaize the graph and set them side by side, like low stree high stress.

    Thank you,

    Iqbal Chowdhury

  • #2
    It is difficult to envisage or replicate your problem since you have not presented any data example or commands you used (See FAQ section about posting data example using -dataex-). Here is an example of bars set side by side using -auto- dataset, but I am not sure if that is what you wanted. Please next time consider providing data example and the exact commands you used:

    Code:
    sysuse auto, clear
    
    mean mpg, over(foreign rep78)
    marginsplot, xdim(rep78) recast(bar) plotopts(barw(.5) )


    [code]




    Roman

    Comment


    • #3
      Roman Mostazir

      Thank you so much for your reply. I used this option, and there were no changes in the graph. Then, I used the following syntax.

      Code:
      mean DEPRES, over (FIC2 SSS_CAT)
      marginsplot, legend(pos(6) col(2) region(lcolor(black)))  recast(bar) bydim(SSS_CAT)  byopts(row(1)) plotopts(barw(0.7) bargap(-100) fcolor("ltblue") xla(, tlc(none)))

      It separates the bars. However, the color of all bars is the same. I cannot change it. Also, I cannot reduce the gap between the two bars. I used the bargap(-100) option. It doesn't work. Can you please help?
      Click image for larger version

Name:	Picture1.png
Views:	1
Size:	74.0 KB
ID:	1766306

      Comment


      • #4
        Why do the bars start at 12? If you want a comparison with zero, the base should be zero; if that's irrelevant, I suggest that you use a marker not a bar to show the mean.

        (I am no kind of expert on marginsplot and so sorry, I can't assist with your precise question.)

        Comment


        • #5
          I am sorry, I don't know how to control the individual bar looks with -bydimensions- after -margins-. I rarely used the bar options with -margins-. The tricky bit is that the -bydimension- treats the whole plot as one plot thus the controls of the individual bars gets trickier. Andrew Musau might be able to help. One way of controlling the bar looks could be avoiding the -bydimension- (see below an example without the -bydimension-). You have to play around with it to fine tune as I am in tight call for time and didn't refine. The bar gap should be reduced increasing the -plotoptions(barw(#))

          Again, you haven't posted any data example! Please read the FAQ section.

          Code:
          sysuse auto, clear
          
          mean mpg, over(foreign rep78)
          
          #delimit ;
          marginsplot, recast(bar) xdim(rep78)
          plot1opts(bcol(red)) ci1opts(bcol(red))
          plot2opts(bcol(blue)) ci2opts(bcol(blue))
          legend(pos(6) row(1))
          plotopts(barw(.9))
          ;
          #delimit cr
          Roman

          Comment


          • #6
            Originally posted by Iqbal Chowdhury View Post
            However, the color of all bars is the same. I cannot change it. Also, I cannot reduce the gap between the two bars.
            To eliminate the gap between bars, you need a bar width of 1. As illustrated in #4, the easiest way to define colors for each bar is to use the graph editor.

            Code:
            sysuse auto, clear
            
            mean mpg if rep78>=3, over(foreign rep78)
            
            marginsplot, legend(pos(6) col(2) region(lcolor(black)))  recast(bar) ysc(r(0 .)) bydim(rep78)  byopts(row(1)) plotopts(barw(1) fcolor("ltblue") xla(, tlc(none)))
            
            local bars 6
            forval j= 1/`bars'{
                gr_edit .plotregion1.plotregion1[`j'].plot1.EditCustomStyle , j(1) style(area(shadestyle(color(red%50))))
                gr_edit .plotregion1.plotregion1[`j'].plot1.EditCustomStyle , j(1) style(area(linestyle(color(red%50))))
                gr_edit .plotregion1.plotregion1[`j'].plot1.EditCustomStyle , j(2) style(area(shadestyle(color(blue%50))))
                gr_edit .plotregion1.plotregion1[`j'].plot1.EditCustomStyle , j(2) style(area(linestyle(color(blue%50))))
            }
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	30.7 KB
ID:	1766365

            Last edited by Andrew Musau; 24 Oct 2024, 18:33.

            Comment


            • #7
              Hello, Andrew Musau, Nick Cox, and Roman Mostazir,

              Thank you so much for your support. It was frustrating as I tried the codes of Mr. Mostazir and Mr. Musau the whole day. However, it did not work out. Probably, it was an issue in the research data center problem. With a broken heart, I tried them on my laptop with my dataset. Bingo! It worked. I am going to try the code again inside the RDC. Hopefully, it will work well now. I am very grateful to you all.

              Comment

              Working...
              X