Announcement

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

  • twoway legend obfuscates dashed bar boundary

    I want to use a dashed line to bound a bar in a twoway. But the legend key insists on putting a solid boundary in addition to the dashed boundary. I only want the dashed boundary. Any ideas? Thanks!

    Code:
    set scheme stmono2
    sysuse auto, clear
    collapse price, by(foreign)
    twoway (bar price foreign if foreign==0) || ( bar price foreign if foreign==1, fcolor(black%10) lcolor(black) lpattern(dash)), xla(0 `"`: label (foreign) 0'"' 1 `"`: label (foreign) 1'"')

    Click image for larger version

Name:	Screenshot 2025-03-07 213312.png
Views:	1
Size:	18.3 KB
ID:	1774024


  • #2
    This is interesting...

    Code:
    fcolor(white%50)
    Click image for larger version

Name:	Screenshot 2025-03-08 122711.png
Views:	1
Size:	16.4 KB
ID:	1774041



    Code:
    fcolor(red%50)
    Click image for larger version

Name:	Screenshot 2025-03-08 122839.png
Views:	1
Size:	10.6 KB
ID:	1774042
    Last edited by lucas reddinger; 08 Mar 2025, 10:34.

    Comment


    • #3
      It appears to be a bug in how Stata creates the legend in reference to the plot. By default, the legend symbol includes a solid outer line that matches the color of the fill. White works in this case because the background is white, making the outer line invisible. You could turn off the legend and create your own, but you should alert Technical Services to get any potential workaround or fix.

      Comment


      • #4
        Accepting the default settings in Stata, which create a solid black outer line for the legend with a white background, and applying some of the ideas from #2 (i.e., a line may be hidden in plain sight if it matches the background), we can define an impossible condition that generates a legend key without interfering with the graph. Here, we use a dashed white line to interrupt the solid black outer line, aligning it with our bar. Then, by using the -order()- option in the legend, we can suppress the second key.

        Code:
        set scheme stmono2
        sysuse auto, clear
        collapse price, by(foreign)
        set scheme stmono2
        sysuse auto, clear
        collapse price, by(foreign)
        
        
        twoway bar price foreign if foreign==0 || ///
        bar price foreign if foreign==1, fcolor(black%10) lcolor(black) lpattern(dash) ||  ///
        bar price foreign if foreign==-1, fcolor(black%10) lcolor(white) lwidth(medium) lpattern(shortdash) lalign(center) ||, ///
        xla(0 `"`: label (foreign) 0'"' 1 `"`: label (foreign) 1'"') leg(order(1 3))
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	20.2 KB
ID:	1774061

        Comment

        Working...
        X