Announcement

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

  • Changing Colors of Individual Bars marginsplot

    Hi Everyone,

    I'm having an oddly difficult time trying to graph something basic in Stata.

    I want to produce a graph from marginsplot that shows bars of k-colors. In the example below, there are four regions, and I would like each bar for NE, N Cntrl, South, and West to be its own color.

    Code:
    sysuse citytemp, clear 
    regress tempjan tempjuly i.region cooldd 
    margins, over(region) atmeans
    *below only uses the first color
    marginsplot, recast(bar) plotopts(color(red green blue orange))
    *below produces an error
    marginsplot, name(`dv', replace) recast(bar) plotopts(bar(1, color(green)) bar(2, color(red)) bar(3, color(orange)) bar(4, color(yellow)))
    I'm kinda at a loss - how do I change individual bar colors following marginsplot?

    Cheers,

    David.

  • #2
    You will have to compute the margins separately and then use coefplot and erepost from SSC. Similar question here: https://www.statalist.org/forums/for...insplot-to-bar.

    Code:
    sysuse citytemp, clear 
    estimates clear
    eststo m1: regress tempjan tempjuly i.region cooldd 
    margins, over(region) atmeans 
    *below only uses the first color
    marginsplot, recast(bar) saving(gr1, replace)
    
    levelsof region, local(regions)
    foreach region of local regions{
        est restore m1
        margins if region==`region', atmeans post
        mat b = e(b)
        mat colname b= "`=strtoname("`:lab (region) `region''")'"
        erepost b=b, rename
        est sto R`region'
    }
    coefplot R*, vert ytitle("Linear Prediction") xtitle("Census Region") ///
    recast(bar) nokey p1(bcolor(red)) p2(bcolor(green)) p3(bcolor(blue)) p4(bcolor(orange)) ///
    title(Adjusted Predictions with 95% CIs) saving(gr2, replace)
    
    gr combine gr1.gph gr2.gph
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	30.1 KB
ID:	1757932

    Comment


    • #3
      Hi Andrew,

      Holy smokes, I feel less bad about not being able to figure it out. I thought it would have been in-built functionality. Thanks so much for taking the time to explain it to me.

      Cheers,

      David.

      Comment


      • #4
        There is an easier way using the graph editor. Below, I specify 70% opacity ("%70"), which you can change.

        Code:
        sysuse citytemp, clear
        estimates clear
        eststo m1: regress tempjan tempjuly i.region cooldd
        margins, over(region) atmeans
        marginsplot, recast(bar)
        
        local colors red blue orange yellow
        forval j= 1/`=wordcount("`colors'")'{
            gr_edit .plotregion1.plot1.EditCustomStyle , j(`j') style(area(shadestyle(color(`=word("`colors'", `j')'%70))))
        }
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	29.9 KB
ID:	1757977

        Last edited by Andrew Musau; 06 Jul 2024, 09:25.

        Comment

        Working...
        X