Announcement

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

  • Graph title in different colors to replace legend

    Hi,

    I am trying to use the graph title to replace the legend. In this example, I would like the word "foreign" to look red and "local" blue. The rest of the words should remain black. I know that you can change the font or add bold or italics, but I could not find this in the documentation. Any help would be appreciated, thanks.


    Code:
    sysuse auto, clear
    twoway ///
    (scatter price mpg if foreign == 1, mcolor("red")) ///
    (scatter price mpg if foreign == 0, mcolor("blue")), ///
    legend(off) ///
    title("Price and mpg for foreign and local cars")

    Click image for larger version

Name:	Screenshot_20200715_112904.png
Views:	1
Size:	17.5 KB
ID:	1563480


  • #2
    I don't know of any way to do this with title options, but you could hack it thusly:

    Code:
    sysuse auto, clear
    twoway ///
    (scatter price mpg if foreign == 1, mcolor("red")) ///
    (scatter price mpg if foreign == 0, mcolor("blue")), ///
    legend(off) ti(" ") ///
    text(17000 20 "Price and mpg for", col(black)) ///
    text(17000 25.3 "foreign", col(red)) ///
    text(17000 27.7 "and", col(black)) ///
    text(17000 30.5 "domestic", col(blue)) ///
    text(17000 33.5 "cars", col(black))
    Not very versatile though...

    Comment


    • #3
      You can assign a colour to an entire title, but I don't know a way of colouring different words differently. You can create your own minimalist legend more or less like this:

      Code:
      sysuse auto, clear
      set scheme s1color 
      twoway ///
      (scatter price mpg if foreign == 1, mcolor("red")  ms(Oh)) ///
      (scatter price mpg if foreign == 0, mcolor("blue") ms(+)), ///
      legend(off) title("Price and mpg") /// 
      text(15000 35 "foreign", size(medium) color(red)) /// 
      text(14000 35 "local", size(medium) color(blue))
      Click image for larger version

Name:	price_mpg.png
Views:	1
Size:	26.0 KB
ID:	1563485



      where I have varied the markers as otherwise there are severe problems of overlap and indeed occlusion.


      Comment

      Working...
      X