Announcement

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

  • maximum value x-axis scatter plot

    Hi everyone,

    I've made some scatter plots and I'd like to adjust the maximum value on the x-axis. Now it ranges from 0-60 and I want to make it narrower; 0-40 for example. This means that some data will be omitted from the plot. Therefore I don't think I can use the x(range) option.

    I think using this is the solution:

    Code:
    scatter yvar xvar if xvar >=10 & xvar <=50
    Or should i use x(label) or x(scale)? And where exactly do I put this in my syntax (since I am also using a lowess)?

    This is the syntax for my scatter:
    Code:
    twoway (scatter z_wfa age_months if use_tdf_preg==0, msymbol(circle_hollow) mcolor(gs2))(lowess z_wfa age_months if use_tdf_preg==0, msymbol(circle_hollow) lcolor(black) lpattern(dash) lwidth(medthick))(scatter z_wfa age_months if use_tdf_preg==1, msymbol(triangle) mcolor(gs5))(lowess z_wfa age_months if  use_tdf_preg==1, msymbol(triangle) lcolor(black) lwidth(medthick))
    I hope you can help me out!

    Cheers,

    Lisanne

  • #2
    Occasionally it is necessary to specify both xscale() and xlabel(). The trick is usually to ensure that what you say is consistent. Sometimes the easiest thing of all is just to drop observations you don't want on the graph in between preserve and restore.

    Omitting inconvenient data is naturally always an act that needs to be defensible.
    Last edited by Nick Cox; 10 Oct 2014, 04:52.

    Comment


    • #3
      Thanks Nick. The thing is that when I try to specify xscale() and xlabel() I keep getting the error 'invalid syntax', probably because I don't know where they should go in the syntax (they refer to twoway,right?)
      I'd rather not drop data, just to avoid making mistakes with that. It's not that the data are inconvenient, I just have very few data after x=40 and it would make my graph more insightful (with more space) if could use a lower xmax.

      Comment


      • #4
        I, indeed we, really can't see your Stata from here. Unless you show the exact syntax used we can only assure you that Stata will be right from its point of view in complaining about invalid syntax. Please look at the FAQ Advice Section 12.

        Comment


        • #5
          Part of my problem is that I don't understand where that part of the syntax should go - since I already have an if-statement in there and because I am using a lowess. For example, when I try

          Code:
          twoway (scatter z_wfa age_months if use_tdf_preg==0, msymbol(circle_hollow) mcolor(gs2))(lowess z_wfa age_months if use_tdf_preg==0, msymbol(circle_hollow) lcolor(black) lpattern(dash) lwidth(medthick))(scatter z_wfa age_months if use_tdf_preg==1, msymbol(triangle) mcolor(gs5))(lowess z_wfa age_months if  use_tdf_preg==1, msymbol(triangle) lcolor(black) lwidth(medthick)), xscale(0 40)
          Stata tells me "option 0 not allowed".

          age_months is the xvar.

          When I try something like
          Code:
          twoway (scatter z_wfa (age_months if age_months <40) if use_tdf_preg==0, msymbol(circle_hollow) mcolor(gs2))(lowess z_wfa (age_months if age_months <40) if use_tdf_preg==0, msymbol(circle_hollow) lcolor(black) lpattern(dash) lwidth(medthick))(scatter z_wfa (age_months if age_months <40) if use_tdf_preg==1, msymbol(triangle) mcolor(gs5))(lowess z_wfa (age_months if age_months <40) if  use_tdf_preg==1, msymbol(triangle) lcolor(black) lwidth(medthick))
          Stata gives the error 'invalid sytax'.

          Comment


          • #6
            In your first code segment one problem is with

            Code:
            xscale(0 40)
            which should be

            Code:
            xscale(r(0 40))
            One clue here is that Stata is objecting to the zero.

            In your second code segment you have four chunks like

            Code:
              
            scatter z_wfa (age_months if age_months <40) if use_tdf_preg==0
            That will just confuse Stata mightily. You're using parentheses as you might in high school mathematics, but Stata has fairly strict rules about when they can be used. Also, you must combine your conditions as in

            Code:
            scatter z_wfa age_months if age_months < 40 & use_tdf_preg == 0
            You have four commands with the same fix needed.
            Last edited by Nick Cox; 13 Oct 2014, 05:28.

            Comment


            • #7
              Unfortunately these options don't work. The 'range' option doesn't work in the sense that Stata gives me the exact same graph as without defining xscale, probably because it doesn't want data to be omitted from the plot.
              The second option would be this if I understand correctly:

              Code:
              twoway (scatter z_wfa age_months if age_months < 40 if use_tdf_preg==0, msymbol(circle_hollow) mcolor(gs2))(lowess z_wfa age_months if age_months < 40 if use_tdf_preg==0, msymbol(circle_hollow) lcolor(black) lpattern(dash) lwidth(medthick))(scatter z_wfa age_months if age_months < 40 if use_tdf_preg==1, msymbol(triangle) mcolor(gs5))(lowess z_wfa age_months if age_months < 40 if use_tdf_preg==1, msymbol(triangle) lcolor(black) lwidth(medthick))
              However that gives 'invalid syntax' again.

              Code:
              twoway scatter z_wfa age_months if age_months < 40
              works fine, but the combination with the second if statement is invalid again. How do I put these together? :-/

              Comment


              • #8
                Lisanne: I think this has already been explained. Once again

                Code:
                if age_months < 40 if use_tdf_preg == 0
                is illegal. You must combine conditions (using in this case &)

                Code:
                if age_months < 40 & use_tdf_preg == 0

                Comment


                • #9
                  Sorry, you're totally right. I copied the wrong syntax there. When I try

                  Code:
                  Twoway(scatter z_wfa age_months if (age_months <40 & use_tdf_preg==0), mcolor(gs2))(lowess z_wfa age_months if (age_months <40 & use_tdf_preg==0), lcolor(gs2)) (scatter z_wfa age_months if (age_months <40 & use_tdf_preg==1), mcolor(gs5))(lowess z_wfa age_months if (age_months <40 & use_tdf_preg==1), lcolor(gs5))
                  Stata gives me the error 'twoway already defined' (error occurred while loading Twoway.ado). How is this possible considering twoway is not a variable but a command?

                  Comment


                  • #10
                    Stata is case-sensitive. In principle Twoway could be a quite different command. In fact it exists, as seen by

                    Code:
                     
                    which Twoway

                    Comment


                    • #11
                      Nick's response (#10) actually points to a problem because -h Twoway- takes one to the same place as -h twoway-; if Stata were consistent about the case here this would not be true

                      Comment


                      • #12
                        Rich: I don't see why you think there is a problem.

                        Stata is being consistent over case-sensitivity. Its own principle is that Twoway and twoway can be different commands, and in practice they are.

                        The reasoning is presumably to allow users to be careless and (in this case) not be bitten by that.

                        It's like being able to go to the left of an obstacle or to the right of an obstacle to end up behind it. The fact that you end up in the same place doesn't mean that there was only one way to get there.

                        When the command separate was being written an objection was that this can be a difficult word to spell even for people with good command of written English. So, there is an undocumented command seperate that in practice takes you to separate. The existence of this extra command is not really a counter-example to the principle that Stata is literal about what you type and does not indulge spelling errors.

                        Comment


                        • #13
                          if there were really a command -Twoway-, -help Twoway- would not be able to find it; that seems to me to be a problem (yes, I know that -which Twoway- finds something but what it finds is part of Stata and appears to be "blank" in the sense that it is just a way "to allow users to be careless"; however, what if Twoway were an actual user-written command and thus one could not get help by typing -h Twoway-?

                          Comment


                          • #14
                            On-lookers should note that we are friends (and even co-authors) (although neither of these states logically implies the other).

                            The fact that Twoway is undocumented is neither here nor there, or a different problem if it is one. Lots of stuff is undocumented, or even non-documented.

                            If Twoway were a user-written command, then sure, users would learn that it was a bad idea to write a command with that name, because StataCorp got there first. Only by messing with Stata's code and/or the adopath (both very bad ideas) could that be circumvented, although users who knew how to do either would not be posting a problem.

                            On my machine Twoway is dated 2007 and I didn't know before today that it exists, and I've not heard anyone stumbling against it before.

                            Comment


                            • #15
                              on my machine I get the following:

                              Code:
                              . which Twoway
                              /Applications/Stata/ado/base/T/Twoway.ado
                              *! version 1.2.1  22jun2012
                              I think in this case Nick and I just disagree about Stata "consistency" re: case; I don't see any reason to take this any further

                              Comment

                              Working...
                              X