Announcement

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

  • showing percentages in a bar graph across subgroup.

    Hello all,

    Please, I am trying to have the figures of the below bar graph disaggregated along gender as indicated in the table below:
    Rural
    Male Female
    n (%) n (%)
    D1: At what age do you think menstruation starts?
    10-12 years 4 (5.6%) 10 (12.2%)
    13-15 years 53 (73.6%) 69 (84.1%)
    16 years and above 5 (6.9%) 3 (3.7%)
    Don't know 10 (13.9%)
    Click image for larger version

Name:	Graph11.jpg
Views:	1
Size:	37.5 KB
ID:	1767406

    I want it to show the percentages as shown in this table instead of what it is showing at the moment. Moreso, is it possible one has "n (%)" format for the bar labels?

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte state long area byte(sex menage)
    1 1 1  2
    1 1 1  2
    1 1 1  2
    1 1 1  2
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  3
    1 1 1  4
    1 1 1  4
    1 1 1  4
    1 1 1  4
    1 1 1  4
    1 1 1 98
    1 1 1 98
    1 1 1 98
    1 1 1 98
    1 1 1 98
    1 1 1 98
    1 1 1 98
    1 1 1 98
    1 1 1 98
    1 1 1 98
    1 1 2  2
    1 1 2  2
    1 1 2  2
    1 1 2  2
    1 1 2  2
    1 1 2  2
    1 1 2  2
    1 1 2  2
    1 1 2  2
    1 1 2  2
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  3
    1 1 2  4
    1 1 2  4
    1 1 2  4
    1 2 1  2
    1 2 1  2
    1 2 1  2
    1 2 1  2
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  3
    1 2 1  4
    1 2 1  4
    1 2 1  4
    1 2 1  4
    1 2 1  4
    1 2 1  4
    1 2 1  4
    1 2 1  4
    1 2 1  4
    1 2 1  4
    1 2 1  4
    1 2 1  4
    1 2 1 98
    1 2 1 98
    1 2 1 98
    1 2 1 98
    1 2 1 98
    1 2 1 98
    1 2 1 98
    1 2 1 98
    1 2 1 98
    1 2 1 98
    1 2 1 98
    1 2 1 98
    1 2 1 98
    1 2 1 98
    1 2 1 98
    1 2 1 98
    1 2 2  2
    1 2 2  2
    1 2 2  2
    1 2 2  2
    1 2 2  2
    1 2 2  2
    1 2 2  2
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  3
    1 2 2  4
    1 2 2  4
    1 2 2  4
    1 2 2  4
    1 2 2  4
    1 2 2  4
    1 2 2  4
    1 2 2  4
    2 1 1  1
    2 1 1  1
    2 1 1  1
    2 1 1  1
    2 1 1  1
    2 1 1  1
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  2
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  3
    2 1 1  4
    2 1 1  4
    2 1 1  4
    2 1 1  4
    2 1 1  4
    2 1 1  4
    2 1 1  4
    2 1 1  4
    2 1 1  4
    2 1 1 98
    2 1 1 98
    2 1 1 98
    2 1 1 98
    2 1 1 98
    2 1 1 98
    2 1 1 98
    2 1 1 98
    2 1 1 98
    2 1 1 98
    2 1 1 98
    2 1 2  1
    2 1 2  1
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  2
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  3
    2 1 2  4
    2 1 2  4
    2 1 2  4
    2 1 2  4
    2 1 2  4
    2 1 2 98
    2 1 2 98
    2 2 1  1
    2 2 1  1
    2 2 1  1
    2 2 1  1
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  2
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  3
    2 2 1  4
    2 2 1  4
    2 2 1  4
    2 2 1  4
    2 2 1  4
    2 2 1  4
    2 2 1  4
    2 2 1  4
    2 2 1  4
    2 2 1  4
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 1 98
    2 2 2  1
    2 2 2  1
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  2
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  3
    2 2 2  4
    2 2 2  4
    2 2 2  4
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  2
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  3
    3 1 1  4
    3 1 1  4
    3 1 1  4
    3 1 1  4
    3 1 1  4
    3 1 1  4
    3 1 1  4
    3 1 1  4
    3 1 1  4
    3 1 1  4
    3 1 1  4
    3 1 1  4
    3 1 1  4
    3 1 1  4
    3 1 1 98
    3 1 1 98
    3 1 1 98
    3 1 1 98
    3 1 1 98
    3 1 1 98
    3 1 1 98
    3 1 1 98
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  2
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  3
    3 1 2  4
    3 1 2  4
    3 1 2  4
    3 1 2  4
    3 1 2  4
    3 1 2  4
    3 1 2  4
    3 1 2  4
    3 1 2  4
    3 1 2  4
    3 1 2  4
    3 1 2  4
    3 1 2  4
    3 1 2 98
    3 1 2 98
    3 1 2 98
    3 2 1  1
    3 2 1  1
    3 2 1  1
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  2
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  3
    3 2 1  4
    3 2 1  4
    3 2 1  4
    3 2 1  4
    3 2 1  4
    3 2 1  4
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 1 98
    3 2 2  2
    3 2 2  2
    3 2 2  2
    3 2 2  2
    3 2 2  2
    3 2 2  2
    3 2 2  2
    3 2 2  2
    3 2 2  2
    3 2 2  2
    3 2 2  2
    3 2 2  2
    end
    label values state state
    label def state 1 "Adamawa State", modify
    label def state 2 "Katsina State", modify
    label def state 3 "Kebbi State", modify
    label values area area
    label def area 1 "Rural", modify
    label def area 2 "Urban", modify
    label values sex sex
    label def sex 1 "Male", modify
    label def sex 2 "Female", modify
    label values menage menage
    label def menage 1 "Before 10 years", modify
    label def menage 2 "10-12 years", modify
    label def menage 3 "13-15 years", modify
    label def menage 4 "16 years and above", modify
    label def menage 98 "Don't know", modify


    Code:
    splitvallabels menage
    catplot if area==1, ///
    by(state) ///
    over(menage, label(nolabel) relabel(`r(relabel)')) ///
    over(sex, label(labsize(vsmall))) ///
    percent(menage) ///
    blabel(bar, format(%2.0f) orientation(vertical) position(outside) size(vsmall)) ysc(r(0 105)) ///
    recast(bar) ///
    intensity(50) ///
    asyvars ///
    ytitle("Percent of Respondents by Gender", size(vsmall)) ///
    
    local ng=`.Graph.graphs.arrnels'
    forval g=1/`ng' {
        local nb=`.Graph.graphs[`g'].plotregion1.barlabels.arrnels'
        forval i=1/`nb' {
          di "`.Graph.graphs[`g'].plotregion1.barlabels[`i'].text[1]'"
          .Graph.graphs[`g'].plotregion1.barlabels[`i'].text[1]="`.Graph.graphs[`g'].plotregion1.barlabels[`i'].text[1]'%"
        }
    }
    .Graph.drawgraph

  • #2
    Thanks for the detailed data example. To keep things as clear and as simple as possible,

    0. Please note that you're asked to explain where community-contributed commands you refer to come from (FAQ Advice #12).

    1. I ignored the use of splitvallabels (from SSC). It often appeals, but I just shortened the value labels, yet naturally it's your choice.

    2. I ignored the use of the Graph Editor. Although (personal prejudice coming up) I think the use of % on every percentage is poor style, I show how to do that differently.

    3. I ignored the use of catplot (from SSC). I don't have prejudice against it -- I wrote it... -- but for your problem I think you can do better. Lose the legend! kill the key! (if you can) is my advice.

    4. I used contract to reduce the example dataset. For your purposes, there is no gain in that reduction, and you should reinstate if area == 1 and remove references to _freq as a weight.

    I think the main point troubling you is that you have the percent() option in catplot the wrong way round: it is not percent(response) but percent(predictor(s)).

    I used tabplot from the Stata Journal. The percent() option works in the same way as it does for catplot.

    Code for producing this data example

    Code:
    keep if area == 1 
    
    drop area 
    
    contract * 
    
    dataex
    Self-contained code to get these graphs

    Many details are up for discussion, such as use of colour and order of categories.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(state sex menage _freq)
    1 1  2  4
    1 1  3 53
    1 1  4  5
    1 1 98 10
    1 2  2 10
    1 2  3 69
    1 2  4  3
    2 1  1  6
    2 1  2 21
    2 1  3 47
    2 1  4  9
    2 1 98 11
    2 2  1  2
    2 2  2 30
    2 2  3 57
    2 2  4  5
    2 2 98  2
    3 1  2 27
    3 1  3 39
    3 1  4 14
    3 1 98  8
    3 2  2 32
    3 2  3 38
    3 2  4 13
    3 2 98  3
    end
    
    label values state state
    label def state 1 "Adamawa", modify
    label def state 2 "Katsina", modify
    label def state 3 "Kebbi", modify
    
    label values sex sex
    label def sex 1 "Male", modify
    label def sex 2 "Female", modify
    
    label values menage menage
    label def menage 1 "Before 10", modify
    label def menage 2 "10-12", modify
    label def menage 3 "13-15", modify
    label def menage 4 "16 and above", modify
    label def menage 98 "Don't know", modify
    
    set scheme stcolor 
    
    tabplot menage state [fw=_freq], by(sex, note(Percent of respondents)) ///
    percent(state sex) showval separate(menage) ///
    ytitle(At what age do you think menstruation starts?) xtitle("")  name(MA1, replace)
    
    egen total = total(_freq), by(state sex)
    gen toshow = strofreal(100 * _freq / total, "%2.1f") + "%"
    
    tabplot menage state [fw=_freq], by(sex, note(Percent of respondents)) ///
    percent(state sex) showval(toshow) separate(menage) ///
    ytitle(At what age do you think menstruation starts?) xtitle("") name(MA2, replace)
    Click image for larger version

Name:	MA1.png
Views:	1
Size:	36.7 KB
ID:	1767414
    Click image for larger version

Name:	MA2.png
Views:	1
Size:	39.3 KB
ID:	1767415

    Comment


    • #3
      Thank you a million. For the purpose I need the graph for, how do I generate the _freq variable. I prefer the second graph but I need to add frequency to the labels, such as n=34 (13.9%) for example.

      Thanks.

      Comment


      • #4
        The code already shows how _freq is produced -- by using contract. This is the modification you seek

        Code:
        gen toshow = strofreal(_freq) + " (" + strofreal(100 * _freq / total, "%2.1f") + "%)"

        Comment


        • #5
          Very grateful. I edited the contract as you pointed out.

          Code:
          set scheme stcolor 
          preserve
          cap drop total
          cap drop toshow
          contract state area sex menage
          egen total = total(_freq), by(state area sex)
          gen toshow = "n=" + strofreal(_freq) + " (" + strofreal(100 * _freq / total, "%2.1f") + "%)"
          
          tabplot menage state if area==1, by(sex, note(Percent of respondents)) ///
          percent(state area sex) showval(toshow, offset(0.10) mlabsize(vsmall)) separate(menage) ///
          ytitle(At what age do you think menstruation starts?) xtitle("") name(MA2, replace)
          restore
          Thank You.

          Click image for larger version

Name:	MA2.jpg
Views:	1
Size:	48.8 KB
ID:	1767428

          Comment


          • #6
            Thanks for the thanks, but your code mushes together two different approaches that aren't compatible. One way to see this is that the heights of your bars don't correspond to the calculated percents!

            That is because you need to declare frequency weights to tabplot if you use contract.

            Backing up: There are two ways to approach your problem.

            You could calculate the frequencies and percents without using contract -- but if you contract you must use the frequencies it produces as weights to get the correct heights for the bars.

            Here is the one thing once again. Compare the two graphs, which I think are identical.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input byte state long area byte(sex menage)
            1 1 1  2
            1 1 1  2
            1 1 1  2
            1 1 1  2
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  3
            1 1 1  4
            1 1 1  4
            1 1 1  4
            1 1 1  4
            1 1 1  4
            1 1 1 98
            1 1 1 98
            1 1 1 98
            1 1 1 98
            1 1 1 98
            1 1 1 98
            1 1 1 98
            1 1 1 98
            1 1 1 98
            1 1 1 98
            1 1 2  2
            1 1 2  2
            1 1 2  2
            1 1 2  2
            1 1 2  2
            1 1 2  2
            1 1 2  2
            1 1 2  2
            1 1 2  2
            1 1 2  2
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  3
            1 1 2  4
            1 1 2  4
            1 1 2  4
            1 2 1  2
            1 2 1  2
            1 2 1  2
            1 2 1  2
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  3
            1 2 1  4
            1 2 1  4
            1 2 1  4
            1 2 1  4
            1 2 1  4
            1 2 1  4
            1 2 1  4
            1 2 1  4
            1 2 1  4
            1 2 1  4
            1 2 1  4
            1 2 1  4
            1 2 1 98
            1 2 1 98
            1 2 1 98
            1 2 1 98
            1 2 1 98
            1 2 1 98
            1 2 1 98
            1 2 1 98
            1 2 1 98
            1 2 1 98
            1 2 1 98
            1 2 1 98
            1 2 1 98
            1 2 1 98
            1 2 1 98
            1 2 1 98
            1 2 2  2
            1 2 2  2
            1 2 2  2
            1 2 2  2
            1 2 2  2
            1 2 2  2
            1 2 2  2
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  3
            1 2 2  4
            1 2 2  4
            1 2 2  4
            1 2 2  4
            1 2 2  4
            1 2 2  4
            1 2 2  4
            1 2 2  4
            2 1 1  1
            2 1 1  1
            2 1 1  1
            2 1 1  1
            2 1 1  1
            2 1 1  1
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  2
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  3
            2 1 1  4
            2 1 1  4
            2 1 1  4
            2 1 1  4
            2 1 1  4
            2 1 1  4
            2 1 1  4
            2 1 1  4
            2 1 1  4
            2 1 1 98
            2 1 1 98
            2 1 1 98
            2 1 1 98
            2 1 1 98
            2 1 1 98
            2 1 1 98
            2 1 1 98
            2 1 1 98
            2 1 1 98
            2 1 1 98
            2 1 2  1
            2 1 2  1
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  2
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  3
            2 1 2  4
            2 1 2  4
            2 1 2  4
            2 1 2  4
            2 1 2  4
            2 1 2 98
            2 1 2 98
            2 2 1  1
            2 2 1  1
            2 2 1  1
            2 2 1  1
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  2
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  3
            2 2 1  4
            2 2 1  4
            2 2 1  4
            2 2 1  4
            2 2 1  4
            2 2 1  4
            2 2 1  4
            2 2 1  4
            2 2 1  4
            2 2 1  4
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 1 98
            2 2 2  1
            2 2 2  1
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  2
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  3
            2 2 2  4
            2 2 2  4
            2 2 2  4
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  2
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  3
            3 1 1  4
            3 1 1  4
            3 1 1  4
            3 1 1  4
            3 1 1  4
            3 1 1  4
            3 1 1  4
            3 1 1  4
            3 1 1  4
            3 1 1  4
            3 1 1  4
            3 1 1  4
            3 1 1  4
            3 1 1  4
            3 1 1 98
            3 1 1 98
            3 1 1 98
            3 1 1 98
            3 1 1 98
            3 1 1 98
            3 1 1 98
            3 1 1 98
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  2
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  3
            3 1 2  4
            3 1 2  4
            3 1 2  4
            3 1 2  4
            3 1 2  4
            3 1 2  4
            3 1 2  4
            3 1 2  4
            3 1 2  4
            3 1 2  4
            3 1 2  4
            3 1 2  4
            3 1 2  4
            3 1 2 98
            3 1 2 98
            3 1 2 98
            3 2 1  1
            3 2 1  1
            3 2 1  1
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  2
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  3
            3 2 1  4
            3 2 1  4
            3 2 1  4
            3 2 1  4
            3 2 1  4
            3 2 1  4
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 1 98
            3 2 2  2
            3 2 2  2
            3 2 2  2
            3 2 2  2
            3 2 2  2
            3 2 2  2
            3 2 2  2
            3 2 2  2
            3 2 2  2
            3 2 2  2
            3 2 2  2
            3 2 2  2
            end
            label values state state
            label def state 1 "Adamawa", modify
            label def state 2 "Katsina", modify
            label def state 3 "Kebbi", modify
            label values area area
            label def area 1 "Rural", modify
            label def area 2 "Urban", modify
            label values sex sex
            label def sex 1 "Male", modify
            label def sex 2 "Female", modify
            label values menage menage
            label def menage 1 "Before 10", modify
            label def menage 2 "10-12", modify
            label def menage 3 "13-15", modify
            label def menage 4 "16 and above", modify
            label def menage 98 "Don't know", modify
            
            set scheme stcolor 
            
            keep if area == 1
            
            bysort state sex (menage) : gen total1 = _N 
            bysort state sex menage: gen toshow1 = strofreal(100 * _N / total1, "%2.1f") + "%"
            
            tabplot menage state, by(sex, note(Percent of respondents)) ///
            percent(state sex) showval(toshow) separate(menage) ///
            ytitle(At what age do you think menstruation starts?) xtitle("") name(MA_1, replace)
            
            contract state sex menage 
            
            bysort state sex (menage) : egen total2 = total(_freq) 
            bysort state sex : gen toshow2 = strofreal(100 * _freq / total2, "%2.1f") + "%"
            
            tabplot menage state [fw=_freq], by(sex, note(Percent of respondents)) ///
            percent(state sex) showval(toshow2) separate(menage) ///
            ytitle(At what age do you think menstruation starts?) xtitle("") name(MA_2, replace)

            Comment


            • #7
              Thank you for the observation. I noticed it too but just couldn't get it clearer. I can see that the first code focuses on area==1 while the second that uses contract drops area entirely. However, my target is to be able to plot the graph for area=1 and area=2 and then merge them using grc1leg2 package from SSC. As it is now, the code does now allow that. Will using preserve with keep area==1 allow me to access the saved graph in memory in order to merge with keep area==2? The second code does not allow to do that at all. I would appreciate your further assistance. Thanks a million.
              Last edited by Kehinde Atoloye; 12 Nov 2024, 05:04.

              Comment


              • #8
                This is what I would do. I see no point in reaching for grc1leg2 here: there are no legends, so nothing to fix.

                I am not going to repeat the code from clear and input to end

                The example data seem incomplete. I would get into the Graph Editor to hide the y axis labels for Urban if this were my project.

                Code:
                * code omitted to read in the data
                label values state state
                label def state 1 "Adamawa", modify
                label def state 2 "Katsina", modify
                label def state 3 "Kebbi", modify
                label values area area
                label def area 1 "Rural", modify
                label def area 2 "Urban", modify
                label values sex sex
                label def sex 1 "Male", modify
                label def sex 2 "Female", modify
                label values menage menage
                label def menage 1 "Before 10", modify
                label def menage 2 "10-12", modify
                label def menage 3 "13-15", modify
                label def menage 4 "16 and above", modify
                label def menage 98 "Don't know", modify
                
                set scheme stcolor
                
                contract state sex menage area
                
                bysort area state sex (menage) : egen total = total(_freq)
                bysort area state sex menage: gen toshow = strofreal(100 * _freq / total, "%2.1f") + "%"
                
                tabplot menage state if area == 1 [fw=_freq], by(sex, compact note("") title(Rural) ) ///
                percent(state sex) showval(toshow) separate(menage) ///
                ytitle("") xtitle("") name(MA_rural, replace)
                
                tabplot menage state if area == 2 [fw=_freq], by(sex,  compact note("") title(Urban)) ///
                percent(state sex) showval(toshow) separate(menage) ///
                ytitle("") xtitle("") name(MA_urban, replace)
                
                graph combine MA_rural MA_urban, imargin(zero) note(Percent of respondents) l2title(At what age do you think menstruation starts?)

                Click image for larger version

Name:	MA-both.png
Views:	1
Size:	108.6 KB
ID:	1767461


                Comment


                • #9
                  This is well appreciated.

                  Comment


                  • #10
                    Originally posted by Nick Cox View Post
                    I am sorry, I have to come back to you one more time. I tried editing the code to cater for other variables and scenarios but seem to be having some challenges. For variables where responses are available for only the female gender, I decide to directly disaggregate by area but unfortunately, the percentages are not adding up to 100. The frequencies are correct though. I have done different maneuvers but to no avail. I just can't lay my hands on the cause.

                    Code:
                    * Example generated by -dataex-. For more info, type help dataex
                    clear
                    input byte(state mh13 area _freq)
                    1 1 1 12
                    1 2 1 29
                    1 3 1 17
                    1 . 1 24
                    1 1 2  9
                    1 2 2 36
                    1 3 2 21
                    1 . 2 13
                    2 1 1 37
                    2 2 1 31
                    2 3 1  5
                    2 . 1 23
                    2 1 2 15
                    2 2 2 29
                    2 3 2 24
                    2 . 2 38
                    3 1 1 11
                    3 2 1 23
                    3 3 1  1
                    3 . 1 51
                    3 1 2 15
                    3 2 2 24
                    3 3 2 10
                    3 . 2 55
                    4 1 1  5
                    4 2 1 18
                    4 3 1  8
                    4 . 1 17
                    4 1 2 17
                    4 2 2 32
                    4 3 2  8
                    4 . 2  7
                    5 1 1  2
                    5 2 1 14
                    5 3 1 12
                    5 . 1 66
                    5 1 2  9
                    5 2 2 50
                    5 3 2 14
                    5 . 2 45
                    end
                    label values state state
                    label def state 1 "State1", modify
                    label def state 2 "State2", modify
                    label def state 3 "State3", modify
                    label def state 4 "State4", modify
                    label def state 5 "State5", modify
                    label values mh13 mh13
                    label def mh13 1 "Very affordable", modify
                    label def mh13 2 "Somewhat affordable", modify
                    label def mh13 3 "Not affordable", modify
                    label values area area
                    label def area 1 "Rural", modify
                    label def area 2 "Urban", modify
                    Code:
                    // keep if sex==2
                    // drop sex
                    set scheme stcolor
                    // cap drop _freq
                    cap graph drop Comfdisc
                    
                    // contract state area mh13
                    
                    bysort state area (mh13): egen total = total(_freq)
                    bysort state area : gen toshow = "n=" + strofreal(_freq) + "(" + strofreal(100 * _freq / total, "%2.1f") + "%)"
                    
                    tabplot mh13 state [fw=_freq], by(area, compact note("") title(Comfortable talking about period with family members) subtitle(Percent of respondents (Female Respondents))) ///
                    percent(area state) showval(toshow, offset(0.10) mlabsize(vsmall)) separate(mh13) barwidth(1.0) ///
                    ytitle("Comfortable") xtitle("State") name(Comfdisc, replace)
                    Click image for larger version

Name:	image_36313.jpg
Views:	1
Size:	56.0 KB
ID:	1767510

                    Last edited by Kehinde Atoloye; 12 Nov 2024, 15:33.

                    Comment


                    • #11
                      I think you are getting inconsistent results because your calculation of the percents to show is based on totals that include missing on your outcome, but tabplot by default ignores missing values.


                      If you want to exclude missings, you need something like

                      Code:
                      bysort state area (mh13): egen total = total(_freq * !missing(mh13))
                      If you want to include missings, you need the missing option in tabplot.

                      Comment


                      • #12
                        Originally posted by Nick Cox View Post
                        that's right. I also later figured it out to be due to the missing values. thank you for your support. I already used drop if missing(mh13) but your approach of total(_freq * !missing(mh13)) is very interesting. thank you for your support.


                        Comment

                        Working...
                        X