Announcement

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

  • Using consistent color for categorical var across marginsplots

    Hi all,

    I am currently using marginsplot to visualize the effects of race in a few different models. Each model excludes one race from the analysis. Because the margins include a different subset of race in each model, marginsplot assigns the colors of the markers/lines inconsistently.

    For ease of comprehension and visual clarity, I would like for each racial category to show as the same color in each plot (eg NH White is red purple even though in some plots NH Black gets assigned a color first). Is there a way for me to specify before the marginsplots are created that I would like the assignment of the colors to be consistent? Worst case I suppose I could manually specify colors in each marginsplot but I'm hoping you all know of a more efficient option!

    Thanks,
    Eva


    Code:
    reg politics i.race##c.age if race!=1
    quietly: margins i.race, at(age=(0(20)80))
    marginsplot, name(whiteout, replace)
    
    reg politics i.race##c.age if race!=2
    quietly: margins i.race, at(age=(0(20)80))
    marginsplot, name(blackout, replace)
    
    graph combine whiteout blackout
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(race age politics)
    3 46 7
    4 37 4
    1 40 3
    4 41 6
    5 72 4
    1 71 3
    1 37 6
    1 45 6
    1 70 4
    3 43 2
    3 37 1
    1 55 2
    1 30 7
    4 38 2
    2 41 3
    3 66 5
    1 54 3
    1 55 1
    1 62 1
    1 80 2
    3 31 7
    3 80 1
    1 24 3
    1 55 7
    3 59 2
    end
    label values race racelab
    label def racelab 1 "NH White", modify
    label def racelab 2 "NH Black", modify
    label def racelab 3 "Hispanic", modify
    label def racelab 4 "AA/NH/PI", modify
    label def racelab 5 "NA/AN", modify
    label values politics polilab
    label def polilab 1 "Strong Democrat", modify
    label def polilab 2 "Not very strong Democrat", modify
    label def polilab 3 "Independent-Democrat", modify
    label def polilab 4 "Independent", modify
    label def polilab 5 "Independent-Republican", modify
    label def polilab 6 "Not very strong Republican", modify
    label def polilab 7 "Strong Republican", modify

  • #2
    This will be easiest if you save the estimates of your margins commands into separate data files and then build the graph from there. You will have much more control over the appearance of your graph. The key is to utilize the undocumented command
    Code:
    _marg_save
    A nice example can be found from an old Statalist thread.

    Comment


    • #3
      Originally posted by Eva Warren View Post

      For ease of comprehension and visual clarity, I would like for each racial category to show as the same color in each plot (eg NH White is red purple even though in some plots NH Black gets assigned a color first). Is there a way for me to specify before the marginsplots are created that I would like the assignment of the colors to be consistent?
      If the coding is consistent, then the answer is yes. You can insist that factor variables keep all categories. Using your example:

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(race age politics)
      3 46 7
      4 37 4
      1 40 3
      4 41 6
      5 72 4
      1 71 3
      1 37 6
      1 45 6
      1 70 4
      3 43 2
      3 37 1
      1 55 2
      1 30 7
      4 38 2
      2 41 3
      3 66 5
      1 54 3
      1 55 1
      1 62 1
      1 80 2
      3 31 7
      3 80 1
      1 24 3
      1 55 7
      3 59 2
      end
      label values race racelab
      label def racelab 1 "NH White", modify
      label def racelab 2 "NH Black", modify
      label def racelab 3 "Hispanic", modify
      label def racelab 4 "AA/NH/PI", modify
      label def racelab 5 "NA/AN", modify
      label values politics polilab
      label def polilab 1 "Strong Democrat", modify
      label def polilab 2 "Not very strong Democrat", modify
      label def polilab 3 "Independent-Democrat", modify
      label def polilab 4 "Independent", modify
      label def polilab 5 "Independent-Republican", modify
      label def polilab 6 "Not very strong Republican", modify
      label def polilab 7 "Strong Republican", modify
      
      *RACE VARIES FROM 1-5. DEFINE COLORS:
      local colors "plot1opts(color(red)) plot2opts(color(blue)) plot3opts(color(green)) plot4opts(color(orange)) plot5opts(color(violet))"
      
      reg politics i.race##c.age if race!=1
      *ASK FACTOR VARIABLES TO KEEP ALL CATEGORIES 
      quietly: margins i(1/5).race, at(age=(0(20)80))
      *MARGINSPLOT WITH DEFINED COLORS
      set scheme s1color
      marginsplot, name(whiteout, replace) `colors'
      
      reg politics i.race##c.age if race!=2
      quietly: margins i(1/5).race, at(age=(0(20)80))
      marginsplot, name(blackout, replace) `colors'
      
      graph combine whiteout blackout
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	84.7 KB
ID:	1688499

      Comment


      • #4
        Andrew Musau This is perfect! Thank you!

        Comment

        Working...
        X