Scheme Ghibli: How to set a palette in Stata

Scheme Ghibli: How to set a palette in Stata

Introduction

This is a description of how I prepared a scheme in Stata to set up my environment so that I automatically plot using colours from the Studio Ghibli films. They're nice colours, though I don't think mine work as well as the person who originally developed them. The figure at the top of the page shows how these colours work in a scatterplot, and the rest of this post is about how to get this working on your own version of Stata.

This is based on work from Ewen Henderson, who posted the methods for setting this up in R at Github.
Ewen used colours adapted from the website Movies in Color, which has some information on the colour palettes used in Ghibli movies. I cannot directly link to the swatches for Ghibli on that site (it's a tumblr type site), but the key pictures are below.

Figure 1: Spirited Away

spirited away easel.jpg

Figure 2: Laputa

laputa easel.jpg

Figure 3: Mononoke

mononoke easel.jpg

So my plan is to set up these colours in Stata, put some as defaults for plotting, and make the others available for use with the correct labeling.

Method

The basic plan here is to set up three components:

  1. Define a colour palette, with a global macro, that contains the names of all the colours
  2. Make a new scheme file that contains the key colours from various palettes, as defaults for plotting
  3. Set up a profile that loads everything when Stata starts

Unfortunately there is one problem - the colours defined by Ewen are in some kind of weird YAML (a type of markdown) format, and can't be read by Stata schemes. So I first need to translate them to RGB for Stata to recognize.

Fortunately, the YAML colours are recognized by Stata palettes, so I first define a palette, then set up a scheme, then write the profile.

Defining a colour palette

This can be done by writing a short program and either saving it as an ado file or running it every time. The general format of the program is like this:

Codeblock 1
program colorpalette_SpiritedMedium
c_local P #1F262E,#353831,#833437,#8F8093,#67B9E9,#C3AF97,#B7D9F2
c_local I sm1,sm2,sm3,sm4,sm5,sm6,sm7
end

This defines a colorpalette called SpiritedMedium which contains the colours from the Spirited Away medium palette (the top middle line in the Spirited Away figure above).

The colour hexes can be obtained by running Ewen's code in R or by following a link at his website to a text list of all the colour hexes. So by careful manipulation I can obtain these codes and use them.

Once the palette program is run you can view the colours by running the command

Codeblock 2
colorpalette SpiritedMedium

which produces Figure 4, showing the colours with their RGB codes.

Figure 4: Example swatch for the Spirited Away palette

SM easel.png

These RGB colour codes can be copy-pasted into a Stata scheme, which can then be loaded whenever you start Stata. Note however that these colours can't be called in Stata - if you enter, for example, sm5 as a colour in a plot like color(sm5) it won't work. You need to enter them into a graph style using the grstyle command, with an option which sets them as global macros:

Codeblock 3
grstyle init
grstyle set color SpiritMedium,n(7) globals

These global macros will be assigned the labels in your colorpalette (the second line of program 1). Once you have done this you can call them as macros like color($sm3). Note the use of the $ identifier.

So first, I wrote a much larger set of programs which defines three palettes, to follow the bottom lines of each of the figures above, as follows:

Codeblock 4
set scheme s1color
program colorpalette_SpiritedAway
c_local P #0F1217,#833437,#345C75,#8F8093,#C19A9B,#67B9E9,#C3AF97,#E1D7CB,#B7D9F2,#C7C0C8,#DBEBF8
c_local I sd1,sm3,sd5,sm4,sl3,sm5,sm6,sl6,sm7,sl4,sl7
end

program colorpalette_Mononoke
c_local P #030A10,#742C14,#3D4F7D,#657060,#CD4F38,#E48C2A,#BA968A,#F2C695,#9FA7BE,#EAD890,#B3B8B1
c_local I md1,mm2,mm3,mm4,mm5,mm6,ml2,ml6,ml3,mm7,ml4
end
	
program colorpalette_Laputa
c_local P #090D10,#403369,#776A3D,#5C5992,#AE93BE,#B4DAE5,#F0D77B,#D7CADE,#DAEDF3,#AFACC9
c_local I ld1,lm3,ld7,lm4,lm5,ld6,lm6,lm7,ll5,ll6,ll4
end

program colorpalette_Ghibli
c_local P #030A10,#742C14,#3D4F7D,#657060,#8F8093,#C19A9B,#67B9E9,#AE93BE,#B4DAE5,#F0D77B,#838A90,#BA968A,#9FA7BE,#DBEBF8,#DCD3C4
c_local I md1,mm2,mm3,mm4,sm4,sl3,sm5,lm5,ld6,lm6,ml1,ml2,ml3,sl7,tl7
end

This picks out a range from Spirited Away dark to light, then Mononoke from dark to light, then Laputa from Dark to Light. The labels beneath show where they come from, so for example in the SpiritedAway palette the first colour is coded sd1, meaning it is the first color in the Dark palette from Movies in Color. The final palette, Ghibli, combines sections of the others to make a palette of 15 colors, which are necessary for the defaults in the scheme I am going to make. I could not find a suitable final color so I picked up a final one from My Neighbor Totoro. Note you can easily make extra palettes for the other movies, but a large part of the swatches for each movie is very tonally (?) close dark colours that I think won't work in plotting, which is why each of my palettes contains only 11 colours, and why the overall palette contains a mixture from all the shows.

Note that it seems you need to run the first line, to set the scheme to s1color, or else the color palettes produce a weird error: function Intensify() not declared in class ColrSpace. This error is not solved by installing or reinstalling either the Colrspace or palette packages.

Once you have done this, codeblock 5 will set up the palettes in your style folder, and set up the global macros to call those colours, then print a graph of the results.

Codeblock 5
grstyle init
grstyle set color SpiritedAway,n(11) globals(sp*)
grstyle set color Mononoke,n(11) globals(mn*)
grstyle set color Laputa,n(11) globals(lp*)
grstyle set color Ghibli,n(15) globals(gh*)
colorpalette, lc(black): SpiritedAway / Mononoke / Laputa / Ghibli

This produces Figure 5, which shows the structure of the palettes.

Figure 5: Four palettes from Studio Ghibli

swatches.png

I think they're good! The grstyle command option globals sets up the macros, which enable you to call the corresponding colours using a simple macro. So for example, in a scatter plot, the option color($sp3) will make the points take the 3rd colour from the Spirited Away palette.

The code below runs this for my current tobacco project, producing a plot of current smoking in Norway in 2010, with (fake) credible interval, using various elements of these palettes: the 15th Ghibli element for the credible interval, the 3rd element of Spirited Away for the spikes, and the 2nd element of Mononoke for the fitted line.

Codeblock 6

local sexType="males"
local yearType=2010
local countryType="norway"
local countryAbb="NOR"
local defType="current_user_any_smoked_tobacco"
twoway (rarea predValL predValU age_midpoint if sex=="`sexType'"&year==`yearType'&country=="`countryType'"&def_type_code=="`defType'",color($gh15)) ///
	(pcspike value start_age end_value end_age if sex=="`sexType'"&year==`yearType'&country=="`countryType'"&def_type_code=="`defType'",color($sp3)) ///
	(line predVal age_midpoint if sex=="`sexType'"&year==`yearType'&country=="`countryType'"&def_type_code=="`defType'",color($mn2))	, ///
	legend(label(1 "Credible Inteval") label(2 "Observed") label(3 "Predicted")) subtitle("Current smoking in Norwegian men, 2010")

This produces Figure 6, which has the light Studio Ghibli colour number 15 for the (fake) credible interval, Spirited Away colour 3 for the spikes that show the data with its age ranges, and Princess Mononoke colour 2 for the predicted line.

Figure 6: Example plot using Ghibli palettes

Ghibli plot example.png

Setting up a scheme

Next we set up the scheme. I learnt some of how to do this from this website, though I did a little more simply, and the filepaths registered on that page have changed since this website was written. Basically, I copied the s1color scheme, which can be found in this folder (on Mac):

  • /Applications/Stata/ado/base/s

Then, I pasted it in the personal folder Stata uses when it starts, which is here:

  • ~/Library/Application Support/Stata/ado/personal

Then I edited it. Note that I did not edit the original scheme! In particular, it has a block of code that defines the default colours for plotting, which are labeled color p down to color p15. These can be simply be replaced with the colours I want to use as my default, which will be my Ghibli palette. I think this code runs from lines 80 - 95 of the s1color scheme, and is shown in Figure 7 (a screenshot).

Figure 7: Screenshot of s1color scheme default plot colours

S1color.png

So you can just swap these in with our list from our palette. However! This scheme cannot read hex colors, so before you put them in you need to generate a color palette listing the corresponding rgb codes, which can be done by typing:

colorpalette Ghibli

which produces figure 8, another block of colours with their corresponding RGB codes.

Figure 8: Full Ghibli palette with RGB codes

GhibliCodes.png

From this you can simply type the RGB codes into the code block for the scheme.

The final code is shown in codeblock 7. Note that I left the color p in its original form. I am not sure what p is used for, because when you call a figure in Stata without specifying a colour it will use p1 first.

Codeblock 7
color p       gs8
color p1      "3 10 16"
color p2      "116 44 20"
color p3      "61 79 125"
color p4      "101 112 96"
color p5      "143 128 147"
color p6      "193 154 155"
color p7      "103 185 233"
color p8      "174 147 190"
color p9      "180 218 229"
color p10     "240 215 123"
color p11     "131 138 144"
color p12     "186 150 138"
color p13     "159 167 190"
color p14     "219 235 248"
color p15     "220 211 196"

You can go through and change other details if you like too. For example the xyline color is set to "cranberry", so I changed it to the Mononoke color of red.

Once the scheme is set up, give it a name like scheme-Name.scheme, and activate it by typing

set scheme Name

I called mine scheme-sg.scheme, so I just type set scheme sg.

💡
The website I got this information from recommends opening these files in notepad or textedit, but you can edit them directly (and much more easily) by opening in Stata's editor. This also avoids issues with naming.

Writing a profile

Finally we want this scheme to be set at start, and ideally for the palettes to be defined when Stata opens, so that:

  • We can call specific Ghibli colours using our global macros, without having to run code to define the palettes (don't forget the $ in front of the macros!)
  • The Ghibli palette will be default for plotting

To do this we save a do file called profile in our Stata folder. For me this is:

  • ~/Library/Application Support/Stata

In this folder there are 4 sub-folders: ado, Stata 14, Stata 15, and Syntax Themes. I just put the profile do file in the base of this folder, not in the Stata 15 or ado sub-folders (I use stata 15 at home).

This profile runs on open, and it just needs the set scheme command. However, I added all the palettes and the grstyle commands, as shown in codeblock 8.

Codeblock 8
set scheme sg

* program the palettes and set the style so that there are global macros for accessing specific colors:


program colorpalette_SpiritedAway
c_local P #0F1217,#833437,#345C75,#8F8093,#C19A9B,#67B9E9,#C3AF97,#E1D7CB,#B7D9F2,#C7C0C8,#DBEBF8
c_local I sd1,sm3,sd5,sm4,sl3,sm5,sm6,sl6,sm7,sl4,sl7
end

program colorpalette_Mononoke
c_local P #030A10,#742C14,#3D4F7D,#657060,#CD4F38,#E48C2A,#BA968A,#F2C695,#9FA7BE,#EAD890,#B3B8B1
c_local I md1,mm2,mm3,mm4,mm5,mm6,ml2,ml6,ml3,mm7,ml4
end
	
program colorpalette_Laputa
c_local P #090D10,#403369,#776A3D,#5C5992,#AE93BE,#B4DAE5,#F0D77B,#D7CADE,#DAEDF3,#AFACC9
c_local I ld1,lm3,ld7,lm4,lm5,ld6,lm6,lm7,ll5,ll6,ll4
end

program colorpalette_Ghibli
c_local P #030A10,#742C14,#3D4F7D,#657060,#8F8093,#C19A9B,#67B9E9,#AE93BE,#B4DAE5,#F0D77B,#838A90,#BA968A,#9FA7BE,#DBEBF8,#DCD3C4
c_local I md1,mm2,mm3,mm4,sm4,sl3,sm5,lm5,ld6,lm6,ml1,ml2,ml3,sl7,tl7
end
grstyle init
grstyle set color SpiritedAway,n(11) globals(sp*)
grstyle set color Mononoke,n(11) globals(mn*)
grstyle set color Laputa,n(11) globals(lp*)
grstyle set color Ghibli,n(15) globals(gh*)

noisily display "successfully loaded the SpiritedAway, Mononoke, Laputa and Ghibli palettes"
noisily display "Use these with the global macros $sp, $mn, $lp and $gh respectively"
noisily display "There are 11 colours in each of the base palettes, and 15 in the Ghibli palette"
noisily display "view them all by typing colorpalette, lc(black): SpiritedAway / Mononoke / Laputa / Ghibli"
💡
In most MacOS setups it can be hard to save a file directly to a folder in the Library/Application support sub-folder, because it won't show up in the save as window. So instead I wrote my profile do-file in Stata, saved it to my desktop, used the "go to folder..." menu option to find my startup sub-folder, and then dragged and dropped the profile .do file into it. This worked.

Once all this is done, you should get the Ghibli palettes as a default. I did, and the figure at the top of this post shows the result of running 6 scatter commands without specifying the color of the points, allowing Stata to cycle through its default colours. The result is definitely Ghibli-esque.

Final note

The Movies in Color website seems to be no longer active but has colour palettes from a wide range of movies. It even has Nausicaa! However, it does not list the hex codes for those palettes, so you would have to contact the person who ran that site, or download the images and use a tool like Gimp or Photoshop to figure out the codes. If you did though, you could set up a default plot scheme for almost any movie on the site. For example:

  • Akira
  • Before Sunrise (I don't think this would be good for plotting)
  • Ferris Bueller's Day Off
  • In The Mood For Love (I don't think this would be good for plotting)

There are lots of other possibilities in the archive!

Finally, I think I will be using these colours as my default plotting style from now on, and will use the palette for this website when I need to!