Announcement

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

  • Is it possible to have labels in two or more colors in Stata?

    Below is a detail of a figure created with Matlab where it is possible to assign different colors to the labels. The 100 sectors displayed can be grouped into 15 subgroups and each subgroup was assigned a different color.

    Is it possible to create such a plot in Stata? The Stata option "labcolor" only allows for a single color for all labels.
    Click image for larger version

Name:	image_5239.png
Views:	1
Size:	144.2 KB
ID:	1345485
    Last edited by Bert Breitenfelder; 16 Jun 2016, 00:46.

  • #2
    I think the trick is to define the value labels and then modify them for each group that you want to separate. Here code:

    Code:
    sysuse auto, clear
    sort foreign make
    sencode make, replace
    
    /* Define Groups Here */
    levelsof make if foreign==1, local(reds)
    levelsof make if foreign==0, local(blues)
    
    /* Line Plot */
    tw ///
    (connected price make, msize(small) lcolor(orange) mcolor(orange)) ///
    (connected mpg make, yaxis(2) msize(small) lcolor(green) mcolor(green)), ///
    yline(5000) ///
    xlabel(none, value angle(45) labsize(tiny)) ///
    xlab(`reds', add labcolor(maroon)) ///
    xlab(`blues', add custom labcolor(navy)) ///
    plotregion(fcolor(white) lcolor(white)) graphregion(fcolor(white) lcolor(white))
    The graphs looks like this:
    Click image for larger version

Name:	colored_labels.jpg
Views:	1
Size:	463.7 KB
ID:	1345571

    Comment


    • #3
      Great, thanks Dimitriy!

      Comment


      • #4

        Dimitriy, can you also help me with this problem? I tried to imitate your code, but the Stata always returned an error:
        egen coun0 = group(coun), label //
        myaxis order1 = coun, sort(mean Productivity) descending
        gen order2=order1-0.2
        gen order3=order1+0.2

        gen indicator=1 if order <=38
        replace indicator=0 if indicator==.
        /* Define Groups Here*/
        levelsof order1 if indicator==1, local(oecd)
        levelsof order1 if indicator==0, local(nooecd)

        twoway (bar Productivity order2, yaxis(1) barw(0.4) color(ebblue) fintensity(100)) (bar Trueproductivity order3, yaxis(1) barw(0.4) color(orange) fintensity(100)) (line CTratio order1, sort yaxis(2)), ///
        yscale(axis(1) range(0 14000)) ylabel(0(2000)14000, format(%9.0f) angle(0) labsize(small) axis(1) glp(solid)) ytitle("carbon productivity or true carbon productivity (USD/tonne)", size(small)) ///
        yscale(axis(2) range(-3 3)) ylabel(0(0.5)3.0, format(%9.1f) angle(0) labsize(small) axis(2)) ytitle("", axis(2)) ///
        xlabel(none, noticks angle(90) labsize(tiny) valuelabel nogrid) xtitle("") ///
        xlab(`oecd '`nonoecd', add custom labcolor(maroon navy)) ///
        legend(order(1 "Y/Ep 2018" 2 "(D/Lc)*(Lp/Ec)" 3 "TProductivity/CProductivity") ring(0) ///
        bplacement(neast) row(3) colgap(0.5) bmargin(0) size(vsmall) symy(1) symx(4) region(style(none))) graphregion(color(white)) ///
        plotregion(margin(t=0 b=0 l=0.5 r=0.5) style(none)) ///
        yline(1.0, axis(2) lstyle(foreground) lcolor(emerald))
        ​​​​​​​
        tick_styles[-1] invalid name
        I think the problem is due to ``add custom''.

        Comment


        • #5
          Nina Liu It's hard to tell what is happening without an MVE.

          My guess is that there is something weird with your quotes. Try

          Code:
          xlab(`oecd', add custom labcolor(maroon)) ///
          xlab(`nonoecd', add custom labcolor(navy)) ///
          If that does not work, please show
          (1) show what you typed
          (2) the exact error message that returned
          (3) using the code delimiters (#s) to format everything

          If you are able, include your data (or a simulated version with the same structure) using dataex and strip out anything in (1) that is not essential for showing the problem.

          Comment

          Working...
          X