Announcement

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

  • Two separate legends on the same graph

    Hello everyone,

    I’m trying to display two separate legends on the same graph. I searched through previous discussions but couldn’t find an answer to this specific issue.

    In my case, I would like to keep the legend for 1, 2, 3 with their colors at the bottom, and place the legend for the two lines at the top, but I haven’t been able to separate the two.

    If anyone has a solution or suggestion, I would really appreciate your help.

    Thanks in advance,

    John


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(monthly_date cumulative_count target_count base2 base5 base6 top2 top5 top6)
    768  3  1.1 0 1 1 1 1 2
    769  4  2.2 0 0 0 0 0 1
    770  8  3.3 0 0 3 0 3 3
    771  9  4.4 0 0 0 0 0 1
    772 10  5.5 0 0 1 0 1 1
    773 11  6.6 0 0 0 0 0 1
    774 13  7.7 0 0 0 0 0 0
    775 15  8.8 0 0 0 0 0 0
    776 15  9.9 0 0 0 0 0 0
    777 15   11 0 0 0 0 0 0
    778 15 12.1 0 0 0 0 0 0
    779 15 13.2 0 0 0 0 0 0
    780 15 14.3 0 0 0 0 0 0
    781 15 15.4 0 0 0 0 0 0
    782 15 16.5 0 0 0 0 0 0
    783 15 17.6 0 0 0 0 0 0
    784 15 18.7 0 0 0 0 0 0
    end
    format %tmm_CY monthly_date
    
    
    
    twoway  (rbar base2 top2 monthly_date, color(blue)) ///
                           (rbar base5 top5 monthly_date, color(red)) ///
                             (rbar base6 top6 monthly_date, color(green)) ///
                             (line cumulative_count monthly_date, yaxis(2)) ///
                             (line target_count monthly_date, lpattern(shortdash_dot) yaxis(2)), /// 
                             xtitle("") ///
                             legend(order(1 "1" 2 "2" 3 "3" 4 "Solid line" 5 "Dashed line") pos(6) col(5) size(vsmall)) //

    Click image for larger version

Name:	Graph.png
Views:	4
Size:	36.2 KB
ID:	1783466



  • #2
    I don't have a suggestion on two legends, except that you could just label the liines directly by text placed nearby..

    I do recommend not combining red and green. Many people have difficult distinguishing the two.

    Wong, B. 2011. Points of view: Color blindness. Nature Methods 8: 441. https://doi.org/10.1038/nmeth.1618 is one of many references explaining what to do instead.

    Comment


    • #3
      In the absence of other suggestions, here is some technique. I used mycolours from SSC but nothing makes that compulsory or even ideal.
      https://www.statalist.org/forums/for...ailable-on-ssc

      Code:
      clear
      input float(monthly_date cumulative_count target_count base2 base5 base6 top2 top5 top6)
      768  3  1.1 0 1 1 1 1 2
      769  4  2.2 0 0 0 0 0 1
      770  8  3.3 0 0 3 0 3 3
      771  9  4.4 0 0 0 0 0 1
      772 10  5.5 0 0 1 0 1 1
      773 11  6.6 0 0 0 0 0 1
      774 13  7.7 0 0 0 0 0 0*
      776 15  9.9 0 0 0 0 0 0
      777 15   11 0 0 0 0 0 0
      778 15 12.1 0 0 0 0 0 0
      779 15 13.2 0 0 0 0 0 0
      780 15 14.3 0 0 0 0 0 0
      781 15 15.4 0 0 0 0 0 0
      782 15 16.5 0 0 0 0 0 0
      783 15 17.6 0 0 0 0 0 0
      784 15 18.7 0 0 0 0 0 0
      end
      format %tmm_CY monthly_date
      
      mycolours 
      
      twoway  (rbar base2 top2 monthly_date, color("`OK1'")) ///
                             (rbar base5 top5 monthly_date, color("`OK2'")) ///
                               (rbar base6 top6 monthly_date, color("`OK3'")) ///
                               (line cumulative_count monthly_date, lc(black) yaxis(2)) ///
                               (line target_count monthly_date, lc(red) lpattern(shortdash_dot) yaxis(2)), ///
                               xtitle("") xtic(768/784) ///
                               text(2.35 778 "solid line", color(black)) ///
                               text(1.78 779.2 "dashed line", color(red)) ///
                               ytitle(Better title needed here, axis(1)) /// 
                               ytitle(and here, axis(2)) /// 
                               legend(order(1 "1" 2 "2" 3 "3") pos(6) col(5) size(vsmall))
      Click image for larger version

Name:	dubois.png
Views:	1
Size:	53.5 KB
ID:	1783488

      Comment


      • #4
        Thank you very much for your feedback and for introducing me to this package, which I wasn’t familiar with.
        Thanks as well for your recommendations. The chart I shared was a very simplified version, I had intentionally reduced it as much as possible for the example.

        And thank you for your involvement on this forum.

        Have a nice day.

        John

        Comment

        Working...
        X