Announcement

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

  • How to add a secondary y-axis to a bar chart with the graph bar command

    Hi everyone,

    I am new here and would appreciate your help with the following bar chart.

    The chart has 3 (year) bars per id (string variable). I am wondering if anyone knows how to add a secondary y-axis with the graph bar command.

    Should the graph bar not allow for a secondary y-axis, would the twoway bar command be the solution? The id can be transformed to a numerical value but should eventually be relabelled with the full name on the x axis.

    Code:
    graph bar (mean) var, over(year) over(id)

    Cheers,
    Giovanni
    Last edited by Giovanni Palmioli; 26 Apr 2016, 15:18.

  • #2
    There is no scope for a secondary y axis with graph bar. I don't know what you want to do with it but twoway bar sounds a more likely direction.

    Please read
    http://www.statalist.org/forums/help#stata for advice on (1) showing example data and code (2) showing graphs as .png attachments, not photos.

    Judging from your example graph, log scale is the answer, not a secondary axis.

    Comment


    • #3
      Hi Nick,

      I would like to follow up on this post. Thanks for the suggestion but I cannot use a log scale because the values are percentages and I need to keep them as they are.

      I would like to have 3 stacked bars (year 2006, 2010, 2010) next to each other per id_s and use a secondary y-axis for some id_s (in particular 1 and 7). How would you use twoway bar(if it is a better option than graph bar)?

      I am interested only in the pairs with id_h=100 but would like to keep all pairs. The panel below is a little different from the one I used for the chart in my original post.

      Your inputs would be much appreciated.

      Code:
       
      id_s id_h year exp_a_perc exp_b_perc
      1 100 2006 18.99203 17.12362
      1 100 2010 31.92924 35.2694
      1 100 2014 18.900452 19.852894
      2 100 2006 3.85358 1.0512108
      2 100 2010 8.024672 2.316856
      2 100 2014 7.654442 1.6625088
      3 100 2006 0.9600852 2.04596
      3 100 2010 1.84154 2.861238
      3 100 2014 0.997911 1.6934196
      4 100 2006 0.0183314 0.517915
      4 100 2010 8.952862 1.3665348
      4 100 2014 5.784374 0.6271664
      5 100 2006 3.041822 5.23905
      5 100 2010 6.202086 9.874898
      5 100 2014 1.5041632 2.863996
      6 100 2006 0.5564346 0.1237718
      6 100 2010 0.953272 0.4472336
      6 100 2014 0.7111274 0.4214042
      7 100 2006 16.805914 28.41752
      7 100 2010 32.60148 35.03638
      7 100 2014 8.408258 15.419746
      8 100 2006 1.3177232 1.1193254
      8 100 2010 2.686618 1.76735
      8 100 2014 2.52161 1.423866
      .. 200 .. .. ..
      .. 200 .. .. ..
      200 .. .. ..

      Comment


      • #4
        Thanks for the data example. Looking at your data I would say that the problem is not so much accommodating the large values as showing the small values properly.

        I tried this:

        Code:
        clear 
        
        set scheme s1color 
        
        input id_s    id_h    year    exp_a_perc    exp_b_perc
        1    100    2006    18.99203    17.12362
        1    100    2010    31.92924    35.2694
        1    100    2014    18.900452    19.852894
        2    100    2006    3.85358    1.0512108
        2    100    2010    8.024672    2.316856
        2    100    2014    7.654442    1.6625088
        3    100    2006    0.9600852    2.04596
        3    100    2010    1.84154    2.861238
        3    100    2014    0.997911    1.6934196
        4    100    2006    0.0183314    0.517915
        4    100    2010    8.952862    1.3665348
        4    100    2014    5.784374    0.6271664
        5    100    2006    3.041822    5.23905
        5    100    2010    6.202086    9.874898
        5    100    2014    1.5041632    2.863996
        6    100    2006    0.5564346    0.1237718
        6    100    2010    0.953272    0.4472336
        6    100    2014    0.7111274    0.4214042
        7    100    2006    16.805914    28.41752
        7    100    2010    32.60148    35.03638
        7    100    2014    8.408258    15.419746
        8    100    2006    1.3177232    1.1193254
        8    100    2010    2.686618    1.76735
        8    100    2014    2.52161    1.423866
        end 
        
        tabplot year id_s [iw=exp_a_perc], showval(format(%2.1f))
        
        more 
        
        tabplot year id_s [iw=exp_b_perc], showval(format(%2.1f))
        
        more 
        
        bysort year (exp_a_perc) : gen order = _N - _n + 1 if year == 2006
        bysort id_s (year): replace order = order[1]
        labmask order, values(id_s)
        
        tabplot year order [iw=exp_a_perc], showval(format(%2.1f))
        Here tabplot must be installed from SSC http://www.statalist.org/forums/foru...updated-on-ssc

        Given several small values, presumably of real interest too, "looking them up" is eased by a hybrid graph and table.

        Here I show your two variables, and with a guess that the identifier may be arbitrary, an ordering on values in 2006.

        labmask should be installed from the Stata Journal files.

        Click image for larger version

Name:	palmioli.png
Views:	2
Size:	11.5 KB
ID:	1342588


        Click image for larger version

Name:	palmioli2.png
Views:	1
Size:	10.9 KB
ID:	1342589


        Click image for larger version

Name:	palmioli3.png
Views:	1
Size:	11.4 KB
ID:	1342590

        Attached Files

        Comment


        • #5
          Nick,

          Many thanks for your prompt feedback.

          Sorry, my previous post was not very clear maybe. I am looking to create a bar chart similar to the one I attached in my first post. Can you see the attachment?

          Hope this clarifies my previous post.




          Comment


          • #6
            You were entirely clear in broad terms. You want one set of stacked bars related to one axis and another to another axis. I have to declare zero interest in writing code for that, as I think it's quite unnecessary.

            Naturally, the thread is entirely open for other suggestions..
            Last edited by Nick Cox; 25 May 2016, 14:11.

            Comment

            Working...
            X