Announcement

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

  • Basic legend

    Hi All,
    I have created several scatterplots and I have tried using the help function to understand how to create a proper legend but I keep getting error messages.

    I used this command: graph twoway scatter iopcc_out mcholesterol3_out || lfit iopcc_out mcholesterol3_out0|| lfit iopcc_out mcholesterol3_out1, sort

    Which is a scatterplot that shows the DV and the IV, but presented by Sex. The graph looks like this:

    I would like to add in y and x axes, and instead of "fitted values" in the legend, I would like it to read "Male" and "Female"



    Click image for larger version

Name:	Screen Shot 2018-07-04 at 4.08.16 PM.png
Views:	1
Size:	114.6 KB
ID:	1451887









  • #2
    Alan,

    There is a legend option that allows you to do what you want.

    Code:
    graph twoway scatter iopcc_out mcholesterol3_out || lfit iopcc_out mcholesterol3_out0|| lfit iopcc_out mcholesterol3_out1, legend(order(2 “Male” 3 “Female”)) sort

    Of course, I haven’t tested this because you haven’t provided any data but I think it should work.

    Lance

    Comment


    • #3
      Originally posted by Lance Erickson View Post
      Alan,

      There is a legend option that allows you to do what you want.

      Code:
      graph twoway scatter iopcc_out mcholesterol3_out || lfit iopcc_out mcholesterol3_out0|| lfit iopcc_out mcholesterol3_out1, legend(order(2 “Male” 3 “Female”)) sort

      Of course, I haven’t tested this because you haven’t provided any data but I think it should work.

      Lance
      Thanks Lance!
      I get the message "“Male” not an integer, option order() ignored

      I have sex coded as 0/1 (0 = male).

      Do you have any advice on what could be changed to make this work?

      Comment


      • #4
        Alan,

        It's impossible to know how to help you when I don't know what you programmed to get that error. I've reproduced the code I suggested with the auto dataset and I don't get any errors. Here's the code...

        Code:
        sysuse auto, clear
        
        tw sc price mpg || lfit price mpg if foreign==0 || lfit price mpg if foreign==1, legend(order(2 "Domestic" 3 "Foreign"))

        As others have suggested to you elsewhere, you increase your chance of getting help if you provide a snippet of data. At the very least, you should share the what you typed along with the error you get.

        Lance

        Comment

        Working...
        X