Announcement

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

  • How do I change the color of the text in my legend, in twoway?

    My colleague created a graph in Stata using the code below. I'd like to make the writing "By eliminating lead" in the legend appear blue, like the writing of 'hlo_expost1'. How do I achieve this? Thanks!
    #delimit ; tw (pccapsym rank hlo rank hlo_expost1, msym(none) lcolor(blue)) (sc rank hlo, mlabel(hlos1) msymbol(none) mlabpos(9)) (sc rank hlo_expost1, mlabel(effect1s1) msymbol(none) mlabpos(3) mlabc(blue)) (sc rank left, mlabel(countryname) msymbol(none) mlabpos(3) mlabcolor(black)), ysc(off) xsc(range(650)) legend(order(2 "Status quo" 3 "By eliminating lead")) ylab(,nogrid) xlab(0(100)600,nogrid) xline(500) xsize(4) ysize(6) xtitle("World Bank, Harmonized Learning Outcomes", margin(20 0 0 0) height(5)) ; #delimit cr

  • #2
    Cross-posted at https://stackoverflow.com/questions/...n-stata-twoway

    Comment


    • #3
      Code:
      help legend options
      Code:
      webuse grunfeld, clear
      set scheme s1mono
      tw line invest year if company==1|| line invest year if company==2, ///
      sort leg(order(1 "some text" 2 "some other text") color(red))
      Click image for larger version

Name:	Graph.png
Views:	1
Size:	52.5 KB
ID:	1716785

      Comment


      • #4
        Andrew Musau I think the challenge in OP's problem is to have different colours for the text of different legend keys.

        Comment


        • #5
          #4 That was my reading too.

          Comment


          • #6
            I can't figure out a legend option in a twoway command that does this, but graph editor seems to be up to the task.

            Click image for larger version

Name:	graphcoltest.png
Views:	1
Size:	93.2 KB
ID:	1716856

            Comment


            • #7
              Originally posted by Hemanshu Kumar View Post
              Andrew Musau I think the challenge in OP's problem is to have different colours for the text of different legend keys.
              You are correct, I did not read the question properly. As the legend is considered one entity, I think the only way to do it is through the graph editor as suggested by Nick and John.

              Comment


              • #8
                You could use text() options to put separate text in different places with different colours.

                This example uses stc1 and stc2 for text items in Stata 18 matching default line colours, but in any other version since Stata 8 you just need to spell out the colours you want.


                Code:
                webuse grunfeld, clear
                su invest if year == 1954 & company == 1, meanonly
                local last1 = r(max)
                su invest if year == 1954 & company == 2, meanonly
                local last2 = r(max)
                tw line invest year if company==1 || line invest year if company==2, ///
                sort text(`last1' 1954.2 "1", color(stc1)) text(`last2' 1954.2 "2", color(stc2)) legend(off)
                Such direct labelling (labeling) is in many ways often preferable to a legend any way.
                Last edited by Nick Cox; 12 Jun 2023, 12:33.

                Comment


                • #9
                  The problem with editing the legend within the graph editor is that it also changes the colors of the symbols in the graph itself. Im creating a graph with two different groups, using symbol shapes to indicate membership of one group and colors to indicate membership of another. Thus, in the graph, every point in the scatter plot has some color, but I want the shapes in the legend to all show up as black. It seems like it cant be done...

                  Code:
                  HTML Code:
                  twoway (scatteri 10 10, mcolor(%0)) /// * graphing invisible point that will be used as the title of the 
                  ||       (rcap num_tables_coef_value_max num_tables_coef_value_min goal_coef_value if goallevel == "No Goal" & practicegroup == "Easy", lc(red)) /// * first graphing se's
                  ||       (rcap num_tables_coef_value_max num_tables_coef_value_min goal_coef_value if goallevel == "Attainable" & practicegroup == "Easy", lc(red)) ///
                  ||       (rcap num_tables_coef_value_max num_tables_coef_value_min goal_coef_value if goallevel == "Baseline" & practicegroup == "Easy", lc(red)) ///
                  ||       (rcap num_tables_coef_value_max num_tables_coef_value_min goal_coef_value if goallevel == "Stretch" & practicegroup == "Easy", lc(red)) ///
                  ||       (rcap num_tables_coef_value_max num_tables_coef_value_min goal_coef_value if goallevel == "No Goal" & practicegroup == "Medium", lc(blue)) ///
                  ||       (rcap num_tables_coef_value_max num_tables_coef_value_min goal_coef_value if goallevel == "Attainable" & practicegroup == "Medium", lc(blue)) ///
                  ||       (rcap num_tables_coef_value_max num_tables_coef_value_min goal_coef_value if goallevel == "Baseline" & practicegroup == "Medium", lc(blue)) ///
                  ||       (rcap num_tables_coef_value_max num_tables_coef_value_min goal_coef_value if goallevel == "Stretch" & practicegroup == "Medium", lc(blue)) ///
                  ||       (rcap num_tables_coef_value_max num_tables_coef_value_min goal_coef_value if goallevel == "No Goal" & practicegroup == "Hard", lc(green)) ///
                  ||       (rcap num_tables_coef_value_max num_tables_coef_value_min goal_coef_value if goallevel == "Attainable" & practicegroup == "Hard", lc(green)) ///
                  ||       (rcap num_tables_coef_value_max num_tables_coef_value_min goal_coef_value if goallevel == "Baseline" & practicegroup == "Hard", lc(green)) ///
                  ||       (rcap num_tables_coef_value_max num_tables_coef_value_min goal_coef_value if goallevel == "Stretch" & practicegroup == "Hard", lc(green)) ///
                  ||     (scatteri 10 10, mcolor(%0)) ///
                  ||       (scatter num_tables_coef_value goal_coef_value if goallevel == "No Goal" & practicegroup == "Easy", mcolor(red) msymbol(S)) /// * now graphing point estimates
                  ||       (scatter num_tables_coef_value goal_coef_value if goallevel == "Attainable" & practicegroup == "Easy", mcolor(red) msymbol(O)) /// 
                  ||       (scatter num_tables_coef_value goal_coef_value if goallevel == "Baseline" & practicegroup == "Easy", mcolor(red) msymbol(T)) ///
                  ||       (scatter num_tables_coef_value goal_coef_value if goallevel == "Stretch" & practicegroup == "Easy", mcolor(red) msymbol(X)) ///
                  ||       (scatter num_tables_coef_value goal_coef_value if goallevel == "No Goal" & practicegroup == "Medium", mcolor(blue) msymbol(S))  ///
                  ||       (scatter num_tables_coef_value goal_coef_value if goallevel == "Attainable" & practicegroup == "Medium", mcolor(blue) msymbol(O)) ///
                  ||       (scatter num_tables_coef_value goal_coef_value if goallevel == "Baseline" & practicegroup == "Medium", mcolor(blue) msymbol(T)) ///
                  ||       (scatter num_tables_coef_value goal_coef_value if goallevel == "Stretch" & practicegroup == "Medium", mcolor(blue) msymbol(X)) ///
                  ||       (scatter num_tables_coef_value goal_coef_value if goallevel == "No Goal" & practicegroup == "Hard", mcolor(green) msymbol(S)) ///
                  ||       (scatter num_tables_coef_value goal_coef_value if goallevel == "Attainable" & practicegroup == "Hard", mcolor(green) msymbol(O)) ///
                  ||       (scatter num_tables_coef_value goal_coef_value if goallevel == "Baseline" & practicegroup == "Hard", mcolor(green) msymbol(T)) ///
                  ||       (scatter num_tables_coef_value goal_coef_value if goallevel == "Stretch" & practicegroup == "Hard", mcolor(green) msymbol(X)) ///
                  ||     (function y = x, range(0 15) lc(grey%30)), ///
                  legend(order(1 "Practice Difficulty" 2 "Easy" 6 "Medium" 10 "Hard" 14 "Goal Level" 15 "No Goal" 16 "Attainable" 17 "Baseline" 18 "Stretch") ///
                          holes(5) ///
                          symxsize(3) ///
                          rows(10) ///
                          cols(1) ///
                          colfirst ///
                          notextfirst ///
                          position(3) ///
                  ) ///
                  xlabel(, labsize(medlarge)) ///
                  ylabel(, labsize(medlarge)) ///
                  xtitle("Goal", size(large)) ///
                  ytitle("Number of Tables Completed", size(large))

                  Comment


                  • #10
                    Originally posted by Sam Lindquist View Post
                    The problem with editing the legend within the graph editor is that it also changes the colors of the symbols in the graph itself. Im creating a graph with two different groups, using symbol shapes to indicate membership of one group and colors to indicate membership of another. Thus, in the graph, every point in the scatter plot has some color, but I want the shapes in the legend to all show up as black. It seems like it cant be done...
                    This thread deals with the legend text, not symbols. The colors of the symbols in the legend correspond to the colors of symbols in the graph itself. Previously, there have been discussions on how to include legend symbols that do not appear in the graph (see, e.g., #7 https://www.statalist.org/forums/for...-in-the-legend). You may consider a different colored symbol as a separate symbol in its own right.

                    Comment


                    • #11
                      Hi Andrew, I checked out that other thread and solved the issue. Thanks!

                      Comment

                      Working...
                      X