Announcement

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

  • scatterplot with text labels for y and x?

    Dear All, I'd like to have a figure like below (need not to be exactly the same).

    Click image for larger version

Name:	topic.png
Views:	1
Size:	6.4 KB
ID:	1545164


    I make up the following data (may not be complete, but I do not know what else data need to be provided).
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str20(y x) str10 markers
    "CEO"      "Co-opt" "a"
    "Dividend" "Co-opt" "b"
    "Leverage" "Co-opt" "c"
    "CEO"      "SP"     "d"
    "Dividend" "SP"     "e"
    "Leverage" "SP"     "f"
    end
    Any suggestions are highly appreciated. Thanks.
    Ho-Chuan (River) Huang
    Stata 19.0, MP(4)

  • #2
    You need values underlying the labels. Below I use labmask from Stata Journal, authored by Nick Cox.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str20(y x) str10 markers
    "CEO"      "Co-opt" "a"
    "Dividend" "Co-opt" "b"
    "Leverage" "Co-opt" "c"
    "CEO"      "SP"     "d"
    "Dividend" "SP"     "e"
    "Leverage" "SP"     "f"
    end
    
    gen X= cond(x=="Co-opt", 3, 6)
    encode y, gen(Y)
    *findit labmask and click link to install
    labmask X, values(x)
    
    scatter Y X, xlab(0 " " 1 " " 2 " " 3 4 " " 5 " " 6 7" " 8 " " 9 " ", ///
    val grid) xsc(r(0 9)) ylab(0 " " 1 2 3 4 " " 5 " ", val grid ///
    angle(0)) ysc(r(0 5)) scheme(s1color) ytitle("") xtitle("")
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	17.1 KB
ID:	1545179

    Last edited by Andrew Musau; 07 Apr 2020, 04:31.

    Comment


    • #3
      Dear Andrew, This suggestion is great. BTY, I wonder if the six circles can be replaced by the corresponding `marker's (the last variable in my data)?
      Ho-Chuan (River) Huang
      Stata 19.0, MP(4)

      Comment


      • #4
        Yes, just a slight addition - highlighted in red below.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str20(y x) str10 markers
        "CEO"      "Co-opt" "a"
        "Dividend" "Co-opt" "b"
        "Leverage" "Co-opt" "c"
        "CEO"      "SP"     "d"
        "Dividend" "SP"     "e"
        "Leverage" "SP"     "f"
        end
        
        gen X= cond(x=="Co-opt", 3, 6)
        encode y, gen(Y)
        *findit labmask and click link to install
        labmask X, values(x)
        
        scatter Y X, xlab(0 " " 1 " " 2 " " 3 4 " " 5 " " 6 7" " 8 " " 9 " ", ///
        val grid) xsc(r(0 9)) ylab(0 " " 1 2 3 4 " " 5 " ", val grid ///
        angle(0)) ysc(r(0 5)) scheme(s1color) ytitle("") xtitle("") mcolor(none) ///
        mlab(markers) mlabpos(0)
        Click image for larger version

Name:	Graph.png
Views:	1
Size:	16.8 KB
ID:	1545183

        Comment


        • #5
          Dear Andrew, Since I am not so familiar with graph options. Could you kindly give me another suggestions for the following (extended) data? Now, I add another element "CSR" in the x axis (Also, another y element, i.e., Crash).
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str20(y x) str10 markers
          "CEO"      "Co-opt" "a"
          "Dividend" "Co-opt" "b"
          "Leverage" "Co-opt" "c"
          "CEO"      "SP"     "d"
          "Dividend" "SP"     "e"
          "Leverage" "SP"     "f"
          "CEO"      "CSR"    "g"
          "Dividend" "CSR"    "h"
          "Leverage" "CSR"    "i"
          "Crash"    "CSR"    "j"
          end
          I'd like to learn the ideas from your further codes. Thanks a lot.
          Ho-Chuan (River) Huang
          Stata 19.0, MP(4)

          Comment


          • #6
            Here, I am just labeling the borders of the rectangle. If you need the inner points, fill in the missing combinations.

            Code:
            clear
            input str20(y x) str10 markers
            "CEO"      "Co-opt" "a"
            "Dividend" "Co-opt" "b"
            "Leverage" "Co-opt" "c"
            "Crash"    "Co-opt" "d"
            "CEO"      "SP"     "e"
            "Crash"    "SP"     "f"
            "CEO"      "CSR"    "g"
            "Dividend" "CSR"    "h"
            "Leverage" "CSR"    "i"
            "Crash"    "CSR"    "j"
            end
            
            gen X= cond(x=="Co-opt", 3, cond(x=="SP", 6, 9))
            gen Y= cond(y=="CEO", 1, cond(y=="Dividend", 2, cond(y=="Leverage", 3, 4)))
            *findit labmask and click link to install
            labmask X, values(x)
            labmask Y, values(y)
            
            scatter Y X, xlab(0 " " 1 " " 2 " " 3 4 " " 5 " " 6 7" " 8 " " 9 ///
            10 " " 11 " ", val grid) xsc(r(0 11)) ylab(0 " " 1 2 3 4  5 " " 6 " ", ///
             val grid angle(0)) ysc(r(0 6)) scheme(s1color) ytitle("") xtitle("") ///
             mcolor(none) mlab(markers) mlabpos(0) mlabsize(large)
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	21.1 KB
ID:	1545189

            Comment


            • #7
              Dear Andrew, Thanks a lot. I will try to understand your code.
              Ho-Chuan (River) Huang
              Stata 19.0, MP(4)

              Comment


              • #8
                Dear Andrew, For unknown reason, the label of y becomes incomplete. Could you have a look? Thanks.
                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input float id str40 y str15(x markers)
                 0 "CEO turnover-performance sensitivity" "Co-option" "CDN14rfs"  
                 1 "CEO pay level"                        "Co-option" "CDN14rfs"  
                 2 "CEO pay-performance sensitivity"      "Co-option" "CDN14rfs"  
                 3 "Investment"                           "Co-option" "CDN14rfs"  
                 4 "Dividend"                             "Co-option" "JL18fm"    
                 5 "Takeover"                             "Co-option" "?"         
                 6 "Capital structure"                    "Co-option" "?"         
                 7 "Cash holdings"                        "Co-option" "KLHF20wp"  
                 8 "Hedging"                              "Co-option" "?"         
                 9 "Institutional ownership"              "Co-option" "?"        
                10 "Tobin's q"                            "Co-option" "?"         
                11 "M&A"                                  "Co-option" "?"       
                12 "Crash risk"                           "Co-option" "KHFL20rfqa"
                13 "Synchronicity"                        "Co-option" "?"       
                14 "Investment (efficiency)"              "Co-option" "?"         
                end
                
                label define X 3 "Co-option" 6 "SP" 9 "CSR"
                encode x, gen(X) 
                label define Y  0 "CEO turnover-performance sensitivity" 1 "CEO pay level" 2 "CEO pay-performance sensitivity" 3 "Investment" ///
                4 "Dividend" 5 "Takeover" 6 "Capital structure" 7 "Cash holdings" 8 "Hedging" 9 "Institutional ownership" 10 "Tobin's q" ///
                11 "M&A" 12 "Crash risk" 13 "Synchronicity" 14 "Investment (efficiency)" 
                encode y, gen(Y) 
                *gen X= cond(x=="Co-option", 3, cond(x=="SP", 6, 9))
                *gen Y= cond(y=="CEO", 1, cond(y=="Dividend", 2, cond(y=="Leverage", 3, 4)))
                *findit labmask and click link to install
                labmask X, values(x)
                labmask Y, values(y)
                
                scatter Y X, ytitle("") xtitle("") mcolor(none) mlab(markers) mlabpos(0) mlabsize(small) ///
                xlab(0 " " 1 " " 2 " " 3 4 " " 5 " " 6 7 " " 8 " " 9 10 " " 11 " ", val grid) xsc(r(0 11)) ///
                ylab(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14, val grid angle(0)) ysc(r(0 14)) scale(0.5)
                Click image for larger version

Name:	ylab.png
Views:	1
Size:	71.0 KB
ID:	1546094
                Ho-Chuan (River) Huang
                Stata 19.0, MP(4)

                Comment


                • #9
                  If you look at your variable "markers", it has some entries "?" for some observations. If you want to fill in downwards (as the first 4 observations have the same labels), you can add the following at the beginning of the code.

                  Code:
                  replace markers = markers[_n-1] if markers=="?"

                  Comment


                  • #10
                    Dear Andrew, I guess that I did not make myself clear. What I asked is that the labels on the vertical (y) axis are incomplete. Any suggestions? Thanks.
                    Ho-Chuan (River) Huang
                    Stata 19.0, MP(4)

                    Comment


                    • #11
                      You mean how the graph is rendered? You can increase the margin outside the y-axis title but your y-axis labels are too long. Adding both

                      Code:
                      ylab(0 (1) 14, val labsize(small) grid angle(0)) ysc(r(0 14) outergap(40))
                      works well in your example. I will suggest that you shorten your labels or introduce line-breaks in case you need a bigger plot area. Note that you don't need to specify all the values within ylab(), the way I put it indicates that increments are in units of 1.

                      Comment


                      • #12
                        Dear Andrew, Would you be kind enough to give another sample for using "line-breaks" in my case? I have no experience in this situation. Thanks again.
                        Ho-Chuan (River) Huang
                        Stata 19.0, MP(4)

                        Comment


                        • #13
                          I can illustrate for one label: After running the code in #10 (with corrections from #11)

                          Code:
                          replace y = `""CEO turnover-" "performance sensitivity""' in 1
                          lab drop Y
                          labmask Y, values(y)
                          scatter Y X, ytitle("") xtitle("") mcolor(none) mlab(markers) mlabpos(0) mlabsize(small) ///
                          xlab(0 " " 1 " " 2 " " 3 4 " " 5 " " 6 7 " " 8 " " 9 10 " " 11 " ", val grid) xsc(r(0 11)) ///
                          ylab(0 (1) 14, val labsize(small) grid angle(0)) ysc(r(0 14) outergap(40)) scale(0.5)
                          The idea is each line is enclosed within double quotes.

                          Comment


                          • #14
                            Dear Andrew, Thanks a lot, It works well.
                            Ho-Chuan (River) Huang
                            Stata 19.0, MP(4)

                            Comment

                            Working...
                            X