Announcement

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

  • Help in creating grouped bar chart

    Hi,

    I am using Stata version 17 and trying to create a grouped bar chart like this:

    Click image for larger version

Name:	sample grouped chart.PNG
Views:	1
Size:	39.1 KB
ID:	1639599


    I use the following codes:

    Code:
    graph bar Score, over(Year) over(Country) title("Test1")


    My sample dataset is like this:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int Year str9 Country double Score
    2000 "Singapore"   2
    2017 "Singapore"   1
    2018 "Singapore"   3
    2000 "Malaysia"  1.3
    2017 "Malaysia"  1.2
    2018 "Malaysia"  1.4
    2000 "Indonesia"   1
    2017 "Indonesia" 1.2
    2018 "Indonesia"  .9
    2000 "Myanmar"    .8
    2017 "Myanmar"    .7
    2018 "Myanmar"    .6
    end
    My bar chart looks like this:

    Click image for larger version

Name:	Graph.png
Views:	1
Size:	101.7 KB
ID:	1639600



    Is there any way to modify the codes to create similar charts as the first one?

    Thanks!



  • #2
    add asyvars option:

    Code:
    graph bar Score, over(Year) over(Country) title("Test1") asyvars legend(position(3) col(1))

    Comment


    • #3
      Hi,

      Thanks for this, it works perfectly! One follow up question: how does one change the font size of the x-axis? Specifically, I would like the country name on the x-axis to be smaller and I could only do that via the graph editor. Any way to do it in coding instead?

      Thanks!

      Comment


      • #4
        Strictly speaking, xaxis1 does not exist in a bar plot. What you can control is font size with respect to the over() variable, that is Country in your data. And you can change it by specifying over_subopts.

        Code:
        graph bar Score, over(Year) over(Country, label(labsize(small))) title("Test1") asyvars legend(position(3) col(1))

        Comment


        • #5
          Varying scores over time suggest to me the alternative of a line chart, but the unequal spacing 2000 2017 2018 would make that idea awkward.

          Here is a dot chart, which doesn't oblige messing with font sizes to squeeze (important!) detail.


          Code:
          clear
          input int Year str9 Country double Score
          2000 "Singapore"   2
          2017 "Singapore"   1
          2018 "Singapore"   3
          2000 "Malaysia"  1.3
          2017 "Malaysia"  1.2
          2018 "Malaysia"  1.4
          2000 "Indonesia"   1
          2017 "Indonesia" 1.2
          2018 "Indonesia"  .9
          2000 "Myanmar"    .8
          2017 "Myanmar"    .7
          2018 "Myanmar"    .6
          end
          
          separate Score, by(Country) veryshortlabel
          graph dot (asis) Score?, over(Year) over(Country) linetype(line) lines(lc(gs12) lw(vthin)) scheme(s1color) ///
          marker(1, ms(Oh)) marker(2, ms(Th)) marker(3, ms(Dh)) marker(4, ms(Dh)) legend(off) ysc(alt) ytitle(Score)
          Click image for larger version

Name:	score.png
Views:	1
Size:	24.4 KB
ID:	1639630



          Here is the bar chart from #4 (using the same scheme)

          Click image for larger version

Name:	score2.png
Views:	1
Size:	20.2 KB
ID:	1639631



          Note that even if you prefer the bar chart, the y axis title should be fixed (done by using graph bar (asis) instead of the default). The legend remains awkward and obliges mental back and forth: direct labelling, as with the dot chart, is surely preferable;

          In the dot chart, a personal choice with graphs with a table flavour is putting the y axis and its information at the top. More discussion at https://www.stata-journal.com/articl...article=gr0053

          graph dot has an undocumented vertical option, but I didn't try it.

          Comment

          Working...
          X