Announcement

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

  • Unable to limit scale of y-axis

    Hi. I'm trying to plot a twoway scatter graph and want to limit my y-axis range from 1 to 40. I have only one observation more than 40, which I would like to not have on the graph as it distorts the visual. Despite trying everything, I seem to be failing at it. I'm using Stata 18.

    Code:
    sort week_num
    tw    (scatter week_docalltele week_num, ///
        mc(gs10) msize(vsmall)              ///
        xline(78 107, lpattern(dash) lcol("cranberry")) ///
        xlab(40 "Pre-intervention" 92 "Mandate" 120 "Platform", noticks labsize (small)) ///
        yscale(range(1 40)) ylab(, nogrid labsize(vsmall))     ///
        ytitle("Number of consultations")   ///
        xtitle("") scheme(s1color) legend(order(1 "observed" 2 "predicted values")) legend(size(vsmall)))  ///
        (lfit linear_spline week_num if period==0, col("navy")) ///
        (lfit linear_spline week_num if period==1, col("purple")) ///
        (lfit linear_spline week_num if period==2, col("brown"))
    
    
    *data sample
    input float(week_num week_docalltele linear_spline period)
    1  3 4.5797243 0
    1  5  4.909664 0
    1  3 4.5797243 0
    1 16  2.900499 0
    1 12 4.2337675 0
    1  6  3.197232 0
    1 62  7.691156 0
    1  5   6.47571 0
    1  3  3.424561 0
    1  2  3.424561 0
    1  3  4.702587 0
    1  1  5.927761 0
    1  1  4.131293 0
    2  6  5.891848 0
    2  6  6.439798 0
    2 15  2.864586 0
    2  3  2.967061 0
    2  2  4.543812 0
    2  2  4.543812 0
    2 10  7.655243 0
    2  6  3.388648 0
    2  5  3.161319 0
    2  9  4.873751 0
    2  7 4.1978545 0
    2  2  3.844571 0
    2  1 4.6666746 0
    2  4  3.388648 0
    2  5 4.0953803 0
    2  2  2.615858 0
    3 29   7.61933 0
    3  7  3.125406 0
    3  8  4.507899 0
    3  1 3.3527355 0
    3  2 11.765936 0
    3  1  2.579945 0
    3  4 4.6307616 0
    3  6  5.855936 0
    3 13 2.8286736 0
    3  3  3.808658 0
    3  6 3.3527355 0
    3 24  4.837838 0
    3  2    4.8194 0
    3  6  4.161942 0
    4 10  4.126029 0
    4  1  6.367972 0
    4 15  12.30428 0
    4  2  4.023555 0
    4  2 11.730023 0
    4  3  3.316823 0
    4  1  4.783487 0
    4  1  4.594849 0
    4  1  7.583417 0
    4  5  4.801925 0
    4  5 4.4719863 0
    4  6  2.792761 0
    5  3 3.7368326 0
    5  3  2.756848 0
    5  3  3.053581 0
    5  1 4.7475743 0
    5  8 4.0901165 0
    5  1  2.859323 0
    5  1  11.69411 0
    5  5  4.436074 0
    5  4   3.28091 0
    5 13 4.7660127 0
    5  1   6.33206 0
    5 16  7.547505 0
    5  6  4.558936 0
    5  1   3.28091 0
    5  1  4.436074 0
    5  1 12.268368 0
    5  4  3.987642 0
    5  9   5.78411 0
    6 14  7.511592 0
    6  4  6.296147 0
    6 12 12.232455 0
    6  4 2.7209356 0
    6  1 4.0542035 0
    6  1 11.658197 0
    6  2   2.82341 0
    6  5  3.017668 0
    6  2 4.5230236 0
    6  3  4.711662 0
    6  1   3.70092 0
    6  1  3.951729 0
    6  7  4.400161 0
    6 11 4.7300997 0
    6  7  5.748197 0
    7  3 3.2090845 0
    7  1  2.787497 0
    7  2 4.4871106 0
    7  2 4.3642483 0
    7  5  5.712285 0
    7  1  2.436294 0
    7  1  2.981755 0
    7  9 3.2090845 0
    7 19  7.475679 0
    7  5  4.018291 0
    7  9  6.260234 0
    7  1  5.673591 0
    Click image for larger version

Name:	yscale.png
Views:	1
Size:	141.5 KB
ID:	1768266

  • #2
    Hi
    I would do this by simply replacing scatter week_docalltele week_num, by scatter week_docalltele week_num if week_docalltele < 40, so that the graph omits the value to discard and automatically adjusts the Y-axis length.
    Hope this helps!
    Thierry Buclin, MD, clinical pharmacologist

    Comment


    • #3
      This is well documented. Axis scale options will never reduce a range if the effect is to omit data, but only extend an axis range.

      Here is the help for axis scale options telling you that you can't do what you want that way:

      range() never narrows the scale of an axis or causes data to
      be omitted from the plot. If you wanted to graph yvar versus xvar for the subset of xvar values between 10 and 50, typing

      . scatter yvar xvar, xsc(r(10 50))

      would not suffice. You need to type

      . scatter yvar xvar if xvar>=10 & xvar<=50
      So using if is one possibility. Showing the data on a transformed scale is (to my mind) a better possibility. Here I see no zeros, so logarithmic scale is the first thing to try. I am not clear how far logarithmic thinking would be consistent with your splining.

      Comment


      • #4
        The -yscale()- option does not restrict the limits of the y-axis; all it can do is expand it. To restrict the limits (and thereby exclude data points) you have to use an -if- clause on the -twoway- command.


        I have only one observation more than 40, which I would like to not have on the graph as it distorts the visual.
        Certainly that one observation is an outlier and it "distorts" the visual. But by removing it, you are distorting the data! Do you have a reason to believe that that observation is a data error? If so you should fix it (if possible) or replace it by a missing value (if not). But if you have no reason to believe it is incorrect data, then I consider it misleading to exclude it from the plot, even if it mars the aesthetics of the picture.

        Added: Crossed with #3, which advocates using a log scale. I think that's a great idea: it preserves the integrity of the data and also preserves the aesthetics.

        Comment


        • #5
          Thank you, Everyone. This helps me understand why it wasn't working and what my options are.

          Comment

          Working...
          X