Announcement

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

  • Change order of x-axis categories in marginsplot

    Hello,

    After the combo margins-marginsplot, I would like to change the order of the categories of my x-axis.
    See example below: suppose I would like to plot category 3 first, then 2, 5, 4 and 1 ... is there a way to do this?

    Code:
    sysuse auto
    reg price mpg i.rep78
    margins rep78
    marginsplot, plotopts(connect(none))


    Click image for larger version

Name:	Graph.png
Views:	1
Size:	41.1 KB
ID:	1586733

    Thank you,
    Mike

  • #2
    If the variable is ordinal, like rep78 in the auto dataset, then it makes no sense to do this. If it is nominal, then recode it before running the regression and margins.

    Comment


    • #3
      Thank you.
      I agree that rep78 is not the best example: the variable I really want to use is nominal ... 'recode' can definitely do the job, but I think I have to be careful and double check value labels and base categories in regressions elsewhere in my do-file ...
      I was wondering whether it could be done in the margins or marginsplot command itself

      Comment


      • #4
        As far as I know, there is no such flexibility built into marginsplot (I may be wrong). A difficult way is to save the margins and recreate the graph using twoway, as I do in #2 here. A better suggestion is to install coefplot from SSC and recreate the marginsplot, exploiting its -order()- option. Let us do this using your example in #1.

        Code:
        sysuse auto, clear
        reg price mpg i.rep78
        margins rep78, post
        *ssc install coefplot
        coefplot, vert ytitle("Linear Prediction")  xlab(1 "3" 2 "2" 3 "5" 4 "4" 5 "1") ///
        ciopts(recast(rcap)) xtitle("Repair Record 1978") ///
        title("Predictive Margins of rep78 with 95% CIs", size(medlarge)) ///
        order(3.rep78 2.rep78 5.rep78 4.rep78 1.rep78)
        Res.:
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	60.1 KB
ID:	1586794

        Last edited by Andrew Musau; 20 Dec 2020, 10:52.

        Comment


        • #5
          Andrew Musau Thank you for this nice solution using coefplot!

          Mike

          Comment

          Working...
          X