Announcement

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

  • Rotating category axis labels and reversing scale using catplot

    Hi everyone,

    I am using catplot from SSC in Stata 17 on Windows. I am using the following code to create a stacked barplot over several variables:

    Code:
    *Stacked bar plot of all antibiotic variables
    local i =1
    local ll " yscale(off)"
    foreach v of var ampiamox penicillin streptomycin tetracycline cephalexin cotrimoxazole augmentin doxycycline erythromycin ciprofloxacin colistin {
    if `i'==11 loc ll "ylab(0(10)100)"
    catplot `v', percent stack asyvars name(g`i', replace) ytit("") blabel(bar, pos(center) format(%2.1f) size(small)) var1opts(label(ang(45))) legend(cols(3) size(tiny))  `ll'
    local `++i'
    }
    
    *Combine graphs with single legend
    grc1leg g1 g2 g3 g4 g5 g6 g7 g8 g9 g10 g11, cols(1) imargin(zero) ycommon xcommon legendfrom(g1) ysize(7) xsize(7)

    The figure looks as below (sorry I can't get a good size, medium is too small and large is too large). I would like to rotate the category axis labels so that they are horizontal. Many of the examples I have seen on Statalist seem to do that automatically, so I'm not sure why this is not happening. I added
    Code:
    var1opts(label(ang(45)))
    to try and do this, but it doesn't do anything. I would also like to reverse the yscale so that "mentioned spontaneously" appears on the left of the plot and "don't know on the right. Is this possible? Or are there other plot types that could be used to do something like this?

    Thanks,

    Sonia


    Click image for larger version

Name:	Figure_3_antibiotics.png
Views:	5
Size:	213.6 KB
ID:	1648015
    Attached Files

  • #2
    You have 11 antibiotics and 3 kinds of mention. I would reshape so that you have 2 variables, not 11. Then draw one graph.

    I faked some data. I tend to use tabplot from the Stata Journal much more than catplot from SSC, knowing that the authors won't be offended. With tabplot there is usually a little bit of tweaking needed if you want to show percents too.


    Code:
    clear
    set obs 100
    set seed 2803
    
    forval j = 1/11 {
        local p1 = `j' / 40
        local p2 = 2 * `p1'
        gen mention`j' = cond(runiform() < `p1', 1, cond(runiform() < `p2', 2, 3))
        local ++j
    }
    
    gen id = _n
    
    reshape long mention, i(id) j(antibiotic)  
    
    label def mention 1 "none" 2 "on probing" 3 "spontaneous"
    label val mention mention
    
    label def antibiotic 1 ampiamox 2 penicillin 3 streptomycin 4 tetracycline 5 cephalexin ///
    6 cotrimoxazole 7 augmentin 8 doxycycline 9 erythromycin 10 ciprofloxacin 11 colistin
    label val antibiotic antibiotic
    
    set scheme s1color
    
    tabplot antibiotic mention, showval(offset(0.3)) xsc(r(0.8 .)) percent(antibiotic) horizontal sep(mention) name(G1, replace) subtitle(percent)
    
    catplot mention antibiotic , name(G2, replace) percent(antibiotic) asyvars stack legend(row(1))
    Here the antibiotics are in some kind of order because the data were created that way. In other circumstances the trickery at https://www.stata-journal.com/articl...article=st0654 may help.


    Click image for larger version

Name:	antibiotic1.png
Views:	1
Size:	29.8 KB
ID:	1648028

    Click image for larger version

Name:	antibiotic_G2.png
Views:	1
Size:	22.8 KB
ID:	1648029

    Comment


    • #3
      That's great! I didn't think of reshaping, it works really well. I used xreverse with tabplot to get spontaneous on the left. Is there a way to do that with catplot? For both plots, is there a way of sorting so that bars are presented in descending order of proportion spontaneous?

      Thanks,

      Sonia

      Comment


      • #4
        I would just reverse the coding. and use the resulting variable.

        Code:
        . gen mention2 = 4 - mention
        
        . label def mention2 1 spontaneous 2 "on probing" 3 none
        
        . label val mention2 mention2

        Comment


        • #5
          Thanks Nick! That was an easier way to solve it.

          Comment

          Working...
          X