Announcement

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

  • How Do I Position A Tick Mark to the Right of Bars in Bar Graphs?

    Hi All,

    I am interested in displaying a bar chart by decile cut-offs, and so would like each tick mark to appear to the right of each bar. So for example in the graph attached, it would be clear that the first bar on the left corresponds to the bin from 0 to 2020. I do not want to simply relabel each of these values by their range (0-2020 etc.), as I will have to run these graphs by group and it will be too cluttered for display. My efforts exploring tick-placement options haven't been fruitful - anyone have any idea how to do this? For example, suppose I was interested in displaying the mean car price by weight decile:

    sysuse auto, clear
    xtile psize = weight, nquantiles(10)
    _pctile weight, nq(10)
    gen psizelab = .
    forvalues x = 1(1)9 {
    replace psizelab = r(r`x') if psize == `x'
    }
    quietly: summ weight, d
    replace psizelab = r(p99) if psize == 10
    collapse (mean) price (first) psizelab, by(psize)
    labmask psize, values(psizelab)
    graph twoway bar price psize, xlabel(1 2 3 4 5 6 7 8 9 10, valuelabel)

    Is there a way to alter the bar chart style or tick option such that the ticks are placed to the right of each bar? Thanks!

  • #2
    Sure. But it is easier to think of just shifting the bars half a unit width to the left. In this case, we shift the bars to be centred on 0.5(1)9.5 and show ticks with value labels at 1(1)10. Bar width defaults to 1; if we needed a different width, we could naturally specify it.

    Code:
    sysuse auto, clear
    xtile psize = weight, nquantiles(10)
    _pctile weight, nq(10)
    gen psizelab = .
    forvalues x = 1(1)9 {
    replace psizelab = r(r`x') if psize == `x'
    }
    quietly: summ weight, d
    replace psizelab = r(p99) if psize == 10
    collapse (mean) price (first) psizelab, by(psize)
    forval j = 1/10 {
       label def psize `j' "`=psizelab[`j']'", modify
    }
    label val psize psize
    replace psize = psize - 0.5
    label li
    graph twoway bar price psize, xlabel(1/10, valuelabel)
    I don't know why you used r(p99); why not r(max)?

    Some points of procedure, both explained in the FAQ:

    1. You used labmask here from SJ, so you are asked to explain where it comes from, as a user-written command. (I didn't use it, as my values at which value labels are to be shown don't occur in the data, but that's not a problem.)

    2. Inserting graphs as .png attachments works better than inserting photos.

    Click image for larger version

Name:	decilebar.png
Views:	3
Size:	26.7 KB
ID:	200799
    Attached Files
    Last edited by Nick Cox; 02 Sep 2014, 12:00.

    Comment


    • #3
      Thank you Nick! This is great - worked beautifully. Also sorry about the lack of etiquette (novice Statalister here). I used the user-written command labmask, which can be found using SSC (-labutil-) and in SJ. Otherwise you're spot on about r(max).

      Comment

      Working...
      X