Announcement

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

  • mycolours package available on SSC

    Thanks as always to Kit Baum who maintains SSC, a new package mycolours is available from SSC. The package consists of a single command mycolours and its help file, although the American spelling mycolors can also be used in calling up the command or its help file if you prefer, or are accustomed to, that spelling. (Nevertheless I draw the line at maintaining two almost identical help files.)

    This is a small deal but in downplaying the command I want to give a small push to the idea behind the command as of more importance than the particular implementation.

    Users of graphics commands know that there are two extreme but compatible ways to tweak details in your graphs. You can specify options on the fly as needed for a particular graph -- or you can use a particular graph scheme that codifies many (or even all) of your own style preferences. Oddly enough when graphics received a total rewrite in Stata 8 in 2003 I was convinced that I would sometime write my own graph scheme, but that never really happened once I discovered that s1color gets me most of the way to where I usually want to be. Incidentally I see a lot of flak on social media (some but not all good-natured) directed at use of Stata's s2color default (most obvious from its blue backdrop) and have to wonder how many of those giving or receiving flak realise that it's only the default and in no sense compulsory (or even recommended).

    Use of colour is evidently a frequent example of option choices and/or style preferences. I've been reading for years, for example, that orange and blue work well together -- especially for those who have difficulty distinguishing red and green. And there's a massive literature on colour in several sciences (and beyond) and helpful websites such as colorbrewer2.org

    An excellent book by Claus Wilke (which I strongly recommend, as at https://www.amazon.com/gp/customer-reviews/R22MWD7RJ6QAFP) uses one of many available colour palettes, which appears to be due to Okabe and Ito (full reference in the help file). It often works well when the aim is to show different groups differently (but we aren't talking about monotonic scales such as low to high or strong negative to strong positive). But it's defined as a series of RGB triples, and I don't want to have to remember (or to look up) and then to type a series of RGB triples, so what to do?

    If you run

    Code:
    mycolours
    you get this output, telling you that local macros have been defined in your namespace (programmers may want to look at the code to see how this is done).

    Code:
    locals defined:
        OK1 and ora for orange
        OK2 and sky for sky blue
        OK3 and bgr for bluish green
        OK4 and yel for yellow
        OK5 and blu for blue
        OK6 and ver for vermilion
        OK7 and rpu for reddish purple
        OK8 and bla for black
    and if you want to see what those colours look like, an understandable desire

    Code:
     
    mycolours, show
    shows this graph



    Click image for larger version

Name:	mycolours.png
Views:	1
Size:	17.8 KB
ID:	1568169


    The deal now is what you can do. You can type those local macro names in graph commands, and you get the colours so defined.

    Here are some examples:


    Code:
    sysuse auto, clear
    mycolours 
    set scheme s1color 
    scatter mpg weight, mc("`ora'")
    scatter mpg weight if foreign, mc("`OK1'") || scatter mpg weight if !foreign, mc("`OK2'") legend(order(1 "Foreign" 2 "Domestic"))
    scatter mpg weight if foreign, mc("`OK1'%50") || scatter mpg weight if !foreign, mc("`OK2'%50") legend(order(1  "Foreign" 2 "Domestic"))
    I show here only the last graph.

    Click image for larger version

Name:	mycolours2.png
Views:	2
Size:	33.2 KB
ID:	1568171


    Naturally, other choices are possible, say different marker symbols too.

    If you don't find this palette interesting or useful, and you would prefer some other palette or palettes, you can still think about defining some local macros in the style of mycolours.
    Attached Files

  • #2
    Dear Nick, this is a nice addition to the Stata tool box for using colours.

    But, note that your example in the help file:
    Code:
    sysuse auto, clear
    scatter mpg weight, mc("`ora'")
    scatter mpg weight if foreign, mc("`OK1'") || scatter mpg weight if !foreign, mc("`OK2'") legend(order(1 "Foreign" 2 "Domestic"))
    scatter mpg weight if foreign, mc("`OK1'%50") || scatter mpg weight if !foreign, mc("`OK2'%50") legend(order(1 "Foreign" 2 "Domestic"))
    gives the result using Stata's default colors.

    What is required is a call to the macro first, like:
    Code:
    sysuse auto, clear
    mycolours
    scatter mpg weight, mc("`ora'")
    
    mycolours
    scatter mpg weight if foreign, mc("`OK1'") || scatter mpg weight if !foreign, mc("`OK2'") legend(order(1 "Foreign" 2 "Domestic"))
    
    mycolours
    scatter mpg weight if foreign, mc("`OK1'%50") || scatter mpg weight if !foreign, mc("`OK2'%50") legend(order(1 "Foreign" 2 "Domestic"))
    http://publicationslist.org/eric.melse

    Comment


    • #3
      ericmelse That's correct. You need to run mycolours first in order to be able to use its results. That's the point, but I will amend the help along these lines. Note that you need do it only once in a session or file. Thanks!

      Comment


      • #4
        Another NATURE METHODS paper of Bang Wong is of interest as well: color coding (without pay wall).
        http://publicationslist.org/eric.melse

        Comment


        • #5
          Thanks to Kit Baum, and to Eric, again, the help file has been updated on SSC, For the record, the code example was in its entirety previously


          Code:
              . mycolours
              . mycolours, show
              . macro list
           
              . sysuse auto, clear
              . scatter mpg weight, mc("`ora'")
              . scatter mpg weight if foreign, mc("`OK1'") || scatter mpg weight if !foreign, mc("`OK2'") legend(order(1
                  "Foreign" 2 "Domestic"))
              . scatter mpg weight if foreign, mc("`OK1'%50") || scatter mpg weight if !foreign, mc("`OK2'%50") legend(order(1
                  "Foreign" 2 "Domestic"))
          and now it is

          Code:
            You need to run mycolours first so that local macros are defined.
          
              . mycolours
              . mycolours, show
              . macro list
           
          
              Now you can invoke those macros in graph commands.
          
              . sysuse auto, clear
              . scatter mpg weight, mc("`ora'")
              . scatter mpg weight if foreign, mc("`OK1'") || scatter mpg weight if !foreign, mc("`OK2'") legend(order(1
                  "Foreign" 2 "Domestic"))
              . scatter mpg weight if foreign, mc("`OK1'%50") || scatter mpg weight if !foreign, mc("`OK2'%50") legend(order(1
                  "Foreign" 2 "Domestic"))
          I have also added some more discussion.

          If you found this interesting enough to install, you should please update with


          Code:
          ssc install mycolours, replace

          Comment

          Working...
          X