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

              Working...
              X