Announcement

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

  • Catplot - manual sort

    Hello,

    I am struggling to sort my catplot for my variable CCR_adjusted.
    I know that with catplot I can sort by ascending and descending, but I want a certain order (Finance practice): CC, CCC-, CCC, CCC+, B-, B, B+, BB-, BB, BB+, BBB-, BBB, BBB+ (if this is not within the data set then still show the label).
    I read that I may have to do it with egen = group() and define my variables. But the problem is that the data values of my variable are the different labels. Do I have to create new variables for each of the possible values and plot them then? How would that look like?

    My code:
    Code:
    catplot CCR_adjusted, percent var1opts(sort(1) descending) recast(bar)
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 CCR_adjusted
    "B"  
    "BB-"
    "B+"  
    "B"  
    "B"  
    "B"  
    "B+"  
    "B"  
    "B"  
    "B-"  
    "B-"  
    "B-"  
    "B"  
    "B"  
    "B"  
    "BB-"
    "B"  
    "B"  
    "B"  
    "B"  
    "B"  
    "CCC+"
    "B"  
    "B+"  
    "B+"  
    "B"  
    "B"  
    "B"  
    "B"  
    "BB-"
    "BB-"
    "B+"  
    "BB+"
    "B+"  
    "BB+"
    "B+"  
    "B"  
    "B+"  
    "BB-"
    "B"  
    "B"  
    "B"  
    "B"  
    "B+"  
    "B+"  
    "BB-"
    "B"  
    "B-"  
    "BB"  
    "BB-"
    "B-"  
    "B"  
    "BB-"
    "B"  
    "BB-"
    "B"  
    "BB-"
    "B"  
    "B"  
    "BB+"
    "B"  
    "B+"  
    "BB-"
    "BB+"
    "B+"  
    "B"  
    "B-"  
    "B+"  
    "BB-"
    "BB"  
    "B-"  
    "B-"  
    "B"  
    "B"  
    "B+"  
    "B"  
    "BB"  
    "BB"  
    "B"  
    "B+"  
    "BB"  
    "B"  
    "B+"  
    "B+"  
    "B+"  
    "BB"  
    "BB+"
    "BBB-"
    "BB-"
    "B"  
    "B"  
    "B"  
    "BB+"
    "B"  
    "BB-"
    "BB+"
    "BBB-"
    "B"  
    "BB"  
    "B"  
    end

  • #2
    catplot is from SSC, authored by Nick Cox (as you are asked to explain). Stata has no clue what "finance practice" is, so you have to specify this. Create a variable defining the order (named "order" in the code below).

    Code:
    catplot CCR_adjusted, percent var1opts(sort(order)) recast(bar)
    Last edited by Andrew Musau; 12 Feb 2020, 06:15.

    Comment


    • #3
      Something like this:

      Code:
      clear * 
      
      local k = 1
      foreach K in CC  CCC- CCC CCC+  B-  B  B+  BB- BB BB+ BBB- BBB BBB+ { 
          label def CCR `k' "`K'", modify 
          local ++k 
      }
      
      label li 
      
      input str9 CCR_adjusted
      "B"  
      "BB-"
      "B+"  
      "B"  
      "B"  
      "B"  
      "B+"  
      "B"  
      "B"  
      "B-"  
      "B-"  
      "B-"  
      "B"  
      "B"  
      "B"  
      "BB-"
      "B"  
      "B"  
      "B"  
      "B"  
      "B"  
      "CCC+"
      "B"  
      "B+"  
      "B+"  
      "B"  
      "B"  
      "B"  
      "B"  
      "BB-"
      "BB-"
      "B+"  
      "BB+"
      "B+"  
      "BB+"
      "B+"  
      "B"  
      "B+"  
      "BB-"
      "B"  
      "B"  
      "B"  
      "B"  
      "B+"  
      "B+"  
      "BB-"
      "B"  
      "B-"  
      "BB"  
      "BB-"
      "B-"  
      "B"  
      "BB-"
      "B"  
      "BB-"
      "B"  
      "BB-"
      "B"  
      "B"  
      "BB+"
      "B"  
      "B+"  
      "BB-"
      "BB+"
      "B+"  
      "B"  
      "B-"  
      "B+"  
      "BB-"
      "BB"  
      "B-"  
      "B-"  
      "B"  
      "B"  
      "B+"  
      "B"  
      "BB"  
      "BB"  
      "B"  
      "B+"  
      "BB"  
      "B"  
      "B+"  
      "B+"  
      "B+"  
      "BB"  
      "BB+"
      "BBB-"
      "BB-"
      "B"  
      "B"  
      "B"  
      "BB+"
      "B"  
      "BB-"
      "BB+"
      "BBB-"
      "B"  
      "BB"  
      "B"  
      end
          
      encode CCR_adjusted, gen(CCR) label(CCR) 
          
      catplot CCR

      Comment


      • #4
        Could you explain me, why I have to use encode? I understood that I have to create a label list, but how does Stata know that it has to assign the frequency to the right label?
        Would there be a different way without catplot/ easier way or is catplot always used? Sorry, I am new to Stata.

        Comment


        • #5
          Well, you want to get strings that aren't properly regarded in alphabetical order into a different order. I can't think of a way to do that without using a numeric variable with value labels attached. I can think of ways of doing that without using encode, but if you are new to Stata, I can assure you that those I can think of now are more complicated.

          Is catplot always used? No, even for this purpose. You can use anything else you like.

          Comment


          • #6
            To directly use an ordering variable as suggested in #2

            Code:
            gen order =.
            local i=1
            foreach K in CC  CCC- CCC CCC+  B-  B  B+  BB- BB BB+ BBB- BBB BBB+ {
                replace order= `i' if CCR_adjusted=="`K'"
                local ++i
            }
            catplot CCR_adjusted, percent var1opts(sort(order)) recast(bar) hor
            Last edited by Andrew Musau; 12 Feb 2020, 09:23.

            Comment


            • #7
              Thank you for explaining. Unrelated questions: Can I output two seperate catplot graphs in one export so both graphs are next to each other? Currently when I catplot one and the next command is also catplot it overwrites the previous graph and shows me it. I scanned through your publication but could not find any information.

              @Anrew: thank you. Your reply is more intuitive for me.
              Last edited by Philipp Grab; 12 Feb 2020, 09:34.

              Comment


              • #8
                Andrew Musau came up with a nice way to avoid encode here. It's no criticism to note that it is simple partly because the categorical labels happen to be "words" in Stata's sense. That's one reason why encode is more general. Also, the trick works here because you can plot using one variable and sort using another, but there are plenty of other problems where value labels are needed.

                I am not clear what "your publication" means here. I have published various things on Stata.

                Two catplots in one display would seem to yield to naming the graphs and using graph combine although depending on the details a by() option might be a neater way to double up the display.

                Comment

                Working...
                X