Announcement

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

  • Bar Graph: Show Category on x-axis with Zero Observations

    Hi there, hoping to get some assistance with showing 4 categories along an x-axis of a bar chart (count) when there are only observations for 3 of those categories in the data set. That is, show that the 4th category recorded no observations. At the moment, my code (see below) is such that only the 3 categories (solar=1, 2 or 3) are displaying and not the 4th (solar=0). I've tried using the "nofill" and "allcategories" commands with no luck. Any help is much appreciated! Cheers!

    gen solar = 0 if solar_string=="No Investment"
    replace solar = 1 if solar_string=="10kW"
    replace solar = 2 if solar_string=="20kW"
    replace solar = 3 if solar_string=="30kW"

    graph bar (count) solar, over(solar_string, sort(solar)) nofill

  • #2
    There is no way to know a missing category if it does not exist in the data. You need to collapse the data and then add the missing category (categories).

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str15 solar_string
    "10kW"
    "10kW"
    "20kW"
    "30kW"
    "30kW"
    "20kW"
    "10kW"
    "10kW"
    "30kW"
    end
    
    contract solar_string
    set obs `=_N+1'
    replace solar_string="No Investment" in `=_N'
    replace _freq=0 in `=_N'
    gr bar _freq, over(solar_string, sort(_freq)) scheme(s1mono) ytitle("Frequency") blab(total)
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	44.1 KB
ID:	1577253

    Comment


    • #3
      Thanks so much Andrew, that's a huge help. Can I ask what the "str15" means in your line of code: input str15 solar_string ? Cheers!

      Comment


      • #4
        I am making up example data (to create a fake sample of your data). str15 means a string variable with a maximum length of 15 characters.

        * Example generated by -dataex-.
        To install: ssc install dataex
        clear
        input str15 solar_string
        "10kW"
        "10kW"
        "20kW"
        "30kW"
        "30kW"
        "20kW"
        "10kW"
        "10kW"
        "30kW"
        end
        This part is not relevant to your code.

        Comment


        • #5
          An easier way to do this is to draw a histogram instead, which can be nothing but a bar chart showing frequencies.

          Here is a code sketch.


          Code:
          gen solar = -1 if solar_string=="No Investment"
          replace solar = 1 if solar_string=="10kW"
          replace solar = 2 if solar_string=="20kW"
          replace solar = 3 if solar_string=="30kW"
          
          label def solar -1 "No investment" 1 "10 kW" 2 "20 kW" 3 "30 kW"
          label val solar solar 
          
          histogram solar , width(1) freq xla(-1 1 2 3, valuelabel)
          Details: If this were my problem I might want to distinguish the nos as being qualitatively as well as quantitatively different. Clearly, this is optional.

          Standard publishing practice (and therefore good presentation practice) is to put a space between the numeral and the unit of measurement for units like kW. See e.g. Chicago Manual of Style 9.16 (17th edition).

          Comment


          • #6
            Thanks Nick, appreciate it!

            Comment

            Working...
            X