Announcement

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

  • Stata Twoway bar plots - different bar gaps

    Hello!

    Can someone help me figure out how can I have a twoway bar plot with different bar gaps, i.e. in the below example I would like to have the first bar and the second bar and the third bar and the forth bar with no gap in between and I would like to have a gap between the second and the third bar.

    Click image for larger version

Name:	Screenshot 2019-06-12 at 09.58.18.png
Views:	2
Size:	122.2 KB
ID:	1502751


    This is my code:
    Code:
    twoway (bar mean sample if sample==1, bcolor(maroon) barw(0.85)) (bar mean sample if sample==2, bcolor(navy) barw(0.85) bargap(-100)) (bar mean sample if sample==3, bcolor (maroon) barw(0.85) ) (bar mean sample if sample==4, bcolor (navy) barw(0.85)) (rcap hi_sem low_sem  sample if sample==1 | sample==2 | sample==3 | sample==4, lcolor(black)), legend( row(1) order(1 "Left side" 2 "Right side" 3 "Left side" 4 "Right side")) graphregion (color(white)) plotregion(margin(b=0)) xtitle("") xlabel( 1.5 "Starting Left" 3.5 "Starting Right", noticks) ylabel (0(200)1200)
    Thank you in advance.

  • #2
    No data example here (FAQ Advice #12), but this shows some technique. I have left out the rcap calls as secondary to your question.

    Code:
    clear 
    input sample mean 
    1   500
    2   600
    3   700 
    4   800  
    end 
    
    gen sample2 = cond(sample <= 2, sample, sample + 0.5) 
    
    twoway (bar mean sample2 if sample==1, bcolor(maroon) barw(0.85)) ///
    (bar mean sample2 if sample==2, bcolor(navy) barw(0.85) bargap(-100)) ///
    (bar mean sample2 if sample==3, bcolor (maroon) barw(0.85) )          ///
    (bar mean sample2 if sample==4, bcolor (navy) barw(0.85)) ///
    , legend( row(1) order(1 "Left side" 2 "Right side")) ///
    graphregion(color(white)) plotregion(margin(b=0)) xtitle("") ///
    xlabel( 1.5 "Starting Left" 4 "Starting Right", noticks) ylabel(0(200)1200)

    Click image for larger version

Name:	leftright.png
Views:	1
Size:	19.2 KB
ID:	1502776

    Comment


    • #3
      Thank you so much for your help!
      Cheers,
      Gonçalo

      Comment


      • #4
        Hi, I've been trying to change the distance between both pair of bars by changing the value of bargap() but I don't notice any variation. Is there anything that I'm not getting?

        Comment


        • #5
          bargap() is an option of graph bar and nothing to do with twoway bar, the topic of this thread. Either way, to give helpful advice, we really need a reproducible example and a sight of the exact code you tried.

          Comment


          • #6
            Actually, I tried the same code that you created (with the same example data that you generated). I just wanted to change the distance of bars 1 and 2 with respect to bars 3 and 4. The question can be related to this post which I did a week ago. I just found that this post was more related to the one I did.

            https://www.statalist.org/forums/for...ont-of-a-chart

            Comment


            • #7
              In #2

              Code:
               
               gen sample2 = cond(sample <= 2, sample, sample + 0.5) 
              maps sample that is 1 2 3 4 to 1 2 3.5 4.5 so that smaller gaps are 1 and the larger gap is 1.5. So, just play with variations on 0.5.

              Comment

              Working...
              X