Announcement

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

  • Issue creating hbar graphs

    Hello Statalisters,

    I'm having a problem with creating horizontal bar graphs and need some assistance. We have a dataset with both zipcodes and census tracts and used the Housing and Urban Development crosswalk file to allow for data to be added from other sources. What this means is that we have zip codes with multiple census tracts (these aren't synonymous in terms of geography but some of our datasources only used census tracts while others used zip codes). Anyway, now I want to create bar graphs that show patterns of evictions and foreclosures but do to the shape of the data when I call the command, the y-axis is crowded. Here's an example of the data:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long zip str19 censustractname int(evictfilings_2020 evictfilings_2021 evictfilings_2022) byte(foreclosurenote_2020 foreclosurenote_2021)
    37501 "Census Tract 219"     65  79 119  0  0
    38101 "Census Tract 43"      21  32  67  0  0
    38103 "Census Tract 1"       23  49  83 14 22
    38103 "Census Tract 113"     32  22  39 14 22
    38103 "Census Tract 114"    155 177 280 14 22
    38103 "Census Tract 37"      41  71  58 14 22
    38103 "Census Tract 38"      40  68  50 14 22
    38103 "Census Tract 39"      84 110 109 14 22
    38103 "Census Tract 42"      86 124 202 14 22
    38103 "Census Tract 43"      21  32  67 14 22
    38104 "Census Tract 115"     43  51  66  6 10
    38104 "Census Tract 16"       1   7   8  6 10
    38104 "Census Tract 17"      31  33  32  6 10
    38104 "Census Tract 25"      45  48  80  6 10
    38104 "Census Tract 26"       7   6  10  6 10
    38104 "Census Tract 31"      16  23  23  6 10
    38104 "Census Tract 32"      69  73 111  6 10
    38104 "Census Tract 33"       1   8  10  6 10
    38104 "Census Tract 34"      50  64  81  6 10
    38104 "Census Tract 35"      43  31  57  6 10
    38104 "Census Tract 36"      44  72  83  6 10
    38104 "Census Tract 37"      41  71  58  6 10
    38104 "Census Tract 38"      40  68  50  6 10
    38104 "Census Tract 46"      31  34  53  6 10
    38104 "Census Tract 62"      25  30  35  6 10
    38104 "Census Tract 63"      19  15  19  6 10
    38104 "Census Tract 66"      18  14  19  6 10
    38105 "Census Tract 113"     32  22  39 22 22
    38105 "Census Tract 20"      41  48  52 22 22
    38105 "Census Tract 21"      33  33  74 22 22
    38105 "Census Tract 24"      66  59  74 22 22
    38105 "Census Tract 25"      45  48  80 22 22
    38105 "Census Tract 36"      44  72  83 22 22
    38105 "Census Tract 38"      40  68  50 22 22
    38105 "Census Tract 39"      84 110 109 22 22
    38105 "Census Tract 42"      86 124 202 22 22
    38106 "Census Tract 115"     43  51  66  0  0
    38106 "Census Tract 116"     50  29  53  0  0
    38106 "Census Tract 117"     24  17  28  0  0
    38106 "Census Tract 225.00" 129 121 310  0  0
    38106 "Census Tract 43"      21  32  67  0  0
    38106 "Census Tract 50"      48  16  28  0  0
    38106 "Census Tract 55"      75  61  72  0  0
    38106 "Census Tract 56"     122 128 280  0  0
    38106 "Census Tract 57"      37  30  48  0  0
    38106 "Census Tract 58"      26  28  41  0  0
    38106 "Census Tract 59"      55  41  61  0  0
    38106 "Census Tract 60"      33  26  37  0  0
    38106 "Census Tract 62"      25  30  35  0  0
    38106 "Census Tract 64"      27  26  39  0  0
    end
    and here is an example of what an hbar chart looks like with that data:

    Click image for larger version

Name:	HbarTest.png
Views:	1
Size:	118.5 KB
ID:	1709681


    Clearly this is a bit nonsensical and I'm wondering if I need to somehow collapse the data prior to creating these graphs, but hesitate to do so as this changes the overall shape of the data. Any ideas?

  • #2
    It appears that you have a number of empty categories. Use the option -nofill- to suppress these.

    Code:
    gr hbar ..., over(zip) nofill

    Comment


    • #3


      Click image for larger version

Name:	TestGraph2.png
Views:	2
Size:	142.3 KB
ID:	1709706
      Click image for larger version

Name:	TestGraph.png
Views:	2
Size:	81.0 KB
ID:	1709707





      Andrew Musau, unfortunately, that doesn't fix the issues I'm having. See attachedI switched the axis, but we have 28 different zip codes to plot, so maybe creating a group of these might work? Or, is there a way to expand (offset) the x or y axis to allow room for the zip codes to show properly?
      Attached Files

      Comment


      • #4
        Try something like:

        Code:
        gr hbar evictfilings_2020 evictfilings_2021 evictfilings_2022, over(zip, lab(labsize(vsmall))) stack ysize(10) legend(cols(1))
        Last edited by Hua Peng (StataCorp); 13 Apr 2023, 13:55.

        Comment


        • #5
          Hua Peng (StataCorp) That did the trick. Thank you!
          Click image for larger version

Name:	EvictionFilings by Year and Zip.png
Views:	1
Size:	58.1 KB
ID:	1709720

          Comment


          • #6
            You could do better by losing the legend and separating + ordering the bars. Below, I use tabplot and labmask, both from the Stata Journal. Some size adjustments may be needed.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input long zip str19 censustractname int(evictfilings_2020 evictfilings_2021 evictfilings_2022) byte(foreclosurenote_2020 foreclosurenote_2021)
            37501 "Census Tract 219"     65  79 119  0  0
            38101 "Census Tract 43"      21  32  67  0  0
            38103 "Census Tract 1"       23  49  83 14 22
            38103 "Census Tract 113"     32  22  39 14 22
            38103 "Census Tract 114"    155 177 280 14 22
            38103 "Census Tract 37"      41  71  58 14 22
            38103 "Census Tract 38"      40  68  50 14 22
            38103 "Census Tract 39"      84 110 109 14 22
            38103 "Census Tract 42"      86 124 202 14 22
            38103 "Census Tract 43"      21  32  67 14 22
            38104 "Census Tract 115"     43  51  66  6 10
            38104 "Census Tract 16"       1   7   8  6 10
            38104 "Census Tract 17"      31  33  32  6 10
            38104 "Census Tract 25"      45  48  80  6 10
            38104 "Census Tract 26"       7   6  10  6 10
            38104 "Census Tract 31"      16  23  23  6 10
            38104 "Census Tract 32"      69  73 111  6 10
            38104 "Census Tract 33"       1   8  10  6 10
            38104 "Census Tract 34"      50  64  81  6 10
            38104 "Census Tract 35"      43  31  57  6 10
            38104 "Census Tract 36"      44  72  83  6 10
            38104 "Census Tract 37"      41  71  58  6 10
            38104 "Census Tract 38"      40  68  50  6 10
            38104 "Census Tract 46"      31  34  53  6 10
            38104 "Census Tract 62"      25  30  35  6 10
            38104 "Census Tract 63"      19  15  19  6 10
            38104 "Census Tract 66"      18  14  19  6 10
            38105 "Census Tract 113"     32  22  39 22 22
            38105 "Census Tract 20"      41  48  52 22 22
            38105 "Census Tract 21"      33  33  74 22 22
            38105 "Census Tract 24"      66  59  74 22 22
            38105 "Census Tract 25"      45  48  80 22 22
            38105 "Census Tract 36"      44  72  83 22 22
            38105 "Census Tract 38"      40  68  50 22 22
            38105 "Census Tract 39"      84 110 109 22 22
            38105 "Census Tract 42"      86 124 202 22 22
            38106 "Census Tract 115"     43  51  66  0  0
            38106 "Census Tract 116"     50  29  53  0  0
            38106 "Census Tract 117"     24  17  28  0  0
            38106 "Census Tract 225.00" 129 121 310  0  0
            38106 "Census Tract 43"      21  32  67  0  0
            38106 "Census Tract 50"      48  16  28  0  0
            38106 "Census Tract 55"      75  61  72  0  0
            38106 "Census Tract 56"     122 128 280  0  0
            38106 "Census Tract 57"      37  30  48  0  0
            38106 "Census Tract 58"      26  28  41  0  0
            38106 "Census Tract 59"      55  41  61  0  0
            38106 "Census Tract 60"      33  26  37  0  0
            38106 "Census Tract 62"      25  30  35  0  0
            38106 "Census Tract 64"      27  26  39  0  0
            end
            
            rename (evictfilings_2020 evictfilings_2021 evictfilings_2022) count=
            reshape long count, i(zip censustract) j(which) string
            gen year= real(substr(which, -4, 4))
            bysort zip: egen total =total(count)
            egen group = group(total zip)
            labmask group, values(zip)
            set scheme s1mono
            tabplot group year [iw=count], horiz xtitle("") ytitle("") note("") ///
            showval title(Eviction Filings by ZIP Code, size(medsmall))
            Res.:
            Click image for larger version

Name:	Graph.png
Views:	1
Size:	34.6 KB
ID:	1709739

            Last edited by Andrew Musau; 13 Apr 2023, 15:43.

            Comment


            • #7
              I guess that the original data are frequencies. graph hbar defaults to showing means, and given individual values, it is happy to report each value as its own mean. You can avoid the dopey "mean of" text in the legend by starting

              Code:
              graph hbar (asis)
              That said, I unsurprisingly support Andrew Musau's alternative. Whether it works as well with the full set of tracts (56 or so) and whether you should be scaling by some measure of the set at risk in each tract and year are open questions.

              Comment

              Working...
              X