Announcement

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

  • twoway rspike: how to make the spike it appear vertically in the legend?

    Dear Statalisters,

    please let me know if there is already a thread about this - I did not find one.
    In a rspike plot, the spikes appear vertically. But in the legend they are shown horizontally. Is there a way to make the spike appear vertically in the legend as well?

    Here is a little toy example to illistrate what I mean:

    Code:
    sysuse auto, clear
    sort weight
    gen id = _n
    
    twoway (connect weight id) ///
           (connect price id) ///
           (rspike weight price id), ///
           legend( symxsize(2pt) order(1 "weight" 2 "price" 3 "Spike"))
    Click image for larger version

Name:	Unbenannt.PNG
Views:	1
Size:	51.2 KB
ID:	1640298
    Last edited by Boris Ivanov; 10 Dec 2021, 04:38.

  • #2
    you could use variables that appear in legend but not in graph (or that are hard to spot...),
    Code:
    sysuse auto, clear
    sort weight
    gen id = _n    
    
    gen ghost1 = 2500
    gen ghost2 = 1
    
    twoway (connect weight id) ///
           (connect price id) ///
           (rspike weight price id ,lc(green)) ///
           (scatter ghost1 ghost2 ,ms(pipe) mlw(thin) mc(green)) ///
           ,leg(symxsize(2pt) order(1 "weight" 2 "price" 4 "Spike"))

    Comment


    • #3
      Thank you for your reply, sneaking a ghost scatter in is a nice idea. In this application it also works fine, as you can easily hide it in the spike. In my real application, however, I am running a loop that uses the rspike to plot confidence intervals. So I cannot easily specify ghost values that will hide the bar symbol. Also, I am using opacity, so you cannot really hide the bar symbol behind the spike.
      So, my toy example was not a good illustration of my setup.
      I adjusted it a little bit, in order to show the problem with this approach:

      Code:
      sysuse auto, clear
      sort weight
      gen id = _n  
      
      sum price
      gen cilo = price -  0.5*r(sd)
      gen cihi = price + 0.5*r(sd)
      
      gen ghost1 = 4000
      gen ghost2 = 1
      
      twoway (connect price id, col(%50)) ///
             (rspike cilo cihi id , lc(green%50) lw(thick)) ///
             (scatter ghost1 ghost2 , ms(pipe) mlw(thick) mc(green%50)) ///
             ,leg(symxsize(3pt) order(1 "price" 3 "Spike"))
      Last edited by Boris Ivanov; 10 Dec 2021, 06:06.

      Comment


      • #4
        Not the answer you were hoping for, but to show confidence intervals for (several) different groups I would use horizontal alignment any way....

        Comment


        • #5
          Ok, then I might just leave the rspike out of the legend and put a remark in the notes that these mark CIs.
          @Nick: I fear I don't understand what you mean by horizintal alignment. Could you elaborate on that?

          Comment


          • #6
            Your spikes in #1 are vertical. Mine would be horizontal.

            Comment


            • #7
              I do not follow #3, but the added plot need not be visible. The easiest way is to specify an impossible condition, e.g., negative values for weight below.

              Code:
              sysuse auto, clear
              sort weight
              gen id = _n
              
              twoway (connect weight id) ///
                     (connect price id) ///
                     (rspike weight price id) ///
                     (scatter weight id if weight<-5, ms(pipe) mcolor(green)), ///
                     legend( symxsize(2pt) order(1 "weight" 2 "price" 4 "Spike"))
              Click image for larger version

Name:	Graph.png
Views:	1
Size:	83.4 KB
ID:	1640357

              Comment


              • #8
                @Nick: Ok, I see. Thank you! @Andrew: Alright, this is very helpful. Thank you!

                Comment

                Working...
                X