Announcement

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

  • Trouble controlling coefplot labels and markers simultaneously

    I am having trouble controlling coefplot labels and markers simultaneously. I'm using coefplot to display a series of means and their CIs, each created in a different city-specific dataset. So I'm looping through the datasets and saving the mean estimate for display.

    Here are 2 examples of the problem.

    Code:
    local agrp `"Chandler Columbus Hartford "Los Angelos" Mesa Montgomery "New Orleans" Portland Tucson"'
            
    local bgrp `"ChandlerCallsCoded_Fix_C4SO ColumbusCallsCoded_Fix_C4SO HartfordCallsCoded_Fix_C4SO LACallsCoded_Fix_C4SO MesaCallsCoded_Fix_C4SO MontgomeryCallsCoded_Fix_C4SO NewOrleansCallsCoded_Fix_C4SO PortlandCallsCoded_Fix_C4SO TucsonCallsCoded_Fix_C4SO"'
    
    local n : word count `agrp'
    
    clear matrix
    forvalues i = 1/`n' {
        local city : word `i' of `agrp'
        local dname : word `i' of `bgrp'
        
        use "${DSAVE}/`dname'", clear
    
        qui mean XVAR
        estimate store m`i'
    }
    
    ** This one creates labels, but won't display the 3rd markers as a square. 
    coefplot m1, bylabel(Chandler) ///
            || m2, bylabel(Columbus)  ///
            || m3, bylabel(Hartford)  ///
            || , bycoefs byopts(xrescale) vert ///
        mcolor(navy) ciopts(color(navy)) ///
        p3(msymbol(S)) ///
        xlabel(, angle(45)) ytitle("Percent of Dispatches") ///
        graphregion(color(white)) msize(small) ///
        legend(order(2 "Calls for service" ///
            6 "A subset of calls for service") rows(2))
    
    ** This one displays the 3rd markers as a square but has no label 
    coefplot m*, vert  mcolor(navy) ciopts(color(navy)) ///
        p3(msymbol(S)) ///
        xlabel(, angle(45)) ytitle("Percent of Dispatches") ///
        graphregion(color(white)) msize(small) ///
        legend(order(2 "Calls for service" ///
            6 "A subset of calls for service") rows(2))
    I can't figure out how to maintain the marker when I add the label. In an ideal world, I'd like to actually add the label INSIDE the loop as the value of iteration-specific `city'... in another version of the code I did that, but using mat and erepost, and I couldn't figure out how to store the CIs with that method. Help on making this code effective and efficient would be appreciated!

  • #2
    It will help if you can add a data example that reproduces the issue. coefplot is from SSC (FAQ Advice #12).

    Comment


    • #3
      Great point, sorry. Ok here is a reproducible example. Note that I have tried a few other versions of this code too, but these 2 got the closest while still failing to do everything i want at once. mlabel doesn't seem to help me, as it labels the actual marker rather than adding an axis tick plus label.


      Code:
      sysuse auto , clear 
      xtile p3=price, nquantiles(3)
      forval i=1/3 {
          qui mean weight
          estimate store m`i'
      }
      
      ** method 1: can make marker 2 square but can't figure out how to do labels 
      coefplot m*, vert  mcolor(navy) ciopts(color(navy)) ///
          p2(msymbol(S)) ///
          xlabel(, angle(45)) ytitle("Blah") /// 
          graphregion(color(white))  ///
          legend(order(1 "labels for most markers" ///
              2 "label for the square markers") rows(2))
      
      ** method 2: got the labels but can't change marker 2 or reference it in legend
      coefplot m1, bylabel(price q1) ///
              || m2, bylabel(price q2)  ///
              || m3, bylabel(price q3)  ///
              || , bycoefs byopts(xrescale) vert ///
          mcolor(navy) ciopts(color(navy)) ///
          p2(msymbol(S)) ///
          xlabel(, angle(45)) ytitle("Blah") /// 
          graphregion(color(white)) ///
          legend(order(2 "Calls for service" ///
              6 "A subset of calls for service") rows(2))

      Comment


      • #4
        I don't think that I have understood what is needed. If you can somehow add a visual of how you want the final graph to look like (perhaps a sketch of the ideal case), I may be able to work backwards from there. Otherwise CI plots and legend positions can be tricky, here the square marker is in pos. 4.

        Code:
        sysuse auto , clear 
        xtile p3=price, nquantiles(3)
        forval i=1/3 {
            qui mean weight
            estimate store m`i'
        }
        
        ** method 1: can make marker 2 square but can't figure out how to do labels 
        coefplot m*, vert  mcolor(navy) ciopts(color(navy)) ///
            p2(msymbol(S)) ///
            xlabel(, angle(45)) ytitle("Blah") /// 
            graphregion(color(white))  ///
            legend(order(4 "label for the square markers" ///
                         6 "label for the round markers") rows(3))
        Res.:

        Click image for larger version

Name:	Graph.png
Views:	1
Size:	25.8 KB
ID:	1689026

        Comment


        • #5
          What I want is the ability to label each mean separately (price q1, price q2, price q3) under it's own tick at the bottom of the graph, just like is produced by the first graphic. But I ALSO want to be able to control the marker shapes (and colors) separately, as the second graphic does.

          Here is a link showing what I like and don't like about each one.

          Created by slightly modified code (sorry!!)

          Code:
          ** method 1: can make marker 2 square but can't figure out how to do labels 
          coefplot m*, vert  mcolor(navy) ciopts(color(navy)) ///
              p2(msymbol(S)) ///
              xlabel(, angle(45)) ytitle("Blah") /// 
              graphregion(color(white))  ///
              legend(order(2 "labels for most markers" ///
                  4 "label for the square markers") rows(2))
          
          ** method 2: got the labels but can't change marker 2 or reference it in legend
          coefplot m1, bylabel(price q1) ///
                  || m2, bylabel(price q2)  ///
                  || m3, bylabel(price q3)  ///
                  || , bycoefs byopts(xrescale) vert ///
              mcolor(navy) ciopts(color(navy)) ///
              p2(msymbol(S)) ///
              xlabel(, angle(45)) ytitle("Blah") /// 
              graphregion(color(white)) ///
              legend(order(1 "labels for most markers" ///
                  2 "label for the square markers") rows(2))

          Comment


          • #6
            Something like?

            Code:
            sysuse auto , clear 
            xtile p3=price, nquantiles(3)
            forval i=1/3 {
                qui mean weight
                estimate store m`i'
            }
                     
            coefplot (m1, msymbol(s)  mc(navy) label(My lab 1)) ///
            (m2, msymbol(t) mc(navy) label(My lab 2)) ///
            (m3, msymbol(o)  mc(navy) label(My lab 3)) , aseq vert swapnames ///
             eqrename(m1= "price q1" m2= "price q2"  m3= "price q3") ///
            xlabel(, angle(45)) ytitle("Blah") ///
            graphregion(color(white)) ciopts(color(navy))
            Res.:

            Click image for larger version

Name:	Graph.png
Views:	1
Size:	41.8 KB
ID:	1689068

            Comment


            • #7
              Yes!! This is amazing, thank you so much! You are relying on some options I didn't even know existed / didn't understand how to use. Thank you!

              Comment


              • #8
                Actually, can I ask 1 more question, an unrelated problem? Is there any way to get estimates store to store the 99% CIs rather than 95% CIs?

                Comment


                • #9
                  For anyone else in the future -- I ended up using legend(order()) for my legend, instead of the label() option after each marker, because the label option forces a legend item for EACH marker, and I just wanted to label some of the markers (the 2 shapes). So I went with something like this:

                  coefplot (m1, msymbol(t) mc(navy)) ///
                  (m2, msymbol(t) mc(navy)) ///
                  (m3, msymbol(s) mc(navy)) , aseq vert swapnames ///
                  eqrename(m1= "price q1" m2= "price q2" m3= "price q3") ///
                  xlabel(, angle(45)) ytitle("Blah") ///
                  legend(order(2 "label for triangles" ///
                  6 "label for squares") rows(2)) ///
                  graphregion(color(white)) ciopts(color(navy))
                  Last edited by Leah Bevis; 12 Nov 2022, 09:55.

                  Comment


                  • #10
                    Originally posted by Leah Bevis View Post
                    Actually, can I ask 1 more question, an unrelated problem? Is there any way to get estimates store to store the 99% CIs rather than 95% CIs?
                    Code:
                    help coefplot##levels

                    Comment


                    • #11
                      Thank you!

                      Comment

                      Working...
                      X