Announcement

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

  • Getting rid of border/box around legend

    I'm relatively new to Stata and trying to create a graph with a legend that satisfies my aesthetic prejudices but can't seem to eliminate the box or border around the legend. Example code here:

    Code:
    clear
    
    input treat time y
    0 0 30.8
    0 1 26.9
    0 2 25.8
    0 3 23.8
    1 0 26.5
    1 1 14.8
    1 2 19.5
    1 3 21.0
    end
    
    twoway (line y time if treat==0, lcolor(blue)) ///
           (line y time if treat==1,  lcolor(red)), ///
           legend(label(1 Placebo) label(2 Treatment) nobox ///
                  cols(1) ring(0) bplacement(ne) )
    produces this graph using StataIC 13.1 for Windows
    Click image for larger version

Name:	Graph.png
Views:	2
Size:	26.9 KB
ID:	636828

    Why is there still a box around the legend when I have specified the "nobox" option? From my reading of the documentation, this is a textbox option inside a legend option. I don't seem to be able to change other textbox options either so I suspect that I'm missing something that tells Stata that I'm trying to specify a textbox option. Any suggestions would be appreciated.

  • #2
    I sympathise with your problem: it's hard to discover where some of these options are. (But it helps a lot that graphics commands are programmable, so you can in effect build an archive via your do files.) Anyway, what you need is region(lstyle(none)) within your legend(...) options.


    Code:
    twoway (line y time if treat==0, lcolor(blue)) ///
           (line y time if treat==1,  lcolor(red)), ///
           legend(label(1 Placebo) label(2 Treatment) nobox ///
               region(lstyle(none)) cols(1) ring(0) bplacement(ne) )

    Comment


    • #3
      Thanks very much for the suggestion. This is better than what I had - although there is still a faint box around the legend. And I don't understand why the nobox option doesn't work.

      Comment


      • #4
        OK. Playing around a little more I find that changing nobox to box puts boxes around the legend labels. So this option does work - just applies to the textboxes containing the labels and not to the whole legend as I was hoping. Adapting Stephen's suggestion and looking at the list of region options (which I had somehow missed before in the documentation) region(style(none)) basically gives me what I was trying for. region(lcolor(white)) is also a good option. Still don't understand why Stephen's region(lstyle(none)) leaves a faint box around the legend.

        Comment


        • #5
          For what it's worth, I create virtually all my graphs in black/white/grey shades, and my twoway include option calls such as

          Code:
          scheme(s2mono) graphregion(color(white))
          If I also have legend( ... region(lstyle(none)) in that same command, then, when I print the resulting graph on a B&W printer, the "faint box around the legend" that you refer to is not apparent. Why do you see what you see? My guess is that Stata has to reserve an area of space in which to place the legend and it is the borders of that white space that are what you are seeing in effect.

          Comment


          • #6
            Specifiying the legend region color ''white'' should work.
            Code:
            twoway (line y time if treat==0, lcolor(blue)) ///
                   (line y time if treat==1,  lcolor(red)), ///
                   legend(label(1 Placebo) label(2 Treatment) region(col(white)) ///
                          cols(1) ring(0) bplacement(ne) )
            Roman

            Comment


            • #7
              Thanks for further comments. I've learned a few things. Fundamentally the issues seem to come down to proper understanding of the hierarchy of basic objects which make up the graph components, knowing how to apply particular options to particular components and how different sets of options may interact.

              As this is my first time on Statalist can someone also advise me how I should close off this thread?

              Comment


              • #8
                Depends what you mean by "close off". You can just summarize and thank people. You did that already, I think, so that's fine.

                Comment


                • #9
                  Paul: thanks for closing the thread -- that's good for Forum working. In addition to Nick's remarks, I would say more specifically that it would help to say precisely which of the suggestions made to you actually helped you in your case (and carefully distinguish between graphs using color and mono schemes).

                  Comment


                  • #10
                    Thanks you all. How do I get rid of thin gray lines in graphs after the graph is produced?

                    Comment


                    • #11
                      If you mean the horizontal lines, then 'nogrid' option after 'ylab' is the solution:



                      Code:
                      twoway scatter wm1 wm2, ylab( ,nogrid)
                      Roman

                      Comment


                      • #12
                        Thanks. But what if the graph is already produced long time ago and one wants to avoid the hassle of looking for the data to plot the graph again but only want to edit it?

                        Comment


                        • #13
                          I suppose graph editor is the only option.
                          Roman

                          Comment


                          • #14
                            The option is not available in the graph editor. Stata needs to do something about this

                            Comment


                            • #15
                              I am not sure what you mean. The option should be there if you are clicking in the right place. I am using Stata-15, I have it and am sure it was available in 14 as well. This is how it is in my OSX:

                              Code:
                              Run the graph editor after opening the graph
                              -> double click any of the y axis labels
                              -> click 'gridlines' button
                              -> uncheck 'showgrid'
                              Roman

                              Comment

                              Working...
                              X