Announcement

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

  • displaying a certain value of an categorical variable in a bar graph

    Hi everyone, I've searched the manuals and forums here for an answer but to no avail. I was hoping there's someone who could answer my question:

    I created a bar graph to display the percentages of all values of a categorical variable (lets call it accident_type). accident_type contains 20 different values (disease, burns, violence, etc...) and I stratified accident_type over years 2010-2015. I used the following code:

    graph bar, over(accident_type) over(years)

    *note I had to use over(accident_type) because it's a string variable and the graph wouldn't let me use time-series operators, luckily over() fixes this problem

    But as you can imagine, displaying 20 values and stratified over 6 years over a bar graph can be quite squished and hard to read so I wanted to display only the value "violence" in accident_type. I used the following code:

    graph bar if accident_type=="violence", over(accident_type) over(years)

    However, this code recalculates the percentage of "violence" as if now I've created a subpopulation where I restricted the dataset to look only at those with "violence" in accident_type. For example, in the first graph "violence" in 2010 was perhaps 5%, and in the second graph "violence" in 2010 is now 25%. I didn't want to restrict the population and recalculate the proportions; I only wanted to restrict the graph to only show "violence". Is there a way to do this?

  • #2
    You don't give a data example to make your code reproducible, but I think I understand the problem. Note, however, that the first over() option is redundant if you restrict the graph to a single category.

    I have two solutions. First, note that the problem is just that the axis labels are too high by a factor you can calculate, so you just fix the axis labels. A helper program mylabels should be installed (just once) from SSC to aid in this.

    Second, calculate the percents directly and then show just those you want.

    Here's code for both:

    Code:
    sysuse auto, clear
    
    * the problem: the second graph should be using the percents from the first
    graph bar, over(foreign) over(rep78) name(G1, replace)
    count if !missing(foreign, rep78)
    local N1 = r(N)
    
    graph bar if foreign, over(rep78) name(G2, replace)
    count if foreign & !missing(rep78)
    local N2 = r(N)
    
    * first solution
    * install just once
    ssc inst mylabels
    mylabels 0(3)12, myscale(@*(`N1'/`N2')) local(labels)  
    graph bar if foreign, over(rep78) yla(`labels', ang(h)) name(G3, replace)
    
    * second solution
    contract foreign rep78 , percent(_percent) nomiss
    graph bar (asis) _percent if foreign, over(rep78) name(G4, replace) yla(0(3)12, ang(h))
    
    graph combine G1 G2 G3 G4
    A third solution would be to consider a different kind of graph e.g. http://www.statalist.org/forums/foru...updated-on-ssc
    Last edited by Nick Cox; 15 Nov 2016, 02:24.

    Comment


    • #3
      Hi Nick, thanks for your help. It's also an honor getting help from you; I've perused numerous help inquiries on google and statalist and you always seem to have the answers.
      Anyways, there seems to be one problem when I run it through my version of stata (version 14.1, revision at 30th March 2016). Because of reasons beyond my control, I cannot and I am unable to update stata to the latest version. I would like to use your first solution as it would be useful to generate codes for future graphs. Would this be an issue?

      Here's the error:

      mylabels 0(3)12, myscale(@*(`N1'/`N2')) local(labels) 'N1' invalid name
      r(198);

      Comment


      • #4
        I can't see that having 14.1 could possibly be a problem here for either solution.

        You should copy code exactly. Your error message implies that you typed identical similar quotes

        Code:
        'N1'
        whereas the code is right: you need different quote marks:

        Code:
        `N1'
        See the section in [U] introducing local macros.

        Comment


        • #5
          thanks Nick for all your help. Worked like a charm

          Comment

          Working...
          X