Announcement

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

  • Bar chart beginner help


    Hi , I am trying to recreate the following graph with my own data:


    Screenshot 2022-01-04 125917.png


    Just an example so you can get an idea of data:

    country ownsaphone year
    Benin no 2016
    Benin yes 2021
    Benin no 2021
    Benin yes 2016
    Botswana no 2016
    Botswana yes 2016
    Botswana no 2021
    Botswana yes 2021
    Burkina faso no 2016
    Burkina faso yes 2016
    Burkina faso no 2021
    Burkina faso yes 2021
    Cameroon no 2016
    Cameroon yes 2016
    Cameroon no 2021
    Cameroon yes 2021
    Conte d Ivoire no 2016
    Conte d Ivoire yes 2016
    Conte d Ivoire no 2021
    Conte d Ivoire yes 2021
    Gabon no 2016
    Gabon yes 2016
    Gabon no 2021
    Gabon yes 2021
    Ghana no 2016
    Ghana yes 2016
    Ghana no 2021
    Ghana yes 2021
    I tried the following :

    graph bar (percent) ownsaphone if ownsaphone == 1, over(year) over (country) asyvars blabel (bar, format(%9.1f))

    Screenshot 2022-01-04 133107.png


    Screenshot 2022-01-04 134224.png


    Obviously, this was not the result that I wanted . I need the bars to show the percentage of people that do own a phone. For example, for Benin in 2021 the bar should show that 77.58% of people owns a phone. I now know that I have to pre-calculate the percentage before. However, I am having trouble with this. Any help would be appreciated - thanks.




  • #2
    Code:
    graph bar ownsaphone, over(year) over (country) asyvars blabel(bar, format(%9.1f))
    will show the mean over values of 0 and 1 which is just the proportion with a phone.

    To see the mean as a percent on bar totals and the axis, just multiply by 100.

    Code:
    gen ownsaphone2 = ownsaphone * 100
    and use that variable instead.

    The fallacy in #1 is common and came up only a few days ago with some else doing bar charts. If you condition on ownsaphone being 1, Stata ignores observations with value 0, and the percents calculated will be wrong. They are wrong for other reasons, as you are asking for the percents of the entire data plotted being in each (country, year) pair, which is not at all the same as the percent having a phone in each country and year.

    That said, the graph will be a mess unless you tidy up the use of upper and lower case in country names and change to graph hbar or graph dot. Note spellings: Côte (nor Conte) d'Ivoire and Burkina Faso.

    Another thought: if you show bar values, you don't need the magnitude axis labels too.

    Comment

    Working...
    X