Announcement

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

  • Adding dollar signs ($) to blabel in Stata

    Hi! When I create a bar graph, I added blabel(bar) to each to show the monetary value above each bar. However, it only shows the value and doesn't have a dollar sign before it (i.e. it shows 100 rather than $100). I know I can go in Stata Graph Editor and manually change all of them by typing a dollar sign before the value. I was wondering if there is any way to manually code it into my graph.

    Thank you in advance!

  • #2
    My recommendation is to label the y-axis as "Price in dollars" instead of including dollar signs everywhere. gr bar makes this difficult, and the advice is usually to switch to twoway bar. However, the undocumented command gr_edit offers a workaround.

    Code:
    sysuse auto, clear
    keep in 4/8
    *YOUR GRAPH COMMAND
    set scheme s1color
    gr bar price, over(make) blabel(total) ytitle("") bar(1, color(blue%40))
    *ADD DOLLAR SIGNS
    forval i=1/`.Graph.plotregion1.barlabels.arrnels' {
        gr_edit .plotregion1.barlabels[`i'].text[1]="$`.Graph.plotregion1.barlabels[`i'].text[1]'"
    }
    Res.:
    Click image for larger version

Name:	Graph.png
Views:	1
Size:	22.1 KB
ID:	1612144

    Comment


    • #3
      Wow!!! This worked! Thank you SO much! The only issue is that the original colors of the graph went away when I used this new code - if you have any tips for that. But it did the $ job perfectly! Thank you.

      Comment


      • #4
        Oh, sorry! I forgot the "keep in 4/8" line. That makes the colors the same. Thank you!

        Comment


        • #5
          You can format the graph however you want. The default scheme is s2color

          Code:
          set scheme s2color
          Above, I used "s1color". For the bars, I specified the colors using the highlighted code:

          Code:
          gr bar price, over(make) blabel(total) ytitle("") bar(1, color(blue%40))
          i.e., blue color with 40% transparency. You can change these as well. If you want all the defaults

          Code:
          sysuse auto, clear
          gr bar price, over(make) blabel(total) ytitle("")
          But the relevant code that you attach to your already made graph to add dollar signs is

          Code:
          *ADD DOLLAR SIGNS
          forval i=1/`.Graph.plotregion1.barlabels.arrnels' {
              gr_edit .plotregion1.barlabels[`i'].text[1]="$`.Graph.plotregion1.barlabels[`i'].text[1]'"
          }
          Last edited by Andrew Musau; 28 May 2021, 13:04.

          Comment

          Working...
          X