Announcement

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

  • Creating graphs with xtline

    Hello everyone,

    I have a data showing the quality of observations before and after a certain ste. (Sample data below)

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(serial_number Quality process_step)
    1 0 3
    1 3 3
    2 2 2
    2 0 1
    3 1 1
    3 3 3
    4 1 1
    4 2 2
    5 0 0
    5 0 0
    end

    i am trying to generate a graph showing the general trend in change in quality (I've attached a sample here)



    i tried using xtline with the following code

    Code:
      
     xtline Quality , ///
            overlay ///
            xsc(r(0.95 2.05) ) ///
            xlabel(1 2, valuelabel labsize(small)) ylabel(, valuelabel labsize(small) ) ///
            i(serial_number) t(process_step) ///
            legend(off)
    but the graph that is generated makes it look like there are only 6 observations - is there a way to somehow "jitter" the lines to somehow show the number of lines that are overlaid?

    Thanks in advance

    regards
    Ahmed
    Last edited by Ahmed Ibrahim; 04 May 2021, 14:02.

  • #2
    What plays the role of time in your example?

    Comment


    • #3
      Hi Nick,

      I'm not sure if you mean in my data or the figure I provided as an example here

      In my data, i am using the process_step variable - 1 for before and 2 for after processing.

      I also realize the sample data I provided is wrong. The following is the correct sample data.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte(serial_number Quality process_step )
       1 0 2
       1 3 1
       2 2 1
       2 0 2
       3 1 2
       3 3 1
       4 1 2
       4 2 1
       5 0 1
       5 0 2
       end
      I'm also open to using any other program if there is one better suited for this task.

      Comment


      • #4
        The answer is presumably process_step so that

        Code:
        xtset serial_number process_step
        was what enabled
        xtline.

        Judging by your data example your data are only barely like those in the display. You have a counted or categorical scale with only a few distinct values, and line plots just result in over-plotting. So, one answer is to turn this into a transition matrix or contingency table.

        I used
        tabplot from the Stata Journal, written up systematically in SJ 16(2), latest version of files in 20(3) but example closer to this in 12(3).

        SJ-20-3 gr0066_2 . . . . . . . . . . . . . . . . Software update for tabplot
        (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
        Q3/20 SJ 20(3):757--758
        added new options frame() and frameopts() allowing framing
        of bars and so-called thermometer plots or charts

        SJ-17-3 gr0066_1 . . . . . . . . . . . . . . . . Software update for tabplot
        (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
        Q3/17 SJ 17(3):779
        added options for reversing axis scales; improved handling of
        axis labels containing quotation marks

        SJ-16-2 gr0066 . . . . . . Speaking Stata: Multiple bar charts in table form
        (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
        Q2/16 SJ 16(2):491--510
        provides multiple bar charts in table form representing
        contingency tables for one, two, or three categorical variables

        SJ-12-3 gr0053 . Speaking Stata: Axis practice, or what goes where on a graph
        (help multqplot if installed) . . . . . . . . . . . . . . . N. J. Cox
        Q3/12 SJ 12(3):549--561
        discusses variations on what goes on each axis of a two-way
        plot; provides multiple quantile plots


        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input byte(serial_number Quality process_step )
         1 0 2
         1 3 1
         2 2 1
         2 0 2
         3 1 2
         3 3 1
         4 1 2
         4 2 1
         5 0 1
         5 0 2
         end
         
         reshape wide Quality, i(serial) j(process_step)
         
         set scheme s1color 
         tabplot Quality2 Quality1, showval yasis xasis
        The example graph is not very interesting, but the version for your full dataset may be better.



        Click image for larger version

Name:	quality_tabplot.png
Views:	1
Size:	15.7 KB
ID:	1607707


        Comment


        • #5
          a

          Comment


          • #6
            Originally posted by Nick Cox View Post
            The answer is presumably process_step so that

            Code:
            xtset serial_number process_step
            was what enabled
            xtline.

            Judging by your data example your data are only barely like those in the display. You have a counted or categorical scale with only a few distinct values, and line plots just result in over-plotting. So, one answer is to turn this into a transition matrix or contingency table.

            I used
            tabplot from the Stata Journal, written up systematically in SJ 16(2), latest version of files in 20(3) but example closer to this in 12(3).

            SJ-20-3 gr0066_2 . . . . . . . . . . . . . . . . Software update for tabplot
            (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
            Q3/20 SJ 20(3):757--758
            added new options frame() and frameopts() allowing framing
            of bars and so-called thermometer plots or charts

            SJ-17-3 gr0066_1 . . . . . . . . . . . . . . . . Software update for tabplot
            (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
            Q3/17 SJ 17(3):779
            added options for reversing axis scales; improved handling of
            axis labels containing quotation marks

            SJ-16-2 gr0066 . . . . . . Speaking Stata: Multiple bar charts in table form
            (help tabplot if installed) . . . . . . . . . . . . . . . . N. J. Cox
            Q2/16 SJ 16(2):491--510
            provides multiple bar charts in table form representing
            contingency tables for one, two, or three categorical variables

            SJ-12-3 gr0053 . Speaking Stata: Axis practice, or what goes where on a graph
            (help multqplot if installed) . . . . . . . . . . . . . . . N. J. Cox
            Q3/12 SJ 12(3):549--561
            discusses variations on what goes on each axis of a two-way
            plot; provides multiple quantile plots


            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input byte(serial_number Quality process_step )
            1 0 2
            1 3 1
            2 2 1
            2 0 2
            3 1 2
            3 3 1
            4 1 2
            4 2 1
            5 0 1
            5 0 2
            end
            
            reshape wide Quality, i(serial) j(process_step)
            
            set scheme s1color
            tabplot Quality2 Quality1, showval yasis xasis
            The example graph is not very interesting, but the version for your full dataset may be better.



            [ATTACH=CONFIG]n1607707[/ATTACH]
            Thank you Nick. This was helpful.

            I did some research and also found another way to go about this. See code below.

            Code:
                reshape wide Quality, i(serial_number) j(Motioncorrectedornot)
                
                *generate new quality variables
                    gen recode_quality1=Quality1
                    gen recode_quality2=Quality2
                    
                *count
                    count if Quality1==Quality2
                    
                    count if Quality1<Quality2
                    
                    count if Quality1>Quality2
                    
            
                *create loop to add small amount to new quality
                    local add_to=0.01
                    
                    forval i=1/38 {
                        
                        replace recode_quality1 = recode_quality1 + `add_to' if serial_number==`i'
                        
                        local add_to = `add_to' + 0.01
                    }
                    
                    gen diff=recode_quality1-recode_quality2
                        sum diff, detail
                        
                    tab recode_quality1
                    
                    
                *drop old variables
                    drop Quality1 Quality2
                    
                *reshape back
                    reshape long recode_quality, i(serial_number) j(Motioncorrectedornot)
                    
                xtline recode_quality , ///
                    overlay ///
                    xtitle("") ytitle("Motion") ///
                    xsc(r(0.95 2.05) ) ///
                    ylabel(0.25 "None" ///
                           1.25 "Some" ///
                           2.25 "Moderate" ///
                           3.25 "Severe" ///
                           , labsize(small)) ///
                    xlabel(1 2, valuelabel labsize(small) ) ///
                    i(serial_number) t(Motioncorrectedornot) ///
                    legend(off) ///
                    name(trendline, replace)
                        graph export "./Graphs/trendline.tif", replace

            Comment

            Working...
            X