Announcement

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

  • Side-by-side bars in marginsplot using recast(bar) with Stata's auto dataset

    Hello Statalist community,

    I'm encountering an issue with generating a bar chart using the marginsplot command after recasting it to a bar chart (recast(bar)). Specifically, I'm trying to display the bars for two different treatment groups (e.g., foreign in the auto dataset) side by side at specific values of another variable (e.g., mpg), but the bars appear on top of one another rather than side by side.

    To illustrate the problem, consider the following example using Stata's built-in auto dataset:

    Code:
    sysuse auto, clear
    reg price i.foreign##c.mpg
    margins foreign, at(mpg=(20 30 40))
    marginsplot, recast(bar)
    In this case, I would like the bars for foreign and domestic cars to be displayed next to each other for each value of mpg (20, 30, 40). However, the bars currently overlap, and I haven't been able to control the spacing between them effectively.

    I understand that the bargap() option within plotopts() might be a potential solution to adjust the gap between the bars. I've tried using the following command:

    Code:
    marginsplot, recast(bar) plotopts(barw(0.4) bargap(20))
    While this does adjust the spacing, I'm still having trouble achieving the desired result of side-by-side bars. If anyone has experience with this issue or suggestions on how to properly configure the bargap() or other relevant options, I would greatly appreciate your help.

    Thanks!
    CJ

  • #2
    Code:
    sysuse auto, clear
    reg price i.foreign##c.mpg
    margins foreign, at(mpg=(20 30 40))
    marginsplot, recast(bar) bydim(mpg)
    Res.:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	30.0 KB
ID:	1762299

    Comment


    • #3
      Andrew Musau nicely cracked the main problem.

      Here are some suggested refinements for what is wanted.

      Code:
      sysuse auto, clear
      reg price i.foreign##c.mpg
      margins foreign, at(mpg=(20 30 40))
      marginsplot, recast(bar) bydim(mpg) byopts(row(1)) plotopts(barw(0.8) fcolor(stc1*0.2) yla(-4000(2000)8000) xla(, tlc(none)))


      Click image for larger version

Name:	andrew2.png
Views:	1
Size:	36.5 KB
ID:	1762302

      Comment


      • #4
        This is really great - thank you! Is there any way to assign different colors to the bars? I was trying to do different colors and gradients for the groups x mpg combinations, but failing, with this modification of Nick's code:

        Code:
        plotopts(barw(0.8) /// fcolor("ltblue" "skyblue" "midblue" "dkblue") /// Treated Group Colors
        lcolor("ltblue" "skyblue" "midblue" "dkblue") /// Set lcolor for edges
        fcolor("orange" "dkorange" "red" "cranberry") /// Control Group Colors
        lcolor("orange" "dkorange" "red" "cranberry") /// Set lcolor for edges)
        But it is just coloring all of the bars orange. (Note also that I am using the white_tableau scheme).

        Thanks!

        Comment


        • #5
          You have previously asked this exact question. Please refer to the thread for the response provided there: https://www.statalist.org/forums/for...insplot-to-bar. Subsequently, I have demonstrated how to use the graph editor to achieve this: https://www.statalist.org/forums/for...rs-marginsplot.

          Comment


          • #6
            Ah apologies for the redundancy!

            Comment

            Working...
            X