Announcement

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

  • Creating a bar graph for dummy variable

    Hi,
    I have a (probably fairly easy question) where I cannot find a proper solution.
    I have a big data set where observations are sampled within 5 countries and I have a dummy variable., where I want to display the percentages for 1 within the country with a horizontal mean line.
    Now I want to create a bar graph that displays the percentages of saying 1 in that dummy variable within the countries. Additionally, I want to include a horizontal line at the overall mean of saying "Yes".

    More specifically, I have
    country = 1(1)5, testlang == 0|1, and I want to display the percentages within the different countries and total.

    I presume I have to use a twoway plot but mostly it results in the plot where 0 & 1 are displayed.

    I hope that the information is sufficient and you can help me.
    Thank you very much in advance,

    Hannah

  • #2
    We can't comment on code you allude to but don't show us. Further, there is no data example here.

    The percent of 1 for a 0. 1 variable is just the mean of that variable on a percent scale. So if I have 0 0 0 1 1 1 1 1 1 1 the mean is 0.7 or 70% and that is the fraction (percent) of 1.

    Thus this sequence which you can run for yourself may be instructive.

    Code:
    sysuse auto, clear
    graph bar foreign, over(rep78)
    graph dot foreign, over(rep78)
    graph dot foreign, over(rep78) vertical linetype(line) lines(lc(gs8) lw(vthin)) yla(0 0.25 "25" 0.5 "50" 0.75 "75" 1 "100", ang(h)) ytitle(Percent foreign cars)
    There are several large and small points here.

    1. graph bar by default shows means of the outcome here a 0. 1 variable foreign.

    2. graph dot is an alternative that should be used much more. For example, with bars it is usually true that zero should be shown. But if the results you want to show do not vary much compared with zero, you waste space on showing that they are more or less the same. Most of the time the interest is in how categories compare with each other, not with zero. graph dot frees you from the obligation to show zero, and there are other advantages. Just one more: graph dot is compatible with logarithmic scale, which is not the case for graph bar.

    3. graph dot has an undocumented vertical option. Naturally, it is not compulsory, and indeed dot charts and bar charts too are often better aligned horizontally, leaving space for category labels to be shown fully and legibly.

    4. I find that the default dotted grid with graph dot does not port well to other software. I tend to use thin grey solid lines instead.

    5. Showing percents rather than fractions can be just a matter of fixing axis labels.

    6. Naturally you should reach in and use informative and appropriate titles.

    In this case, the overall mean can be found from summarize foreign and you can add a line with an extra option such as yline(.297)

    Comment


    • #3
      Hi Nick,
      Thank you for the answer.
      Indeed, I had to show you my output.
      I have solved the problem differently by use of the toolbar. There, I tried to alter my displayed graph (luckily, in Stata 17 the alteration are displayed as a command and, thus, they are easily traceable.
      I, finally, used graph bar, over(spktestlang) blabel(bar) by(country.
      I am sorry for the late reply and the poor description of my question.

      Comment

      Working...
      X