Announcement

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

  • Two way graph _ Chartbar overlapping

    Dear collegues,

    I am currently working on a seafood landing data base for my Phd.

    My purpose is to compare local landing (in 4 harbours, identified as GR, PB, CH and FC) with the total of landing in all of these harbours (identified as Totalcriees).

    My data are organised as following:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int Years double(GR PB CH FC Totalcriees)
    2010  .8585 2.3565    6.15      0      9.3
    2011 3.0615 4.4945 17.6535      0    25.21
    2012 5.0345  6.239  9.4776      0  20.7511
    2013  3.617   .846       0      0    4.463
    2014 3.6375 1.3825  2.0958      0   7.1158
    2015 4.4386  8.495  3.7591      0  16.6927
    2016 1.5185  7.534    6.28      0 15.33705
    2017 2.3155  5.892  5.7251      0  13.9326
    2018   3.22 6.7135  3.4707      0   13.401
    2019  1.528 2.3885  5.5359      0   9.4524
    2020 1.5693   .407    4.12      0      6.1
    2021 2.8072   .212   8.863 9.4816    21.36
    end
    I want to represent the landing in each harbour, with 4 bar charts, by years. After that, i want to add (on the same graph) the total of the landing with a line, based on the same y axis than the previous bar charts. For the moment, i use the following line code :

    Code:
    twoway (bar GR Years) (bar PB Years) (bar CH Years) (bar FC Years) (line Totalcriees Years)
    My problem is that my barcharts are currently overlapping by years, as you can see here:

    Click image for larger version

Name:	STATA.PNG
Views:	1
Size:	65.1 KB
ID:	1663597



    I would like to know how I could position them next to each other by year, and avoid this overlapping? Which option i need to implement in my code ?

    Thank you in advance for your help, I remain available if ever to provide you with more information.

    Regards,

    Jean-François







  • #2
    Thanks for the data example!

    To do what you ask, you'd need to check out the trick documented in https://www.stata-journal.com/articl...article=gr0026 and set up bars of width 0.25 placed at different offsets.

    I wouldn't do that given your data. Stacked bars are one alternative, but here is another. I use tabplot from the Stata Journal and mycolours from SSC.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int Years double(GR PB CH FC Totalcriees)
    2010  .8585 2.3565    6.15      0      9.3
    2011 3.0615 4.4945 17.6535      0    25.21
    2012 5.0345  6.239  9.4776      0  20.7511
    2013  3.617   .846       0      0    4.463
    2014 3.6375 1.3825  2.0958      0   7.1158
    2015 4.4386  8.495  3.7591      0  16.6927
    2016 1.5185  7.534    6.28      0 15.33705
    2017 2.3155  5.892  5.7251      0  13.9326
    2018   3.22 6.7135  3.4707      0   13.401
    2019  1.528 2.3885  5.5359      0   9.4524
    2020 1.5693   .407    4.12      0      6.1
    2021 2.8072   .212   8.863 9.4816    21.36
    end
    
    rename (??) (catch=)
    
    reshape long catch , i(Years) j(which) string 
    
    label def where 1 GR 2 PB 3 CH 4 FC
    encode which, gen(where) label(where)
    
    gen totalpos = 5 
    gen totalshow = strofreal(Total, "%2.1f")
    
    mycolours 
    
    tabplot where Years [iw=catch], xasis barw(0.9) separate(which) showval(format(%2.1f)) ///
    bar1(color("`OK1'")) bar2(color("`OK2'")) bar3(color("`OK3'")) bar4(color("`OK4'"))  /// 
    addplot(scatter totalpos Years, ms(none) mla(totalshow) mlabc(red) mlabpos(0) mlabsize(medium)) ///
    xtitle("") xla(, noticks) ytitle("Catch") subtitle("") ymla(5 "Total", notick ang(h) labc(red) labsize(medium)) ysc(r(. 5.2))


    Click image for larger version

Name:	catch.png
Views:	1
Size:	28.0 KB
ID:	1663614

    Comment


    • #3
      You can also try this though the graph may be further edited
      graph bar (mean) GR PB CH FC (mean) Totalcriees, over(Years) bargap(1) intensity(40) blabel(total, size(vsmall) orientation(vertical) format(%9.1f)) yscale(range(0 28))

      Comment


      • #4
        If you follow Noah's path, you will get a cleaner legend with


        Code:
        graph bar (asis) GR PB CH FC Totalcriees, over(Years) bargap(1) intensity(40) blabel(total, size(vsmall) orientation(vertical) format(%9.1f)) yscale(range(0 28))

        Comment


        • #5
          If this was my graph, I would opt for the tabplot design in #2 which makes the comparison across categories easier. Here is a way to get a stacked design as you request in #1.

          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input int Years double(GR PB CH FC Total)
          2010  .8585 2.3565    6.15      0      9.3
          2011 3.0615 4.4945 17.6535      0    25.21
          2012 5.0345  6.239  9.4776      0  20.7511
          2013  3.617   .846       0      0    4.463
          2014 3.6375 1.3825  2.0958      0   7.1158
          2015 4.4386  8.495  3.7591      0  16.6927
          2016 1.5185  7.534    6.28      0 15.33705
          2017 2.3155  5.892  5.7251      0  13.9326
          2018   3.22 6.7135  3.4707      0   13.401
          2019  1.528 2.3885  5.5359      0   9.4524
          2020 1.5693   .407    4.12      0      6.1
          2021 2.8072   .212   8.863 9.4816    21.36
          end
          
          local vars FC CH PB GR
          gen FCs=FC
          local i 1
          foreach var of local vars{
              if `i'>=2{
                  gen `var's=`var'+ `=word("`vars'", `i'-1)'s
              }
              local ++i
          }
          *net install cleanplots, from("https://tdmize.github.io/data/cleanplots")
          set scheme cleanplots
          twoway (bar GRs PBs CHs FCs Years) (line Total Years), xlab(2010/2021, noticks) xtitle("")
          Click image for larger version

Name:	Graph.png
Views:	1
Size:	47.4 KB
ID:	1663626

          Comment


          • #6
            Thanks everyone for your answers ! I will submit these three proposals to my partners!

            Regards,

            Jean-François

            Comment


            • #7
              Whatever you choose, it is likely to be helpful if you show longer names for the four harbours.

              If you choose stacking, the total line is redundant.

              Comment


              • #8
                Can we have a pseudo- twoway bars command? Such a psedudo-like command will produce a twoway bar like graph bar, over(), but with bars separate.

                Twoway Bar for two variables by blocks of time

                plotting multiple bar and line graphs

                Combining stacked barchart (with negative values) with line using twoway

                Code:
                * Example generated by -dataex-. For more info, type help dataex
                clear
                input int Years double(GR PB CH FC Totalcriees)
                2010  .8585 2.3565    6.15      0      9.3
                2011 3.0615 4.4945 17.6535      0    25.21
                2012 5.0345  6.239  9.4776      0  20.7511
                2013  3.617   .846       0      0    4.463
                2014 3.6375 1.3825  2.0958      0   7.1158
                2015 4.4386  8.495  3.7591      0  16.6927
                2016 1.5185  7.534    6.28      0 15.33705
                2017 2.3155  5.892  5.7251      0  13.9326
                2018   3.22 6.7135  3.4707      0   13.401
                2019  1.528 2.3885  5.5359      0   9.4524
                2020 1.5693   .407    4.12      0      6.1
                2021 2.8072   .212   8.863 9.4816    21.36
                end
                
                gen Years1=Years-0.6
                gen Years2=Years-0.3
                gen Years3=Years
                gen Years4=Years+0.3
                twoway (bar GR Years1, barwidth(0.2)) (bar PB Years2, barwidth(0.2)) (bar CH Years3, barwidth(0.2)) (bar FC Years4, barwidth(0.2)) (line Totalcriees Years), xlabel(2010/2021)
                Click image for larger version

Name:	Graph.png
Views:	1
Size:	222.9 KB
ID:	1787537

                Comment


                • #9
                  Here is a small user-written command, pseudobars, that automates the horizontal positioning of grouped bars and allows a line to be overlaid at the centre of each group.

                  I have attached the command (.ado), its help file (.sthlp), and a self-contained example (.do). Save all three files in the same folder, set that folder as your Stata working directory, and run the example do-file. The example includes its own data, so no separate dataset is needed.

                  For the data in this thread, the basic syntax is:
                  Code:
                   pseudobars GR PB CH FC, over(Years) line(Totalcriees)
                  Type help pseudobars for further options. This command was tested in Stata 16.1. I hope this is useful for the automatic positioning you were looking for.


                  Click image for larger version

Name:	00_comparison.png
Views:	1
Size:	450.5 KB
ID:	1787549
                  Attached Files

                  Comment


                  • #11
                    Thank you so much Choonjoo Lee. That's great! And I must say that the graph, Four views of the same harbour data is aesthetically beautiful!

                    Comment


                    • #12
                      Follows Choonjoo Lee, (I like -pseudobars- so much!) I continue to do some illustrative work. The data used below is downloaded from WeatherArchive. The command -sbplot- was wrote by Nick Cox. Just as he said:
                      sbplot may be useful for combining bars with other styles of representing data, such as continuous lines for time series or error bars for showing uncertainties.
                      Code:
                      * Example generated by -dataex-. To install: ssc install dataex
                      clear
                      input str10 time byte weather_code double(temperature_2m_max temperature_2m_min precipitation_sum) byte snowfall_sum double(sunshine_duration wind_speed_10m_max wind_gusts_10m_max) byte precipitation_hours
                      "2026/8/18"   3 32.4 25.1    . .        . 10.4 29.9  2
                      "2026/8/19"   3 33.1   26    0 0 43281.52 12.8 36.7  0
                      "2026/8/20"  96 31.1 26.8   11 0 30650.73 15.3 39.2 11
                      "2026/8/21"  51 31.8 26.6    2 0    43200 15.9 38.5 12
                      "2026/8/22"  96 30.7 26.9  8.8 0 41980.23 16.3 40.3 15
                      "2026/8/23"  95   31 27.1  3.1 0    43200 22.6   54 16
                      "2026/8/24"  51 31.6 26.8   .3 0  43370.4 18.6 45.4  3
                      "2026/8/25"  51 31.5 25.9   .2 0    43200 14.1 37.8  2
                      "2026/8/26"  51 33.1 25.6   .1 0    43200 12.8 34.6  1
                      "2026/8/27"  53 32.6 27.2  3.3 0    43200 23.8 58.3 12
                      "2026/8/28"  95 31.3 26.9  7.2 0 35528.14 20.6 54.7 15
                      "2026/8/29"  51 32.3 26.5    1 0    43200 14.1 36.7  8
                      "2026/8/30"  96 31.9 26.9  6.6 0  37306.9 17.2 41.8 14
                      "2026/8/31"  96 32.6 25.9 12.1 0    43200 14.9 39.6 14
                      "2026/9/1"   51 30.2 25.8   .1 0 17561.68 11.8 28.4  1
                      "2026/9/2"   95 28.9 23.2 21.7 0  3970.62 15.8 38.2 15
                      "2026/9/3"   95 30.6   25  3.4 0 40287.58 16.9 43.9 16
                      "2026/9/4"   53 30.6 25.2  2.4 0    43200 17.8 41.8 14
                      "2026/9/5"   51 30.8 24.4   .8 0    43200 16.3 41.4  5
                      "2026/9/6"   51 29.5 23.8  1.2 0    43200 12.1 34.9  8
                      "2026/9/7"   51 30.3 23.3  1.1 0 41164.83 15.5 41.4  8
                      "2026/9/8"    3   31 23.2    0 0  40743.5 17.5 44.6  0
                      "2026/9/9"   51 28.9 22.5   .1 0  28629.1 17.7 45.4  1
                      "2026/9/10"  51 29.4 20.5   .1 0  40032.6 17.2 43.6  1
                      "2026/9/11"  53   27 22.1  1.6 0  2910.02   13 31.7  8
                      "2026/9/12"   3 31.4 22.1    0 0 43041.54 11.3 33.8  0
                      "2026/9/13"   3 32.7 22.6    0 0 39260.49 12.7 34.9  0
                      "2026/9/14"  51 31.5 23.2   .5 0 42173.92 13.4   36  3
                      "2026/9/15"   3 29.6 22.4    0 0 38926.07 14.4 39.2  0
                      "2026/9/16"   3 30.3 20.9    0 0 40903.61 12.7 35.3  0
                      "2026/9/17"  51 29.7   22   .8 0  22586.2 12.1 31.7  4
                      "2026/9/18"  53 30.8 23.8  1.7 0 26429.19 11.4 31.3  4
                      "2026/9/19"  95 30.6 24.2 10.8 0 17651.81   10 29.2 14
                      "2026/9/20"   3 28.9 22.4    0 0 42605.23   11   27  0
                      "2026/9/21"  51 29.1 21.3   .6 0 40554.79 12.6 28.8  3
                      "2026/9/22"  51 29.3 21.9   .1 0 35020.48 10.4 27.7  1
                      "2026/9/23"   3 29.2 22.8    0 0 19374.97 10.8 25.9  0
                      "2026/9/24"  51 30.2 23.5   .3 0 19806.45 11.1 29.5  1
                      "2026/9/25"  51 32.5 23.6   .1 0 40711.06 10.9 30.6  1
                      "2026/9/26"  80   33 24.3  6.2 0  35875.8 11.3 37.4 13
                      "2026/9/27"  95 28.9 23.4 10.3 0 17520.67  8.8 31.3 18
                      "2026/9/28"  53 28.1 23.6  2.4 0 26342.72   12 32.4  5
                      "2026/9/29"  80 26.2 22.5  3.5 0  3681.86 14.8 38.9  9
                      "2026/9/30"  81   25 21.1  8.6 0  3059.84  9.8 27.7 11
                      "2026/10/1"  53 25.5 20.5    1 0 13320.26 14.5 38.9  7
                      "2026/10/2"  55 23.1 18.8  2.3 0        0 10.7 26.3  2
                      "2026/10/3"  81 22.8 18.5 16.8 0  4026.99 10.2   23 17
                      "2026/10/4"  61 21.4 19.5 13.7 0        0 12.3 28.4 16
                      "2026/10/5"   3 22.2 16.1    0 0 37273.24 15.5 40.7  0
                      "2026/10/6"   2 22.7 13.8    0 0 40334.63 10.5 26.6  0
                      "2026/10/7"   0 25.3 13.7    0 0 40101.65    9 16.9  0
                      "2026/10/8"   3 24.9 14.8    0 0 38082.22  7.9 16.2  0
                      "2026/10/9"   3 25.8 16.1    0 0 25536.01  9.1 19.8  0
                      "2026/10/10" 80 25.3   18    5 0    39600  7.8 18.4 12
                      "2026/10/11"  3 25.7 18.9    0 0    39600 10.9 29.5  0
                      "2026/10/12"  3 24.9 17.1    0 0 18934.33  9.8 25.2  0
                      "2026/10/13" 45 27.1   20    0 0  36076.6 12.8 33.5  0
                      "2026/10/14"  3 24.3   18    0 0    39600 12.7 32.4  0
                      "2026/10/15"  3 24.2 17.3    0 0 17100.73 11.6 30.6  0
                      "2026/10/16" 51 26.5 20.3   .2 0 31110.82 16.5 40.7  2
                      "2026/10/17"  3 28.2 21.4    0 0    39478 18.9 42.8  0
                      "2026/10/18" 53 32.1 21.4  2.5 0 38374.18 18.8 42.5  9
                      "2026/10/19" 51 25.1 20.9    1 0     3600  7.8 18.7  5
                      "2026/10/20" 55 28.1 20.1  4.4 0 22899.77 12.8 28.4  5
                      end
                      Code:
                      gen date=date(time,"YMD")
                      format %td date
                      * sbplot is wrote by Nick Cox ox, 19990209
                      ssc install sbplot
                      sbplot precipitation_sum temperature_2m_max temperature_2m_min date
                      * sbplot8 is a hacks of sbplot, it allows users run under Sata 8 or latter
                      sbplot8 precipitation_sum temperature_2m_max temperature_2m_min date, line name(g1, replace)
                      pseudobars precipitation_sum, over(date) addvars(temperature_2m_max temperature_2m_min) addplot(line temperature_2m_max temperature_2m_min date) xlabel(#5) legend(order(1 "precipitation_sum" 2 "temperature_2m_max" 3 "temperature_2m_min")) name(g2, replace)
                      graph combine g1 g2, ycommon xcommon row(2)
                      Click image for larger version

Name:	Untitled.png
Views:	1
Size:	172.5 KB
ID:	1787565


                      Click image for larger version

Name:	Graph.png
Views:	1
Size:	246.6 KB
ID:	1787566

                      Comment


                      • #13
                        Weather in Shanghai, 18 August 2026 – 20 October 2026
                        Click image for larger version

Name:	2026-10-21_173304_337.png
Views:	1
Size:	231.0 KB
ID:	1787568

                        Comment


                        • #14
                          sbplot as you can see was originally written in 1999 before Stata 8 rewrote the graphics command completely.

                          The design in #12 and #13 works, just about, for these data, but would not so work at well for temperatures nearer 0 deg C.

                          I would usually just have two panels with separate scales for similar data (temperature and precipitation) and reach for
                          graph combine.

                          More generally, reverting to #8 on, there isn't or shouldn't be anything pseudo about these bars. They are standard bars. You just want bars side by side and need a name for that. Multiple bars is still ambiguous, but perhaps better nevertheless.

                          Comment


                          • #15
                            I would like to push back a bit on Nick Cox's point. Of course, I would prefer to call them "twoway bars," but I also accept the term "pseudo bars" (and indeed, that is what I proposed). Here is why:

                            First, as is well known, twoway bar can only accept a single y-variable, with the syntax twoway bar yvar xvar. In this respect, it is far less flexible than its sibling twoway line, which accepts a varlist (twoway line varlist). To plot multiple y-vars, one has to specify multiple twoway bar commands separated by || or parentheses.

                            Second, when there are multiple y-vars at the same value of xvar, the standard twoway bar will stack them on top of each other. This is precisely why there are so many questions on Statalist asking "how do I stop the bars from overlapping?"

                            Third, and this is related to the previous two points, the reason we are inclined to call them "pseudo bars" is that we make slight adjustments to xvar at the same value, so that the bars do not overlap. Under these circumstances, it seems fair to say that these are not bars in the standard Stata sense, and it is more appropriate to call them "pseudo"—that is, imitative or resembling. We certainly do not mean that these bars are fake.

                            Comment

                            Working...
                            X