Announcement

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

  • Producing Cleveland dot plot lines between two y and x values in a scatter plot

    Hi!

    I would like to produce a cleveland dot plot type of graph with lines that connects each x values for every given y, like this one: https://uc-r.github.io/public/images...plots/icon.png

    In addition to this, I would like to attach the labels of the y variable to each value on the y axis.

    Does the silly example data from the code below need any workaround or is there a simple way to connect the scatter dots by adding another type of twoway graph in addition to the scatter plots?

    My first post on Statalist so please let me know if something is dissatisfying with my post.

    Thanks!
    /Anton

    Code:
    sysuse auto, clear
    
    egen rank_price = rank(price)
    
    labmask rank_price, values(make)
    
    gsort rank_price
    
    gen price3 = round(price/1.75/100)
    
    gen price1 = round(price/100)
    
    // Graph
    twoway (scatter rank_price price1 if rank_price <= 10, mlabel(price1) msize(vlarge) mlabpos(0) mlabtextstyle(color(black))) ///
           (scatter rank_price price3 if rank_price <= 10, mlabel(price3)  msize(vlarge) mlabpos(0) mlabtextstyle(color(black))), ///
           xlabel(0(5)45) ///
           title("Cleveland Dot Plot")

  • #2
    The sort below is just to connect adjacent markers. It may not be what you want.

    Code:
    sysuse auto, clear
    
    egen rank_price = rank(price)
    
    labmask rank_price, values(make)
    
    gsort rank_price
    
    gen Price3 = round(price/1.75/100)
    
    gen Price1 = round(price/100)
    
    keep in 1/10
    gen order=_n
    reshape long Price, i(make) j(which)
    sort order Price 
    
    
    // Graph
    twoway (scatter rank_price Price if which==1, mlabel(Price) msize(vlarge) mlabpos(0) mlabtextstyle(color(black))) ///
           (scatter rank_price Price if which==3, mlabel(Price)  msize(vlarge) mlabpos(0) mlabtextstyle(color(black))) ///
           (scatter rank_price Price, connect(L) lc(black) msy(none)), ///
           xlabel(0(5)45) leg(off) ///
           title("Cleveland Dot Plot")
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	32.4 KB
ID:	1754106

    Comment


    • #3
      See also https://journals.sagepub.com/doi/pdf...867X0900900408

      Comment


      • #4
        This is exactly what I am looking for. Thanks Andrew!

        Thanks for the read Nick, will look into it.

        /Anton

        Comment

        Working...
        X