Announcement

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

  • How to use catplot command properly in a panel setting?

    Hi,

    I am trying to use "catplot" to plot a survey result about trust on a scale of 1 to 5, where 1 = not trust at all and 5 = fully trust. My data looks like this, where "entity" and "TrustScore" are variable names.
    Entity TrustScore
    President 1
    President 2
    President 3
    President 5
    President 5
    Congress 5
    Congress 1
    Congress 2
    Congress 3
    Congress 4
    Congress 5
    Usfirm 1
    Usfirm 1
    Usfirm 1
    Usfirm 2
    Usfirm 2
    Usfirm 2
    Usfirm 3
    Usfirm 3
    Usfirm 3
    Usfirm 4
    Usfirm 4
    Usfirm 1
    Usfirm 1
    Usfirm 1
    Usfirm 1
    Using the above data, I want to have a figure where each row is about one entity, and the bar shows the relative frequency of each categorical value with a different color. I was able to generate the figure that I wanted using the code below. However, this code now generates an error message "varlist not allowed". I think maybe the syntax for "catplot" changed? I would highly appreciate it if someone can help me.


    catplot TrustScore Entity, asyvars stack percent(Entity) var3opts(label(labsize(vsmall))) ytitle(Percent) ///
    bar(1, color(blue) fintensity(inten100)) ///
    bar(2, color(blue) fintensity(inten40)) ///
    bar(3, color(dimgray) fintensity(inten100)) ///
    bar(4, color(red) fintensity(inten40)) ///
    bar(5, color(red) fintensity(inten100)) ///
    legend(size(small)) ///
    name(a_expret_fig, replace)

    Last edited by Charlie Smith; 29 Nov 2024, 10:12. Reason: catplot

  • #2
    catplot is from SSC (FAQ Advice #12: please tell us the provenance of community-contributed commands you refer to).

    Indeed. The syntax has changed, as discussed at https://www.statalist.org/forums/for...tegorical-data

    Start quickly with #2 in that thread then read #1.

    Reading https://www.statalist.org/forums/for...to-have-broken is optional and is a roundabout route to the same point.

    From one point of view I should not have changed the syntax, so sorry for the inconvenience. From another it's surprising that people puzzled about why a command doesn't work don't check the help file. That is, you must have downloaded the new files, although perhaps that was part of a general update.

    That said, your code shows that you were making some text labels vsmall, and why do that?

    This is a rewriting of your data example and your catplot code with some small changes. I also show code for tabplot from the Stata Journal. As author of both commands, I am without bias, but tend to prefer results from the latter.

    Stacking bars is to my mind a greatly over-rated device, bringing into being a legend or key that obliges mental back and forth; often making amounts difficult to compare; and even making zeros or very small amounts unnecessarily hard to spot. The stacked design also makes it harder to add annotation legibly.

    Clearly you're just showing a fragment of your full dataset. Opinions might shift a little given results for all your data.

    Code:
    clear 
    input str9 Entity    TrustScore
    President    1
    President    2
    President    3
    President    5
    President    5
    Congress    5
    Congress    1
    Congress    2
    Congress    3
    Congress    4
    Congress    5
    Usfirm    1
    Usfirm    1
    Usfirm    1
    Usfirm    2
    Usfirm    2
    Usfirm    2
    Usfirm    3
    Usfirm    3
    Usfirm    3
    Usfirm    4
    Usfirm    4
    Usfirm    1
    Usfirm    1
    Usfirm    1
    Usfirm    1
    end 
    
    catplot, over(TrustScore) over(Entity) asyvars stack percent(Entity) ytitle(Percent) ///
    bar(1, color(blue) fintensity(inten100)) ///
    bar(2, color(blue) fintensity(inten40)) ///
    bar(3, color(dimgray) fintensity(inten100)) ///
    bar(4, color(red) fintensity(inten40)) ///
    bar(5, color(red) fintensity(inten100)) ///
    legend(row(1)) name(G1, replace)
    
    tabplot TrustScore Entity, separate(TrustScore) percent(Entity) subtitle(Percent) /// 
    bar1(color(blue) fintensity(inten100)) ///
    bar2(color(blue) fintensity(inten40)) ///
    bar3(fcolor(dimgray) lcolor(gray) fintensity(inten100)) ///
    bar4(color(red) fintensity(inten40)) ///
    bar5(color(red) fintensity(inten100)) showval yreverse name(G2, replace)
    Click image for larger version

Name:	charlie_G1.png
Views:	1
Size:	20.5 KB
ID:	1768507
    Click image for larger version

Name:	charlie_G2.png
Views:	1
Size:	19.1 KB
ID:	1768508


    Comment


    • #3
      Dear Nick,

      Thank you so much for your reply!

      This code works very well. I should've checked previous threads thoroughly.

      I couldn't find...

      Thanks again for writing this awesome code and also replying to my question.

      Best,
      Charlie

      Comment

      Working...
      X