Announcement

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

  • How to remove labels on just one subplot



    I'm trying to plot two side-by-side horizontal bar charts and am hoping to exclude the labels on the second subplot. Here's what I have so far, using an example dataset:

    clear
    input str16(category region) float(cnt1 cnt2 cnt3 cnt4) int(sorter)
    "eventtype1" "east" 40 21.82 10 1 1
    "eventtype2" "east" 34.5 1.82 8 1 2
    "eventtype3" "east" 12 14.82 3 1 4
    "eventtype4" "east" 47 17.82 4 1 3
    "eventtype1" "west" 50 21.82 1 4 1
    "eventtype2" "west" 24.5 3.82 8 5 2
    "eventtype3" "west" 32 11.82 3 12 4
    "eventtype4" "west" 27 27.82 4 1 3
    end

    graph hbar (asis) cnt1 cnt2 cnt3 cnt4, by(region) ///
    over(category, sort(sorter) label(labsize(medium))) ///
    stack graphregion(color(white)) cw ///
    ysize(8) xsize(15) ///
    ytitle("",size(medsmall)) ylab(, labsize(medium)) ///
    bar(1, color(edkblue)) bar(2, color(emidblue)) ///
    bar(3, color(ebblue)) bar(4, color(eltblue)) ///
    legend(order(1 "characteristic 1" 2 "characteristic 2" ///
    3 "characteristic 3" 4 "characteristic 4") size(medium) ///
    position(5) col(2) region(lstyle(none) fcol(white) lwidth(none))) ///
    name(g2, replace)

    I would like to remove all of the "eventtype*" labels from the second subplot.

    Click image for larger version

Name:	plot1.PNG
Views:	1
Size:	38.1 KB
ID:	1670844

  • #2
    Hi Krista,

    Just to clarify, do you need to do this programmatically for some reason, like you want to be able to reproduce the procedure exactly at a later date, you want to be able to send the code to a colleague, or because you want a general way to make plots like this?

    Otherwise this is easy to do in the graph editor. Run the code you have so far and click "start graph editor" at the top of the window that opens. Select the element containing the second set of bar labels with left click. Now, right click the element and select "hide" from the tooltip menu.

    Comment


    • #3
      I don't have an answer to the specific question in #1.

      I have a comment on the stacked design, which is popular but in my view oversold. One of several defects is that small amounts can be hard work to discern, requiring a lot of back and forth between the legend and the short bars implied. I often suggest a different design.

      There could be a careful rationale for your colours, but type 4 which is often rare surely needs a stronger colour.

      tabplot is from the Stata Journal. For an overview, see https://www.statalist.org/forums/for...updated-on-ssc

      mycolours is from SSC. For an overview, see https://www.statalist.org/forums/for...ailable-on-ssc


      Code:
      clear
      input str16(category region) float(cnt1 cnt2 cnt3 cnt4) int(sorter)
      "eventtype1" "east" 40 21.82 10 1 1
      "eventtype2" "east" 34.5 1.82 8 1 2
      "eventtype3" "east" 12 14.82 3 1 4    
      "eventtype4" "east" 47 17.82 4 1 3
      "eventtype1" "west" 50 21.82 1 4 1
      "eventtype2" "west" 24.5 3.82 8 5 2
      "eventtype3" "west" 32 11.82 3 12 4
      "eventtype4" "west" 27 27.82 4 1 3
      end
      
      set scheme s1color 
      
      reshape long cnt , i(sorter region) j(characteristic)
      
      list 
      
      label def sorter 3 "4" 4 "3"
      label val sorter sorter 
      label var sorter "type"
      
      * from Stata Journal 
      tabplot characteristic sorter [iw=cnt], by(region, note("")) separate(characteristic) bar1(color(edkblue)) bar2(color(emidblue)) ///
      bar3(color(ebblue)) bar4(color(eltblue)) showval name(G1, replace)
      
      * ssc install mycolours 
      mycolours 
      
      tabplot characteristic sorter [iw=cnt], by(region, note("")) separate(characteristic) bar1(color("`OK1'")) bar2(color("`OK2'")) ///
      bar3(color("`OK3'")) bar4(color("`OK8'")) showval name(G2, replace)

      Click image for larger version

Name:	notstacked.png
Views:	1
Size:	21.7 KB
ID:	1670860

      Comment


      • #4
        Thank you, both! I don't often think to use the graph editor -- making this change manually would be fine for my case.

        Nick, I agree that the plot is difficult to read, especially for the rare categories. Thanks for the suggestion!

        Comment

        Working...
        X