Announcement

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

  • Controlling the colors of specific bars when recasting marginsplot to bar?

    {EDIT: To not accidentally attach a million versions of the graph, like a dummy}

    Hi all,

    Been trying a bunch of different solutions to this, but I have a marginsplot that I have recast to a bar graph to show the predictive margins at two different points in the distribution for 3 different groups. My basic problem is that I would like to highlight the fact that the bars are for three separate groups using different colors for each set of 2 bars. The below example (slightly modified from this very helpful post) doesn't have the same sets of groups, but is an easy way to show the basic problem

    Code:
    sysuse auto, clear
    reg price c.mpg##foreign
    margins, at(mpg=(20 30 40) foreign=(0 1)) vsquish
    marginsplot,  xdim(mpg foreign) recast(bar) plotopts(barw(.8)) xlabel(,labsize(vsmall))
    Which produces this graph:




    Now imagine that I want to color each of those bars a different color. I tried this suggestion:

    Code:
    marginsplot,  xdim(mpg foreign) recast(bar) plotopts(barw(.8)) xlabel(,labsize(vsmall)) ///
    plot1opts(fcolor(cranberry)) plot2opts(fcolor(midblue))
    But alas - I get the same error as the person replying on that thread: "option plot2opts() not allowed". Interestngly, plot1opts() alone does not throw an error. I think this + the help file suggests to me that plot#opts is for controlling multiple plots, not multiple plot elements within a given plot. So I'm stuck, but have tried a shot in the dark with things such as:

    Code:
    marginsplot,  xdim(mpg foreign) recast(bar) plotopts(barw(.8)) xlabel(,labsize(vsmall)) ///
    plotopts(barw(.8)) marker(1, mfcolor(cranberry)) marker(2, mfcolor(cranberry)) marker(3, mfcolor(midblue))
    But to no avail. Any suggestions?
    Attached Files
    Last edited by CJ Libassi; 07 Sep 2021, 14:39.

  • #2
    Just because you see a few bars does not mean that you have several plots. Stata is right telling you that

    "option plot2opts() not allowed"
    as you only have one plot. You have to jump through hoops to crack this one. Below I use coefplot and estout from SSC. You can improve on the final presentation.

    Code:
    cap prog drop repb
    program repb, eclass
    erepost b=b, rename
    end
    
    sysuse auto, clear
    reg price c.mpg##foreign 
    eststo all
    local i 1
    forval mpg= 20(10)40{
        forval foreign=0/1{
            est restore all
            margins, at(mpg=(`mpg') foreign=(`foreign')) vsquish post
            mat b=e(b)
            mat colname b= _at`i'
            repb
            eststo m`i'
            local ++i
         }
    }
    set scheme s1color
    coefplot m*,  vert ytitle("Linear Prediction") recast(bar) nokey
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	20.8 KB
ID:	1626667

    Comment

    Working...
    X