Announcement

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

  • Line plot

    Hello,

    I would like to make a plot similar to the picture below. My data is below as well. I want to graph placementdays on the X axis and the percentage on the Y axis. Thanks--CJ

    Click image for larger version

Name:	Capture.PNG
Views:	1
Size:	24.8 KB
ID:	1680143

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float placementdays1
      1
     55
      2
      3
      2
      1
      7
     23
     56
     40
     90
     22
     13
      1
      3
     43
      .
     21
    183
    843
      4
     14
     10
     22
     26
     78
      8
    135
      9
     77
    105
      .
    369
    112
    147
     21
      5
    574
      3
    154
      3
    344
      2
      3
    623
      4
     62
      3
      3
      2
     14
     26
    813
    682
      0
      3
    138
     55
     30
      5
     63
     17
      3
     10
     57
     32
     50
     78
      1
     55
    168
    561
     50
     21
     19
      .
      .
     55
    337
      5
      6
     21
    499
      3
    153
    170
      6
    261
    261
     28
      3
    277
      2
      1
     81
     12
      2
      1
      2
     85
    end

  • #2
    I guess the issue is that I want to graph the percent that have placementdays==1,2,3,.......500,.....etc but not sure hwo to make this variable?

    Comment


    • #3
      Something along these lines?

      Code:
      contract placementdays1, cpercent(cumulperc)
      gen float still_in_care = 100 - cumulperc
      twoway line still_in_care placementdays1, xtitle("Placement days") ytitle("% still in care")
      NB. the scheme used for the graph below is lean1, available by typing
      Code:
      net install gr0002_3.pkg


      Click image for larger version

Name:	Screenshot 2022-09-01 at 12.17.18 AM.png
Views:	1
Size:	123.7 KB
ID:	1680164
      Last edited by Hemanshu Kumar; 31 Aug 2022, 12:50.

      Comment


      • #4
        I am not sure that I understand the graph, which doesn't appear to show monotonic decline.

        distplot from the Stata Journal is one command for complementary or reverse cumulative distributions (survivor or survival functions).

        Code:
        . search distplot, historical
        
        Search of official help files, FAQs, Examples, and Stata Journals
        
        FAQ     . . . . . . . . . . . . . . . . . . . . . . . . . . .  NJC Stata plots
                . . . . . . . . . . . . . . . . . .  UCLA Academic Technology Services
                7/08    https://stats.idre.ucla.edu/stata/faq/graph/njc-stata-plots/
        
        SJ-19-1 gr41_5  . . . . . . . . . . . . . . . . . Software update for distplot
                (help distplot if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q1/19   SJ 19(1):260
                changes include better handling of the by() option calls;
                simpler default y-axis titles; more detailed discussion of
                exactly what is plotted; and more information on ridits
        
        SJ-10-1 gr41_4  . . . . . . . . . . . . . . . . . Software update for distplot
                (help distplot if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q1/10   SJ 10(1):164
                new reverse(ge) option specifies plotting probabilities or
                frequencies greater than or equal to any data value
        
        SJ-5-3  gr0018  . . . . . . . . . .  Speaking Stata: The protean quantile plot
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                Q3/05   SJ 5(3):442--460           (see gr41_3 and gr42_3 for commands)
                discusses quantile and distribution plots as used in
                the analysis of species abundance data in ecology
        
        SJ-5-3  gr41_3  . . . . . . . . . . . . . . . . . Software update for distplot
                (help distplot if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q3/05   SJ 5(3):471
                simplified syntax; both by() and over() are now allowed
        
        SJ-4-2  gr0004  .  Speaking Stata: Graphing categorical and compositional data
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                Q2/04   SJ 4(2):190--215                                 (no commands)
                discusses graphical possibilities for categorical and
                compositional data
        
        SJ-4-1  gr0003  . . . . . . . . . . . . Speaking Stata: Graphing distributions
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                Q1/04   SJ 4(1):66--88                                   (no commands)
                a review of official and user-written commands for
                graphing univariate distributions; includes tricks
                beyond what is obviously and readily available
        
        SJ-3-4  gr41_2  . . . . . . . . . . . . . . . . . Software update for distplot
                (help distplot if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q4/03   SJ 3(4):449
                option tscale() renamed as trscale()
        
        SJ-3-2  gr41_1  . . . . . . . . . . . . . . . . . Software update for distplot
                (help distplot if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                Q2/03   SJ 3(2):211
                enhanced to use Stata 8 graphics and provides new options
        
        STB-51  gr41  . . . . . . . . . . . . . . . . . .  Distribution function plots
                (help distplot if installed)  . . . . . . . . . . . . . . .  N. J. Cox
                9/99    pp.12--16; STB Reprints Vol 9, pp.108--112
                plots the cumulative distribution function or survival function
                and allows multiple variables
        Here is some sample code for your data example. Some of the option choices, notably
        Code:
        xsc(alt)
        , are just for fun.

        Code:
        distplot placementdays1, trscale(100 * @) reverse ytitle(% still in care) yla(, ang(h)) xtitle(Placement days) xsc(alt)

        Attached Files

        Comment


        • #5
          Click image for larger version

Name:	placementdays.png
Views:	2
Size:	22.0 KB
ID:	1680160


          Here's the graph again if you can't see it easily.

          Comment


          • #6
            Thank you both. Both of these solutions work---trying to decide what to use, but I appreciate it.

            Comment


            • #7
              Nick, on distplot, is there anyway to show only the graph from 30 days on? I tried dropping and also specifinyg if placementdays1>=30 but it will still show from 0 to the end of placementdays

              Comment


              • #8
                I tried xscale(range(30 100)) but didn't seem to work

                Comment


                • #9
                  Collin James I want to flag an important difference that arises when you look at a subset of the placementdays1 domain. Let me do this using a much larger number than 30, so it's easy to see.

                  If you do
                  Code:
                  distplot placementdays1 if placementdays1>=500, trscale(100 * @) reverse ytitle(% still in care) yla(, ang(h)) xtitle(Placement days) xsc(alt)
                  the code treats the values greater 500 as the entire domain, and computes percentages based on that. The resulting graph looks like this:
                  Click image for larger version

Name:	Screenshot 2022-09-01 at 2.53.37 AM.png
Views:	1
Size:	118.3 KB
ID:	1680198



                  On the other hand, if you use the solution in #3 and introduce the if condition only at the graphing stage, i.e.
                  Code:
                  contract placementdays1, cpercent(cumulperc)
                  gen float still_in_care = 100 - cumulperc
                  
                  twoway line still_in_care placementdays1 if placementdays1>=500, xtitle("Placement days") ytitle("% still in care")
                  then the percentages are computed over the entire domain (from 0 onwards) and the plot is restricted to the domain above 500. So you get:
                  Click image for larger version

Name:	Screenshot 2022-09-01 at 2.55.52 AM.png
Views:	1
Size:	110.7 KB
ID:	1680199


                  While for obvious reasons, the shape of the two graphs is similar, notice the y-axis: the percentages being graphed are very different. My sense is you want the latter, not the former.

                  Comment


                  • #10
                    Hemanshu Kumar is bang on. An if qualifier with distplot doesn't calculate with all of the data but select some of the results to show; it restricts the calculation to the data selected. That is pretty much standard Stata behaviour.

                    Also range() within yscale() or xscale() never selects part of the graph; it is not a zooming suboption. This is documented within the help for axis scale options.

                    range() never narrows the scale of an axis or causes data to be omitted from the plot.
                    What you want naturally makes perfect sense but #7 and #8 don't work in that way. Hemanshu explained how to get it.

                    Note also cumul as an official command long predating distplot.

                    Comment


                    • #11
                      Thank you both---there is one issue with the solution in #3. I want to plot two different lines by a variable called inc_parent (that takes values 1 or 0). Do I have to include this in the contract statement in some way? And if so, what does it do when it contracts my variable "inc_parent." Yes, I see what you mean about how the distribution is calculated. This is very helpful. Thanks!

                      Comment


                      • #12
                        The best way to understand it is to try it. I would contract to frequencies first, and then calculate what is wanted afterwards. See also cumul as flagged in #7,

                        Comment

                        Working...
                        X