Announcement

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

  • #16
    In other words, if Stata's official graph bar yvars command could accept the addplot option (just as Choonjoo Lee's syntax does), why would we take the long way around? Why is it that in the graph bar command, bars at the same value of xvar (that is, over(varname)) do not overlap with one another, yet when we get to twoway bar, such a setup is simply not supported?

    Comment


    • #17
      Your need is to convince StataCorp -- not me -- and I don't speak for StataCorp. But I am still asking StataCorp: if you implement this, please, please, don't call them pseudobars. The term makes no sense to me and is not one I have ever seen outside this thread. (But by all means anyone please give references to their use in reputable literature or internet sources if I am missing a somewhere standard term.)

      twoway bars really won't fly. It is far too close to twoway bar.

      Also, seriously @Chen, why not suggest the precise syntax you would like to see? As I understand it, StataCorp almost always once they have decided on a new command discuss the syntax and the documentation (!) in detail before writing the code.

      The syntax includes options. A user who likes this idea may want to specify different colours, different widths, even perhaps different bases.

      (As a user-programmer responsible for several commands over several years, I almost never write commands this way, although perhaps I should. Most commands I write grow out of noticing that I am writing the same commands repeatedly to do something and decide that there is a need to bundle them together. Bells and whistles get added later. And the help file follows.)

      Comment


      • #18
        Thank you, Nick, for the clarification about sbplot and for the advice on presentation. I agree that precipitation and temperature are clearer in separate panels with independent y scales, especially when temperatures are near or below zero.

        I have added an optional panels display to the wrapper:

        Code:
        pseudobars rain, over(date) line(tmax tmin) panels ///
            barpanelopts(ytitle("mm")) ///
            linepanelopts(ytitle("Degrees C"))
        This uses graph combine with a common x-axis range and separate y scales. The existing overlay remains available by omitting panels. The choice depends on the units and the purpose of the comparison.

        I also take your point about the name: these are ordinary grouped (side-by-side) bars. I have clarified that in the help, while retaining the command name for compatibility. Thank you also, Chen, for trying the command, suggestion, and sharing the weather example. The attached codes uses standard Stata graphing techniques, and I am sharing it as a starting point for discussion. Please feel free to use, modify, or extend the code, including changing the command name, to suit your needs. No need to ask me first.

        Click image for larger version

Name:	07_weather_panels.png
Views:	1
Size:	161.3 KB
ID:	1787579

        Attached Files
        Last edited by Choonjoo Lee; 21 Oct 2026, 06:27.

        Comment


        • #19
          The differences between graph bar and twoway bar are puzzling even to many experienced users. Only occasionally do I want to combine bars and lines and it's programmable, just not with graph bar.

          I would be very, very surprised if addplot() could be added to graph bar etc. It's based on utterly different principles. But if StataCorp can make that surprise of mine look ignorant and foolish, that's good for users and salutary for me.

          As I understand it way back in Stata 8 twoway and the other branches of graph were kept separate with great reluctance but out of necessity.

          The point can be made otherwise for those who know R, either slightly (as in my case) or well. If you know its ggplot2 you will know that it covers most kinds of graphs, but not all. (At least that's my understanding.) The underlying reason is the same.

          Pinging @Hua Peng here: Hua is however excused from promises that may be hard for StataCorp to keep. At Stata users' meetings and conferences the standard responses to community requests are "we'll do that" and "we'll think about that".

          Comment


          • #20
            Fair point—I don't have a citation for 'pseudo bars' in the literature. The term is my own coinage, meant to distinguish this from the standard twoway bar / graph bar behavior.

            I agree twoway bars is too close to twoway bar. If StataCorp ever implements this, I'm happy to let them pick the name. And I suggest Choonjoo Lee submit this new command to SSC, with a name whatever he likes. (How about add a base() option? Or baropts(base(#)))

            Your point about commands growing out of repeated patterns is exactly how pseudobars came about—I kept writing twoway bar ... || twoway bar ... with manual x-offsets, and decided to bundle it. Of course, there are other reasons, and some of them had been put above.

            Comment


            • #21
              In Stata 19 03jun2026 update, we added:

              Code:
                4. graph twoway bar and graph twoway rbar have new option baroffset(#) for offsetting the centers of the bars.
              In -help twoway bar-:

              Code:
              baroffset(#) offsets the center of the bar by # times the bar width set in barwidth().  The center is moved
                      left if # is negative and right if # is positive.  For horizontal bars, the center is moved down if #
                      is negative and up if # is positive.
              back to Chen Samulsion's original question, we may use the baroffset() option instead creating Y1-Y4 variables:

              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
              
              twoway (bar GR Years, barwidth(0.2) baroffset(-1.5)) ///
                     (bar PB Years, barwidth(0.2) baroffset(-0.5)) ///
                     (bar CH Years, barwidth(0.2) baroffset(0.5)) ///
                     (bar FC Years, barwidth(0.2) baroffset(1.5 )) ///
                     (line Totalcriees Years), xlabel(2010/2021)
              Click image for larger version

Name:	bar_offset.png
Views:	1
Size:	50.4 KB
ID:	1787588
              Last edited by Hua Peng (StataCorp); 21 Oct 2026, 07:08.

              Comment


              • #22
                Chen Samulsion
                Thank you, Chen, for the suggestion about SSC and for proposing base(). Your original idea, examples and feedback have been central to this wrapper.

                I have now added base(#), with baropts(base(#)) as an equivalent spelling. For example:

                Code:
                pseudobars index_a index_b, over(year) base(100)
                The default remains zero. The selected reference applies to all bars and also works with panels; the data and line values remain unchanged. Please specify the reference in just one of the two places.

                I see this as a contribution to our discussion, using standard Stata graphing techniques. Please feel free to use, modify, extend, rename or redistribute the code without asking me first. I have acknowledged your contributions and Nick's advice in the help file.
                Attached Files
                Last edited by Choonjoo Lee; 21 Oct 2026, 07:28.

                Comment


                • #23
                  Thank you, Hua, for the clear example and for highlighting baroffset(). This provides a direct solution in updated Stata 19. The wrapper shared here automates the positioning across multiple series and also works in Stata 16.1, where I tested it. It is helpful to have the official approach documented alongside it.

                  Comment


                  • #24
                    Choonjoo Lee , baroffset() is a simple tool to avoid the need of creating extra variables. Your command does a lot more (besides works for older version of Stata).

                    Comment


                    • #25
                      Dear Choonjoo Lee, thank you for the kind words and for the swift update.

                      The speed and care with which you turned that into a working command, and then revised it in response to the discussion, is genuinely impressive. The base(#) option is a good addition; being able to set a common reference point without touching the underlying data or line values will make the command more useful in exactly the kind of applied settings where graph bar and twoway bar each fall short.

                      I also appreciate your note that this is a contribution to the discussion using standard Stata graphing techniques. That framing is apt, and it is generous of you to say the original idea and feedback were central. I noticed you have recorded both my contributions and Nick's in the help file—thank you for that, though for my part it feels like more credit than I deserve, since the implementation is entirely yours. The real credit belongs to you.

                      Thank you also for the permission to use, modify, or rename the code. I hope others will pick it up and adapt it to their own needs; the more people try it in different settings, the clearer its useful boundaries will become. I look forward to seeing how it evolves.

                      Comment


                      • #26
                        Just as what I posted in Stata 20 wishlist #182, MS Excel make such graph very easily. Using -pseudobars-, I want to reproduce such graph as below, But failed. What had I missed in specifing options?

                        Code:
                        sysuse sp500
                        keep in 1/21
                        pseudobars open high low close, over(date) ///
                                  baropts(base(1220) yaxis(1)) ///
                                  addvars(volume) addplot(line volume date, yaxis(2)) ///
                                  xlabel(, angle(45)) xtitle("") ///
                                  legend(pos(6) label(5 "Volume (thousands)") row(1))
                        Click image for larger version

Name:	Excel_sp500.png
Views:	2
Size:	67.3 KB
ID:	1787603


                        Click image for larger version

Name:	Graph.png
Views:	1
Size:	340.9 KB
ID:	1787604

                        Comment


                        • #27
                          Chen Samulsion

                          Thank you, Chen, for this helpful example. Use categorical to space the observed trading dates equally. I have also updated the wrapper to preserve date formatting on the categorical axis.
                          For the volume series, line(volume) lineopts(yaxis(2)) provides a simpler specification. The example below includes all five legend entries and sets the right-hand scale to match your Excel example. Tested in Stata 16.1.
                          Code:
                           sysuse sp500, clear keep in 1/21 format date %tdDD-Mon-YY
                          Code:
                           pseudobars open high low close, over(date) categorical base(1220) ///     line(volume) lineopts(yaxis(2)) ///     xlabel(, angle(45) labsize(vsmall)) xtitle("") ///     ylabel(1220(20)1400, axis(1) angle(horizontal) labsize(vsmall)) ///     ylabel(0(5000)25000, axis(2) angle(horizontal) labsize(vsmall)) ///     yscale(range(0 25000) axis(2)) ///     ytitle("Price index", axis(1) size(vsmall) margin(medsmall)) ///     ytitle("Volume (thousands)", axis(2) size(vsmall) margin(medsmall)) ///     legend(order(1 "Open" 2 "High" 3 "Low" 4 "Close" ///         5 "Volume (thousands)") pos(6) rows(1) size(vsmall) ///         symxsize(3) symysize(2) keygap(1) colgap(2) ///         region(margin(medsmall)))
                          If you prefer addplot(), use addvars(volume) addplot(line volume @x, yaxis(2)); @x keeps the line aligned with the categorical positions. With this approach, legend(order(...)) is needed to include the added line in the legend.
                          Click image for larger version

Name:	chen_sp500_builtin.png
Views:	1
Size:	136.4 KB
ID:	1787607


                          As a small related experiment, I also explored coordinate transformations for 3D-style views using standard Stata twoway plots. The attached figure shows the same fitted surface from simulated data as contours, slices and two projected views. The predictions remain unchanged; only their display coordinates change. This illustrates how a shared transformation can keep the surface, axes and confidence intervals aligned. It is a separate experiment, not a 3D feature of pseudobars; the intervals shown are pointwise 95% confidence intervals for the fitted mean.
                          This experiment extends the coordinate-based approach used in pseudobars: calculate plotting positions first, then apply a consistent transformation to the graphical elements. Here, the same principle is used to project a fitted surface, its axes and confidence intervals into a 2D Stata graph.
                          The attached do-file is self-contained and reproduces all the static figures using simulated data and built-in Stata commands. Tested in Stata 16.1; no additional packages are required.
                          Attached Files

                          Comment


                          • #28
                            Thank you so much Choonjoo Lee. That's amazing! It feels like Stata is a kaleidoscope in your hands. You keep finding new ways to make it work.

                            Comment

                            Working...
                            X