Announcement

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

  • Advanced scatter plots

    Good afternoon,

    My goal is to create a line plot where the lines are vertical.

    To explain:

    There are three variables
    1. The first is a string - let's call it String1
    2, the other two are doubles, Double1
    3.
    and Double2

    My objective is to have the String1 observations on the Y-axis as labels and double1 and double2 are verticle lines. The x-axis must have a scale of 1-5.

    Any help is greatly appreciated.

    Kind regards,
    Elizabeth

  • #2
    Something like?

    Code:
    clear
    input str1 name double(var1 var2)
    "A" 2.9 4.9
    "B" 3.13 4.25
    "C" 1.2 2.9
    "D" 1.3 3.3
    end
    
    reshape long var, i(name) j(which)
    encode name, gen(y)
    scatter y var, connect(L) ylab(, val) ytitle("") xtitle("")
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	21.8 KB
ID:	1751789

    Comment


    • #3
      Close. Can the horizontal lines be so that they are verticle? So it's like D connects to C and C connects to B and so on.

      Comment


      • #4
        Ah! See this recent thread: https://www.statalist.org/forums/for...and-after-plot

        Comment


        • #5
          And the y axis must have all of the labels. This is awesome! Thank you!

          Comment


          • #6
            I checked out the link you provided. It seems like what you have here is much closer. Is there a command that forces the lines to connect vertically? So it is apparent that there are two different variables?

            Comment


            • #7
              I attached the plot that your code created, and I drew over it to demonstrate my request.
              Attached Files

              Comment


              • #8
                Here I use Andrew Musau's example.

                Code:
                clear
                input str1 name double(var1 var2)
                "A" 2.9 4.9
                "B" 3.13 4.25
                "C" 1.2 2.9
                "D" 1.3 3.3
                end
                
                gen Y = _n 
                
                * BEGIN labmask from the Stata Journal is an alternative to this
                * labmask Y, values(name)
                local N = _N 
                
                forval y = 1/`N' { 
                    label def Y `y' "`=name[`y']'", add 
                }
                
                label val Y Y 
                * END 
                
                
                scatter Y var1 || scatter Y var2, yla(1/4, valuelabel) ysc(reverse) || line Y var1 || line Y var2 ///
                || rspike var1 var2 Y, horizontal legend(off)
                Click image for larger version

Name:	whatever3.png
Views:	1
Size:	41.1 KB
ID:	1751803

                Comment


                • #9
                  Hi Nick,

                  Thank you for this. How do you omit the horizontal lines so that it is just a vertical green and yellow line?

                  Comment


                  • #10
                    Just delete the last part of the code:

                    Code:
                    clear
                    input str1 name double(var1 var2)
                    "A" 2.9 4.9
                    "B" 3.13 4.25
                    "C" 1.2 2.9
                    "D" 1.3 3.3
                    end
                    
                    gen Y = _n 
                    
                    * BEGIN labmask from the Stata Journal is an alternative to this
                    * labmask Y, values(name)
                    local N = _N 
                    
                    forval y = 1/`N' { 
                        label def Y `y' "`=name[`y']'", add 
                    }
                    
                    label val Y Y 
                    * END 
                    
                    
                    scatter Y var1 || scatter Y var2, yla(1/4, valuelabel) ysc(reverse) || ///
                    line Y var1, lc(blue) || line Y var2, lc(red) legend(off)
                    Click image for larger version

Name:	Graph.png
Views:	1
Size:	30.9 KB
ID:	1751829

                    Comment


                    • #11
                      Code:
                      encode name, gen(y) label(y) 
                      
                      tw (connect y var1)(connect y var2), ///
                          ysc(rev) ylab(,valuelab) ytit("") leg(off)

                      Comment

                      Working...
                      X