How to make graphs with colour gradients

This post illustrates how to create a Stata bar chart with conditional colouring.

Picture

The colour gradient options were introduced in Stata 18. The code will not work with earlier versions of Stata.

1. Native twoway graphing command

Here is a script that will replicate the figure above:

 . webuse set https://www.jankabatek.com/datasets/
 . webuse gradient_data, clear
   
 . twoway bar FEE_CHANGE id, colorvar(FEE_CHANGE) colorstart(red) colorend(lime) colorcuts(-75(1)75) clegend(off) barwidth(0.9) horizontal ylabel(1(1)27,val labs(small)) ytitle("") xtitle("Change in student fees (%)") xlabel(-75(25)125) name(GRDNT1,replace)  

And here is the command split into relevant options, using the do-file syntax:

 . webuse set https://www.jankabatek.com/datasets/
 . webuse gradient_data, clear
   
 . twoway bar FEE_CHANGE id,         /// baseline twoway command
     colorvar(FEE_CHANGE)            /// value of FEE_CHANGE governs the colour for each bar
     colorstart(red) colorend(lime)  /// colour gradient ranges from red to lime
     colorcuts(-75(1)75)             /// number of distinct colours. Try: colorrule(lin)
     clegend(off)                    /// getting rid of the colour legend (it's messy)
     barwidth(0.9)                   /// making the bars look nicer 
     horizontal                      /// unrelated options from this line onwards
     ylabel(1(1)27,val labs(small))  /// 
     xlabel(-75(25)125) ytitle("")   ///
     xtitle("Change in student fees (%)") name(GRDNT1,replace)  

2. Plot suite

We can make the same chart using the plot suite. This way, we can combine the chart with other complex visual elements:

 . webuse set https://www.jankabatek.com/datasets/
 . webuse gradient_data, clear
 . net install plottabs, from("https://raw.githubusercontent.com/jankabatek/plotsuite/master/ado/") replace
 . ssc install schemepack
      
 . plotmeans FEE_CHANGE, over(id) clear gr(bar) ci(off) colorvar(y_val1) colorstart(red) colorend(lime) colorcuts(-75(1)75) clegend(off) scheme(white_tableau) barwidth(0.9) horizontal ylabel(1(1)27,val labs(small)) ytitle("") xtitle("Change in student fees (%)") xlabel(-75(25)125) name(GRDNT2,replace)  

The same command using the do-file syntax:

 . webuse set https://www.jankabatek.com/datasets/
 . webuse gradient_data, clear
 . ssc install plottabs 
 . ssc install schemepack
     
 . plotmeans FEE_CHANGE, over(id) clear gr(bar) ci(off)     /// plotmeans options
       colorvar(y_val1) colorstart(red) colorend(lime)      /// essential gradient options  
       colorcuts(-75(1)75)                                  /// finer gradient options. 
       clegend(off)                                         /// getting rid of the legend
       barwidth(0.9)                                        /// making the bars look nicer 
       scheme(white_tableau)                                /// nicer scheme, thx to Asjad 
       horizontal ylabel(1(1)27,val labs(small)) ytitle("") /// twoway options
       xtitle("Change in student fees (%)") xlabel(-75(25)125) name(GRDNT2,replace)     
Note the different variable (y_val1) in colorvar - this is because plot suite stores the plotted data in a separate frame with standardized variable names

3. Coefplot

We can also make the same chart using the coefplot. Doing so however requires running regressions / creating matrices (see below):

 . webuse set https://www.jankabatek.com/datasets/
 . webuse gradient_data, clear
 . ssc install coefplot
 . ssc install schemepack

 . reg FEE_CHANGE ibn.id, nocons

 . coefplot, recast(bar) grid(between glpattern(solid) glcolor(gs15)) colorvar(FEE_CHANGE) colorstart(red) colorend(lime) colorcuts(-75(1)75) clegend(off) barwidth(0.9) scheme(white_tableau)  xtitle("Change in student fees (%)") xlabel(-75(25)125) name(GRDNT3,replace)  

The same command using the do-file syntax:

 . webuse set https://www.jankabatek.com/datasets/
 . webuse gradient_data, clear
 . ssc install coefplot
 . ssc install schemepack
     
 . reg FEE_CHANGE ibn.id, nocons

 . coefplot, recast(bar)                             /// coefplot options  
       grid(between glpattern(solid) glcolor(gs15))  /// (incl. nicer horizontal grid lines)
       colorvar(FEE_CHANGE)                          /// essential colour gradient options  
       colorstart(red) colorend(lime)                ///   
       colorcuts(-75(1)75)                           /// finer gradient options.  
       clegend(off)                                  /// getting rid of the colour legend  
       barwidth(0.9)                                 /// making the bars look nicer  
       scheme(white_tableau)                         /// nicer scheme, thx to Asjad 
       xtitle("Change in student fees (%)") xlabel(-75(25)125) name(GRDNT3,replace)