Announcement

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

  • bar graph excluding bar representing missing values

    Dear community,

    I am wondering how one can "exclude" a certain bar from showing without selecting the "hide bar" Option while editing? I want the missing values to be factored in while calculating the height of the bar but don't want to have an additional category on the x-axis that Shows how many values had been missing Overall. So far I tried nofill which sort of excludes all of the missing values when calculating the height of the bars which renders them larger than they truly are...

    Thank you!

    Marie

  • #2
    No code, no data example. We can't even tell whether you are using graph bar (or graph hbar) or twoway bar (or twoway rbar) and what the bars show. Some guesses are easier than others, but better, please, to tell us more. Please read and act on FAQ Advice #12.

    Comment


    • #3
      Of Course, sorry! I won't forget to show some code and the commands I used again: First of all here are some of the relevant variables. For e.g. facilityid 11 there are 10 observations for different years with a binary identifer that indicates whether a loan was granted by an indivdual with political connections or not.
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(years valid_con_mediocre) double facilityid
      1992 0 11
         0 0 11
         0 0 11
      1988 0 11
         0 0 11
         0 0 11
      1990 1 11
         0 0 11
         0 0 11
      1994 0 11
      end
      ------------------ copy up to and including the previous line ------------------

      I used the following command:

      graph bar (percent) valid_con_mediocre if dup2<2, over(levels_new, relabel(1 "Other" 2 "CXO/President" 3 "Vice President" 4 "Chairman" 5 "Director" 6 "Secretary")) over(nine_eight_two) asyvars stack ytitle(PolCon [%]) title(Graph 2: Distribution of PolCon loans by corporate role [1982-2000], size(medium)).

      Currently, the x-axis is showing all the years (which is correct) but also a "0" bar which I would like to discard. If I add "nofill" at the end of the command the height of the bars in percent is distorted as all the "0" are disregarded. I do want them to weigh in but I simply don't want them to be represented by an additional bar in my graph. How could I achieve that?

      Thanks in Advance!

      Marie
      Last edited by Mascha Mascha; 08 Jun 2019, 15:03.

      Comment


      • #4
        Your data example doesn't allow your command to be reproduced. Variables

        Code:
        dup2 levels_new  nine_eight_two
        are nowhere to be seen in the data example, while

        Code:
        years facilityid
        don't appear in the code. There is a happy medium between too little information (#1) and too much that still isn't clear (#3)! So, this isn't working well. Let's try a different approach. The flavour of your question is, I think, that you want percent calculations to be done for all categories but the graph to show only some of them. To get that, you need to calculate what you want to show before you ask for a graph. Here is a reproducible example:


        Code:
        sysuse auto, clear  
        bysort foreign : gen freq = _N  
        gen toshow = 100 * freq/_N  
        tabdisp foreign, c(toshow)  
        set scheme s1color  
        graph bar (mean) toshow if foreign == 0, over(foreign) ytitle(percent of total)
        Note that
        (mean) here is just a device to stop graph bar complaining. The (mean) is the mean over a constant within each group calculated in advance. (asis) could be made to work, but would need another trick.
        Last edited by Nick Cox; 09 Jun 2019, 06:46.

        Comment


        • #5
          Dear Nick,

          thank you for the previous answer. May I ask a follow-up question?

          The problem is the following: If we would like to show the share of cars that had 3 repairs over a variety of dummies and categorical variables (say in the example foreign, headroom and trunk) , while taking the missing values into account, something like this would work:

          Code:
          foreach var of varlist foreign headroom trunk {
          graph bar, over(rep78) over(`var') missing blabel(bar, format(%5.2f) size(tiny)) title("Graph `var')
          }
          If we now only want the percentage shares of 3 repairs per value of a given variable per graph be shown, how could we do that?

          Thanks in advance!
          Luca
          Last edited by luca luca; 14 Jul 2020, 07:35.

          Comment


          • #6
            Repair record in the auto data is a grade for quality and not a count of number of repairs. Still, that is trivial here. I think the answer is as in #4: you need to calculate what you want to show before you ask for a graph. So for example I think you are asking for something like

            Code:
            . sysuse auto , clear
            (1978 Automobile Data)
            
            . egen toshow = mean(100 * (rep78 == 3)), by(foreign)
            
            .  egen tag = tag(foreign)
            
            . graph bar (asis) toshow if tag, over(foreign) title(% of Repair record grade 3)

            Comment

            Working...
            X