Skip to content

Instantly share code, notes, and snippets.

@seemantk
Last active August 22, 2016 14:49
Show Gist options
  • Save seemantk/9322bd4124af02d44d596cdfd247459c to your computer and use it in GitHub Desktop.
Save seemantk/9322bd4124af02d44d596cdfd247459c to your computer and use it in GitHub Desktop.
Bar Chart IV: Switchable bar charts

Visualizing causes of death in Zambia: IV

From the 2015 Living Conditions Survey

Introduction

We left off with a basic bar chart in d3v4 showing the number of people who died in Zambia last year by given causes. Since our dataset is richer than that, we'll add gender to the visualization. So, we'll show:

  1. As before, the total number of people who died by each cause.
  2. The total number of females who died by each cause.
  3. The total number of males who died by each cause.

The first thing to note in this chart is that the radio buttons above the chart enable the user to choose which subset of the data they want to see. The heights of the bars change, or flash, to a new height representing the user's choice. So, if you choose "Male", the bars will change to show the number of male deaths for each cause.

For this example, we only need to know this: at any given time, the user can choose "Male", "Female", or "All" and the chart must change to correspond. We'll discover how that happens in the next example.

Fundamentals

Let's dive deeper into d3.nest() and d3.csv(). Please take the time to review the linked tutorial.

Previously, we partitioned the array by CAUSE, and simply had to get the length of the resulting sub-array belonging to each CAUSE.

Now we need to partition twice before counting. As before we partition the array by CAUSE. Then for each CAUSE, we partition its resulting array by SEX.

Now, we simply get the length of the resulting sub-array belonging to each SEX in each CAUSE.

Something to note. The dataset has SEX encoded as either "1" or "2". In the questionnaire used by the survey takers, they provide the following interpretation: 1 = Male, 2 = Female.

Beyond the Fundamentals

We need to change "1" to "Male" and "2" to "Female" in the SEX column for each record.

  • The perfect time to do this is as we're reading in each row.
  • Fortunately, d3.csv() lets us do that.
    • We use a Javascript Object as a dictionary in function row to decode the gender field.

We need to partition the data twice. So, we use nest.key() twice:

     d3.nest()
       .key(function(d) { return d.CAUSE; })
       .key(function(d) { return d.SEX; })
       .rollup(function(leaves) { return leaves.length; })
       .entries(data)

This looks just like the previous examples, but with one extra nest.key() step. We would wind up with an array of objects that look something like:

    {
        key: "FEVER/MALARIA"
      , values: [{key: "male", value: 113}, {key: "female", value: 95}]
    }

This is a great start. We show the all causes (keys) on the x-axis, so it makes sense to have that as the top-level key. Let's think through how we'll use the values part of the data object.

In the above example, values is an array of two objects, one object represents male deaths, and the other object represents female deaths. Since we want the user to be able to choose which subset of the data they want to see

 d3.nest()
         .key(function(d) { return d.CAUSE; })
         .key(function(d) { return d.SEX; })
         .rollup(function(leaves) { return leaves.length; })
         .entries(data)
         .map(function(d) {
             var obj = {
                   cause: d.key
                 , all: d3.sum(d.values, function(v) { return v.value; })
               }
             ;
             d.values.forEach(function(v) {  obj[v.key] = v.value; });

             return obj;
           })

And this results in an array of objects that look like:

    {
        cause: "FEVER/MALARIA"
      , all: 208
      , female: 95
      , male: 113
    }

This is a nice, straightforward data format for us to use to power all three bar charts.

Further Discussion

Next time, we'll explore how to cause the HTML elements to trigger change in the chart, to show our new enriched dataset.

AGE SEX CAUSE OTHER
38 1 FEVER/MALARIA
45 1 DIARRHOEA WITH BLOOD
62 2 COUGH/COLD/CHEST INFECTION
37 2 FEVER/MALARIA
N/A
35 1 DON'T KNOW
45 2 ABDOMINAL PAINS
20 2 LACK OF BLOOD/ANEAMIA
1 2 FEVER/MALARIA
31 2 LACK OF BLOOD/ANEAMIA
86 2 PNEUMONIA/CHEST PAIN
5 1 ACCIDENT
52 1 CANCER OF ANY KIND
23 1 FEVER/MALARIA
4 1 FEVER/MALARIA
55 2 CANCER OF ANY KIND
50 1 DIABETES/SUGAR DISEASE
N/A
31 2 LACK OF BLOOD/ANEAMIA
7 2 LACK OF BLOOD/ANEAMIA
50 2 FEVER/MALARIA
62 2 STROKE
78 1 ABDOMINAL PAINS
40 1 HYPERTENSION
48 1 TUBERCULOSIS
55 2 CANCER OF ANY KIND
68 1 DON'T KNOW
28 1 FEVER/MALARIA
27 2 FEVER/MALARIA
70 2 FEVER/MALARIA
40 1 STROKE
58 1 TUBERCULOSIS
74 1 ABDOMINAL PAINS
45 1 SUICIDE
45 1 LACK OF BLOOD/ANEAMIA
29 2 HEADACHE
25 2 OTHER (SPECIFY) due to delivery
58 1 FEVER/MALARIA
N/A
10 2 FEVER/MALARIA
34 2 DON'T KNOW
55 1 TUBERCULOSIS
19 2 OTHER (SPECIFY) sufferd from heart problem
7 1 DON'T KNOW
17 2 FEVER/MALARIA
25 2 CONSTIPATION/STOMACH UPSET
2 2 FEVER/MALARIA
90 2 DON'T KNOW
59 1 PARALYSIS OF ANY KIND
30 2 OTHER (SPECIFY) SWELLING OF STOMACH
40 1 FEVER/MALARIA
45 1 OTHER (SPECIFY) COLLAPSET
39 1 HYPERTENSION
29 1 FEVER/MALARIA
28 2 FEVER/MALARIA
45 2 FEVER/MALARIA
5 1 CEREBRAL MALARIA
25 1 DIARRHOEA AND VOMITTING
22 1 OTHER (SPECIFY) he drowned in water
76 1 DON'T KNOW
22 2 OTHER (SPECIFY) LIVER PROBLEM
0 1 OTHER (SPECIFY) still birth
1 1 COUGH/COLD/CHEST INFECTION
7 2 CONSTIPATION/STOMACH UPSET
5 2 BRONCHITIS
56 2 HYPERTENSION
N/A
66 2 DON'T KNOW
73 2 LIVER INFECTION/SIDE PAIN
N/A
62 2 TUBERCULOSIS
31 1 DIABETES/SUGAR DISEASE
90 2 PARALYSIS OF ANY KIND
8 2 COUGH/COLD/CHEST INFECTION
20 2 FEVER/MALARIA
90 2 COUGH/COLD/CHEST INFECTION
72 1 HYPERTENSION
21 1 FEVER/MALARIA
75 2 COUGH/COLD/CHEST INFECTION
28 2 FEVER/MALARIA
72 1 DIARRHOEA WITH BLOOD
19 1 ACCIDENT
87 2 DON'T KNOW
2 2 FEVER/MALARIA
20 1 DIARRHOEA WITHOUT BLOOD
36 1 FEVER/MALARIA
78 1 CONSTIPATION/STOMACH UPSET
63 1 DIABETES/SUGAR DISEASE
88 1 BRONCHITIS
67 1 FEVER/MALARIA
28 1 ASTHMA
36 2 CANCER OF ANY KIND
16 1 DIARRHOEA AND VOMITTING
68 2 LIVER INFECTION/SIDE PAIN
63 2 COUGH/COLD/CHEST INFECTION
67 2 MURDERED
63 2 TUBERCULOSIS
67 1 DIABETES/SUGAR DISEASE
36 1 CANCER OF ANY KIND
14 1 OTHER (SPECIFY) Epilepsy
2 2 OTHER (SPECIFY) bemuikila mankwala mumala
0 1 FEVER/MALARIA
7 1 DIARRHOEA AND VOMITTING
2 2 DIARRHOEA AND VOMITTING
78 1 TUBERCULOSIS
60 2 FEVER/MALARIA
59 1 FEVER/MALARIA
35 1 HEADACHE
32 1 CANCER OF ANY KIND
45 2 VOMITTING
65 2 HEADACHE
22 2 ASTHMA
0 1 MENINGITIS
1 1 DIARRHOEA WITHOUT BLOOD
46 1 MURDERED
0 2 OTHER (SPECIFY) was not sucking
15 2 OTHER (SPECIFY) lung infection
5 1 OTHER (SPECIFY) swelling of the belly and vomiting blood
32 2 CONSTIPATION/STOMACH UPSET
35 1 ACCIDENT
42 2 COUGH/COLD/CHEST INFECTION
40 2 PNEUMONIA/CHEST PAIN
N/A
38 2 FEVER/MALARIA
4 1 LACK OF BLOOD/ANEAMIA
49 1 COUGH/COLD/CHEST INFECTION
40 2 LIVER INFECTION/SIDE PAIN
75 1 DIABETES/SUGAR DISEASE
28 1 MURDERED
30 2 OTHER (SPECIFY) HIV Aids
66 2 DIABETES/SUGAR DISEASE
60 1 DIABETES/SUGAR DISEASE
30 1 FEVER/MALARIA
35 1 FEVER/MALARIA
62 2 STROKE
78 1 STROKE
18 2 DIARRHOEA WITH BLOOD
1 1 PNEUMONIA/CHEST PAIN
52 1 TUBERCULOSIS
21 1 ACCIDENT
37 2 HYPERTENSION
44 1 COUGH/COLD/CHEST INFECTION
20 2 DIARRHOEA AND VOMITTING
N/A
8 1 DON'T KNOW
4 1 PNEUMONIA/CHEST PAIN
35 1 VOMITTING
9 1 COUGH/COLD/CHEST INFECTION
1 2 DON'T KNOW
46 1 FEVER/MALARIA
15 2 CEREBRAL MALARIA
1 2 FEVER/MALARIA
78 1 TUBERCULOSIS
87 2 STROKE
24 1 OTHER (SPECIFY) Stomach pains
86 2 HEADACHE
1 2 CEREBRAL MALARIA
33 1 SUICIDE
90 2 SKIN RASH/SKIN INFECTION
60 2 MURDERED
25 2 FEVER/MALARIA
30 1 ACCIDENT
40 2 ACCIDENT
1 2 FEVER/MALARIA
40 2 BRONCHITIS
20 2 LIVER INFECTION/SIDE PAIN
55 1 PNEUMONIA/CHEST PAIN
15 1 DON'T KNOW
67 1 ACCIDENT
26 2 DON'T KNOW
42 2 DON'T KNOW
40 1 FEVER/MALARIA
83 1 STROKE
74 1 STROKE
38 1 OTHER (SPECIFY) HIV/AIDS
12 1 FEVER/MALARIA
2 1 OTHER (SPECIFY) drowned
19 1 OTHER (SPECIFY) vomiting blood
1 2 FEVER/MALARIA
28 1 PNEUMONIA/CHEST PAIN
1 2 BRONCHITIS
14 1 PILES/HAEMOROIDS
45 2 CANCER OF ANY KIND
29 2 HYPERTENSION
25 2 LACK OF BLOOD/ANEAMIA
3 1 LACK OF BLOOD/ANEAMIA
0 1 OTHER (SPECIFY) Hydrocephalas
10 2 HYPERTENSION
1 1 CEREBRAL MALARIA
4 2 DON'T KNOW
36 1 COUGH/COLD/CHEST INFECTION
28 1 DIARRHOEA WITH BLOOD
62 1 TUBERCULOSIS
29 1 ACCIDENT
N/A
6 1 ACCIDENT
23 2 TUBERCULOSIS
N/A
31 1 ACCIDENT
4 1 MEASLES
44 1 DON'T KNOW
42 2 HYPERTENSION
25 1 DON'T KNOW
22 2 DON'T KNOW
N/A
N/A
N/A
N/A
N/A
22 1 OTHER (SPECIFY) brain infection
0 2 FEVER/MALARIA
48 1 HYPERTENSION
36 1 COUGH/COLD/CHEST INFECTION
45 1 TUBERCULOSIS
20 2 HYPERTENSION
2 1 DIARRHOEA WITHOUT BLOOD
85 2 STROKE
62 1 STROKE
30 1 FEVER/MALARIA
60 1 OTHER (SPECIFY) road accident
20 1 LACK OF BLOOD/ANEAMIA
32 2 TUBERCULOSIS
58 1 COUGH/COLD/CHEST INFECTION
38 2 COUGH/COLD/CHEST INFECTION
0 2 FEVER/MALARIA
35 1 TUBERCULOSIS
30 1 HEADACHE
34 2 CANCER OF ANY KIND
1 1 OTHER (SPECIFY) swallening of the legs and wounds in the mouth
78 2 OTHER (SPECIFY) run mad
40 2 OTHER (SPECIFY) supasticius traditional sickness . had a pain in th leg
45 2 OTHER (SPECIFY) swelling of the body
9 2 DON'T KNOW
39 2 OTHER (SPECIFY) was opperated in the stomarch and got worse
8 1 CONSTIPATION/STOMACH UPSET
12 2 FEVER/MALARIA
87 1 ABDOMINAL PAINS
60 2 TUBERCULOSIS
N/A
18 2 COUGH/COLD/CHEST INFECTION
18 1 TUBERCULOSIS
3 2 LACK OF BLOOD/ANEAMIA
22 1 FEVER/MALARIA
30 2 FEVER/MALARIA
46 1 FEVER/MALARIA
1 2 FEVER/MALARIA
5 2 DON'T KNOW
21 2 OTHER (SPECIFY) Yellow Fever
9 1 ASTHMA
35 2 CANCER OF ANY KIND
6 2 OTHER (SPECIFY) R.T.A
76 1 ABDOMINAL PAINS
25 2 FEVER/MALARIA
69 1 OTHER (SPECIFY) Heart Failure
2 2 FEVER/MALARIA
3 2 DIARRHOEA AND VOMITTING
0 1 DON'T KNOW
0 2 FEVER/MALARIA
12 1 DIARRHOEA WITHOUT BLOOD
12 1 DIARRHOEA WITHOUT BLOOD
5 1 ACCIDENT
0 1 FEVER/MALARIA
0 2 OTHER (SPECIFY) dead while sleeping
60 1 STROKE
2 2 MENINGITIS
7 2 DON'T KNOW
52 2 DIARRHOEA WITH BLOOD
53 1 OTHER (SPECIFY) whith craft
70 1 CANCER OF ANY KIND
30 1 OTHER (SPECIFY) HIV/AIDS
N/A
76 2 STROKE
N/A
24 1 ACCIDENT
49 1 BOILS
90 1 COUGH/COLD/CHEST INFECTION
52 1 PNEUMONIA/CHEST PAIN
60 2 OTHER (SPECIFY) heart problems
50 2 DON'T KNOW
7 2 OTHER (SPECIFY) sickle cell anaemia
83 1 HYPERTENSION
48 2 FEVER/MALARIA
5 1 DIARRHOEA AND VOMITTING
67 2 HYPERTENSION
90 2 HYPERTENSION
N/A
0 1 DON'T KNOW
0 2 DON'T KNOW
65 2 DON'T KNOW
85 1 LACK OF BLOOD/ANEAMIA
65 2 CANCER OF ANY KIND
27 1 LACK OF BLOOD/ANEAMIA
46 1 CEREBRAL MALARIA
28 1 COUGH/COLD/CHEST INFECTION
28 1 OTHER (SPECIFY) HIV/AIDS
2 2 COUGH/COLD/CHEST INFECTION
1 1 FEVER/MALARIA
0 2 VOMITTING
24 2 ASTHMA
75 2 FEVER/MALARIA
2 1 ASTHMA
76 1 FEVER/MALARIA
10 1 COUGH/COLD/CHEST INFECTION
50 1 FEVER/MALARIA
1 2 FEVER/MALARIA
53 1 PARALYSIS OF ANY KIND
20 2 FEVER/MALARIA
32 1 FEVER/MALARIA
71 2 DON'T KNOW
2 1 FEVER/MALARIA
14 2 FEVER/MALARIA
3 1 FEVER/MALARIA
1 2 FEVER/MALARIA
42 2 CANCER OF ANY KIND
5 2 COUGH/COLD/CHEST INFECTION
N/A
0 1 DON'T KNOW
28 1 DIARRHOEA WITH BLOOD
25 2 HEADACHE
3 2 OTHER (SPECIFY) mysterious death
20 2 FEVER/MALARIA
2 2 FEVER/MALARIA
32 2 COUGH/COLD/CHEST INFECTION
1 2 FEVER/MALARIA
35 1 OTHER (SPECIFY) HIV/AIDS
72 2 STROKE
73 1 DON'T KNOW
82 1 DIABETES/SUGAR DISEASE
79 2 DIARRHOEA WITH BLOOD
55 1 ABDOMINAL PAINS
41 1 HEADACHE
7 1 OTHER (SPECIFY) Lack of blood
5 1 FEVER/MALARIA
8 1 DIARRHOEA AND VOMITTING
8 2 JAUNDICE/YELLOWNESS
20 2 COUGH/COLD/CHEST INFECTION
39 2 OTHER (SPECIFY) HIV /AIDS
36 1 OTHER (SPECIFY) Drowned in the river
79 1 DON'T KNOW
33 1 OTHER (SPECIFY) was wiched
90 2 DON'T KNOW
0 1 FEVER/MALARIA
89 2 DIABETES/SUGAR DISEASE
1 2 FEVER/MALARIA
26 2 DON'T KNOW
26 2 DIARRHOEA AND VOMITTING
65 1 OTHER (SPECIFY) eyes
78 1 CONSTIPATION/STOMACH UPSET
6 1 COUGH/COLD/CHEST INFECTION
1 2 FEVER/MALARIA
68 1 STROKE
40 1 TUBERCULOSIS
44 1 ACCIDENT
55 1 TUBERCULOSIS
2 2 FEVER/MALARIA
19 2 FEVER/MALARIA
41 2 FEVER/MALARIA
31 1 ACCIDENT
58 1 TUBERCULOSIS
19 1 ACCIDENT
40 2 HYPERTENSION
5 1 OTHER (SPECIFY) He dropped in the water well
21 1 MENINGITIS
66 2 DON'T KNOW
N/A
70 1 TUBERCULOSIS
1 1 DIARRHOEA AND VOMITTING
9 2 PNEUMONIA/CHEST PAIN
34 1 LACK OF BLOOD/ANEAMIA
3 1 BRONCHITIS
43 2 CEREBRAL MALARIA
8 2 COUGH/COLD/CHEST INFECTION
4 2 PNEUMONIA/CHEST PAIN
81 2 FEVER/MALARIA
16 2 OTHER (SPECIFY) post natal
77 2 FEVER/MALARIA
0 1 FEVER/MALARIA
N/A
85 2 STROKE
25 2 FEVER/MALARIA
46 2 TUBERCULOSIS
25 2 TUBERCULOSIS
30 1 COUGH/COLD/CHEST INFECTION
60 1 TUBERCULOSIS
26 1 MURDERED
3 1 DON'T KNOW
4 1 FEVER/MALARIA
0 2 OTHER (SPECIFY) still birth
75 1 FEVER/MALARIA
65 2 FEVER/MALARIA
N/A
46 2 CONSTIPATION/STOMACH UPSET
90 2 OTHER (SPECIFY) mubili wenze kupya
72 2 STROKE
86 1 DON'T KNOW
25 1 MURDERED
50 1 STROKE
5 2 ABDOMINAL PAINS
2 2 FEVER/MALARIA
34 2 TUBERCULOSIS
10 1 OTHER (SPECIFY) lightening
10 1 FEVER/MALARIA
56 1 ABDOMINAL PAINS
35 1 OTHER (SPECIFY) beer drinking(kachasu)
37 2 OTHER (SPECIFY) Post natal
8 1 FEVER/MALARIA
28 1 OTHER (SPECIFY) HIV/AIDS
0 2 DON'T KNOW
7 2 DON'T KNOW
8 2 DON'T KNOW
1 2 DON'T KNOW
1 2 DON'T KNOW
45 1 DIABETES/SUGAR DISEASE
86 2 FEVER/MALARIA
4 2 PNEUMONIA/CHEST PAIN
3 2 ABDOMINAL PAINS
39 1 OTHER (SPECIFY) Bewitched
32 1 BRONCHITIS
28 1 FEVER/MALARIA
0 2 DON'T KNOW
0 1 DON'T KNOW
3 2 DIARRHOEA WITHOUT BLOOD
0 1 FEVER/MALARIA
0 2 DON'T KNOW
15 2 CEREBRAL MALARIA
75 1 CANCER OF ANY KIND
20 2 SUICIDE
5 1 SKIN RASH/SKIN INFECTION
2 2 CEREBRAL MALARIA
N/A
N/A
27 2 SUICIDE
23 2 CEREBRAL MALARIA
72 1 LACK OF BLOOD/ANEAMIA
21 2 JAUNDICE/YELLOWNESS
9 1 FEVER/MALARIA
50 2 DIABETES/SUGAR DISEASE
3 2 FEVER/MALARIA
1 1 CEREBRAL MALARIA
13 2 COUGH/COLD/CHEST INFECTION
28 1 LACK OF BLOOD/ANEAMIA
N/A
2 2 FEVER/MALARIA
36 1 COUGH/COLD/CHEST INFECTION
30 2 CANCER OF ANY KIND
N/A
57 2 DIARRHOEA WITHOUT BLOOD
9 1 FEVER/MALARIA
36 2 ACCIDENT
7 2 ACCIDENT
5 2 ACCIDENT
90 1 COUGH/COLD/CHEST INFECTION
2 1 FEVER/MALARIA
61 1 ACCIDENT
45 1 PNEUMONIA/CHEST PAIN
30 2 FEVER/MALARIA
74 1 LACK OF BLOOD/ANEAMIA
25 1 TUBERCULOSIS
7 1 OTHER (SPECIFY) food poisoning
72 2 STROKE
2 1 ABDOMINAL PAINS
75 2 BRONCHITIS
41 2 CANCER OF ANY KIND
35 1 TUBERCULOSIS
21 2 FEVER/MALARIA
N/A
50 1 FEVER/MALARIA
4 2 LACK OF BLOOD/ANEAMIA
15 1 TUBERCULOSIS
24 1 OTHER (SPECIFY) epilepsy
19 2 SUICIDE
12 1 FEVER/MALARIA
5 1 HEADACHE
50 1 FEVER/MALARIA
40 2 COUGH/COLD/CHEST INFECTION
32 1 COUGH/COLD/CHEST INFECTION
0 1 DON'T KNOW
34 1 TUBERCULOSIS
10 2 FEVER/MALARIA
84 2 OTHER (SPECIFY) low blood pressure
58 1 PARALYSIS OF ANY KIND
88 1 OTHER (SPECIFY) old age
82 1 ASTHMA
45 1 PNEUMONIA/CHEST PAIN
76 2 PARALYSIS OF ANY KIND
65 2 COUGH/COLD/CHEST INFECTION
45 2 OTHER (SPECIFY) heart problems
35 2 DIABETES/SUGAR DISEASE
25 2 DIARRHOEA WITH BLOOD
30 2 OTHER (SPECIFY) HIV/ADS
6 1 SHINGLES/HERPES ZOSTER
16 2 DON'T KNOW
54 2 CANCER OF ANY KIND
5 2 CEREBRAL MALARIA
76 2 CONSTIPATION/STOMACH UPSET
62 1 DIABETES/SUGAR DISEASE
41 1 ABDOMINAL PAINS
2 1 DIARRHOEA WITHOUT BLOOD
14 2 PARALYSIS OF ANY KIND
5 2 CONSTIPATION/STOMACH UPSET
40 1 HYPERTENSION
6 1 FEVER/MALARIA
3 2 DIARRHOEA AND VOMITTING
1 1 COUGH/COLD/CHEST INFECTION
2 1 DIARRHOEA WITHOUT BLOOD
3 2 FEVER/MALARIA
5 1 FEVER/MALARIA
8 2 DIARRHOEA WITH BLOOD
56 1 COUGH/COLD/CHEST INFECTION
2 2 DON'T KNOW
90 1 FEVER/MALARIA
43 1 OTHER (SPECIFY) Bilhazia
45 2 FEVER/MALARIA
13 1 FEVER/MALARIA
83 2 PNEUMONIA/CHEST PAIN
7 1 FEVER/MALARIA
40 2 ACCIDENT
45 1 TUBERCULOSIS
45 2 DIARRHOEA AND VOMITTING
2 1 FEVER/MALARIA
3 1 PARALYSIS OF ANY KIND
4 1 SKIN RASH/SKIN INFECTION
80 2 FEVER/MALARIA
7 2 FEVER/MALARIA
34 2 DON'T KNOW
1 2 FEVER/MALARIA
12 2 OTHER (SPECIFY) Witchcraft
31 1 ACCIDENT
48 1 DON'T KNOW
49 1 HEADACHE
60 1 TUBERCULOSIS
72 1 FEVER/MALARIA
69 2 DON'T KNOW
17 2 DON'T KNOW
55 2 DON'T KNOW
37 1 FEVER/MALARIA
27 2 STROKE
7 1 FEVER/MALARIA
55 2 ABDOMINAL PAINS
52 2 DON'T KNOW
N/A
42 1 OTHER (SPECIFY) poisoning
20 1 ABDOMINAL PAINS
28 1 OTHER (SPECIFY) Was beaten to death by other people
30 1 DON'T KNOW
60 1 CONSTIPATION/STOMACH UPSET
60 2 ABDOMINAL PAINS
55 2 DIARRHOEA AND VOMITTING
36 2 PNEUMONIA/CHEST PAIN
2 2 COUGH/COLD/CHEST INFECTION
15 1 OTHER (SPECIFY) Drowned in lake Bangweulu
0 1 FEVER/MALARIA
28 2 ASTHMA
3 2 FEVER/MALARIA
12 1 CANCER OF ANY KIND
72 2 ASTHMA
32 1 DIARRHOEA WITH BLOOD
42 1 LACK OF BLOOD/ANEAMIA
6 2 ACCIDENT
3 2 VOMITTING
30 1 TUBERCULOSIS
2 1 FEVER/MALARIA
20 2 ABDOMINAL PAINS
2 1 FEVER/MALARIA
72 2 FEVER/MALARIA
90 2 FEVER/MALARIA
24 1 OTHER (SPECIFY) drinking beer
35 1 FEVER/MALARIA
60 1 PNEUMONIA/CHEST PAIN
3 2 DON'T KNOW
50 1 CEREBRAL MALARIA
33 2 SUICIDE
7 2 CEREBRAL MALARIA
1 1 DON'T KNOW
7 1 CONSTIPATION/STOMACH UPSET
22 1 DON'T KNOW
6 1 SHINGLES/HERPES ZOSTER
39 1 MURDERED
60 2 FEVER/MALARIA
12 2 FEVER/MALARIA
40 1 DON'T KNOW
72 1 DON'T KNOW
25 1 TUBERCULOSIS
19 1 ACCIDENT
76 2 OTHER (SPECIFY) too old
45 2 ASTHMA
3 2 DIARRHOEA AND VOMITTING
52 1 LIVER INFECTION/SIDE PAIN
4 1 FEVER/MALARIA
36 1 COUGH/COLD/CHEST INFECTION
15 1 ABDOMINAL PAINS
N/A
8 1 FEVER/MALARIA
5 1 FEVER/MALARIA
15 1 HEADACHE
20 2 DIARRHOEA WITHOUT BLOOD
25 2 FEVER/MALARIA
80 2 OTHER (SPECIFY) breast cancer
30 1 TUBERCULOSIS
4 1 VOMITTING
15 2 LACK OF BLOOD/ANEAMIA
21 2 CEREBRAL MALARIA
13 1 ACCIDENT
90 1 DIABETES/SUGAR DISEASE
13 1 PARALYSIS OF ANY KIND
12 2 FEVER/MALARIA
1 1 FEVER/MALARIA
33 1 OTHER (SPECIFY) poisoned
0 1 COUGH/COLD/CHEST INFECTION
82 1 STROKE
N/A
N/A
N/A
16 1 FEVER/MALARIA
14 1 FEVER/MALARIA
46 1 ACCIDENT
6 2 BRONCHITIS
N/A
50 1 DON'T KNOW
42 2 MURDERED
40 2 OTHER (SPECIFY) Witchcraft
23 1 FEVER/MALARIA
2 1 COUGH/COLD/CHEST INFECTION
40 1 CEREBRAL MALARIA
44 1 LIVER INFECTION/SIDE PAIN
10 1 FEVER/MALARIA
15 1 FEVER/MALARIA
70 2 DON'T KNOW
71 2 TUBERCULOSIS
12 1 LACK OF BLOOD/ANEAMIA
17 1 LACK OF BLOOD/ANEAMIA
15 2 HEADACHE
2 1 LACK OF BLOOD/ANEAMIA
68 2 OTHER (SPECIFY) swelling of the stomach. ...
55 2 ACCIDENT
12 1 STROKE
70 1 FEVER/MALARIA
30 2 HEADACHE
30 2 ABDOMINAL PAINS
66 1 OTHER (SPECIFY) sharp pain in the ribs
42 1 CEREBRAL MALARIA
48 1 FEVER/MALARIA
15 1 COUGH/COLD/CHEST INFECTION
8 1 MURDERED
1 2 BRONCHITIS
79 1 DIABETES/SUGAR DISEASE
55 1 CANCER OF ANY KIND
45 1 OTHER (SPECIFY) witchcraf
40 2 OTHER (SPECIFY) prolong priod
30 1 FEVER/MALARIA
52 2 FEVER/MALARIA
28 2 DON'T KNOW
2 1 CONSTIPATION/STOMACH UPSET
24 1 DON'T KNOW
20 1 FEVER/MALARIA
40 2 DIARRHOEA WITHOUT BLOOD
62 1 CONSTIPATION/STOMACH UPSET
82 2 HYPERTENSION
48 1 MENINGITIS
26 2 FEVER/MALARIA
36 2 ABDOMINAL PAINS
44 2 DIABETES/SUGAR DISEASE
36 1 ACCIDENT
34 1 ACCIDENT
36 1 ACCIDENT
0 1 FEVER/MALARIA
14 2 ABDOMINAL PAINS
62 2 DIARRHOEA WITH BLOOD
52 2 COUGH/COLD/CHEST INFECTION
33 1 MENINGITIS
8 1 FEVER/MALARIA
N/A
N/A
N/A
N/A
50 1 CONSTIPATION/STOMACH UPSET
24 2 FEVER/MALARIA
4 1 FEVER/MALARIA
47 1 PNEUMONIA/CHEST PAIN
0 2 FEVER/MALARIA
35 1 MURDERED
25 1 FEVER/MALARIA
24 2 DON'T KNOW
2 1 COUGH/COLD/CHEST INFECTION
38 1 CEREBRAL MALARIA
N/A
46 1 TUBERCULOSIS
43 2 OTHER (SPECIFY) bitten by a dog
39 2 TUBERCULOSIS
90 1 DIARRHOEA AND VOMITTING
33 2 COUGH/COLD/CHEST INFECTION
55 1 OTHER (SPECIFY) mendo
24 2 DON'T KNOW
7 2 DIARRHOEA WITHOUT BLOOD
52 1 FEVER/MALARIA
22 2 COUGH/COLD/CHEST INFECTION
22 2 DON'T KNOW
1 2 DIARRHOEA WITHOUT BLOOD
0 1 OTHER (SPECIFY) the baby died 3 hours after being born
38 2 HEADACHE
2 1 CEREBRAL MALARIA
49 1 FEVER/MALARIA
55 2 STROKE
36 2 ASTHMA
4 2 MENINGITIS
1 2 OTHER (SPECIFY) was not ill it was just a sudden death
4 2 BRONCHITIS
49 1 TUBERCULOSIS
40 2 TUBERCULOSIS
35 1 TUBERCULOSIS
52 2 MURDERED
36 1 TUBERCULOSIS
3 1 ACCIDENT
38 1 ACCIDENT
25 1 TUBERCULOSIS
60 2 COUGH/COLD/CHEST INFECTION
17 1 FEVER/MALARIA
4 1 COUGH/COLD/CHEST INFECTION
3 2 COUGH/COLD/CHEST INFECTION
89 2 OTHER (SPECIFY) heart failure
55 2 MENINGITIS
0 1 FEVER/MALARIA
38 1 CEREBRAL MALARIA
48 1 ACCIDENT
25 2 FEVER/MALARIA
3 2 PNEUMONIA/CHEST PAIN
4 1 TUBERCULOSIS
3 2 PNEUMONIA/CHEST PAIN
3 2 CEREBRAL MALARIA
5 1 TUBERCULOSIS
3 1 ACCIDENT
N/A
78 2 VOMITTING
52 2 FEVER/MALARIA
39 2 CANCER OF ANY KIND
59 2 DIABETES/SUGAR DISEASE
80 2 DIABETES/SUGAR DISEASE
19 2 DIARRHOEA WITHOUT BLOOD
52 1 TUBERCULOSIS
45 1 CEREBRAL MALARIA
N/A
N/A
N/A
65 1 ASTHMA
40 1 CEREBRAL MALARIA
3 1 CEREBRAL MALARIA
74 2 CANCER OF ANY KIND
2 2 DIARRHOEA AND VOMITTING
53 1 HEADACHE
78 2 DIABETES/SUGAR DISEASE
48 2 FEVER/MALARIA
62 1 STROKE
34 2 OTHER (SPECIFY) HIV/AIDS
63 1 STROKE
38 2 MURDERED
46 2 LIVER INFECTION/SIDE PAIN
N/A
5 2 ASTHMA
28 1 TUBERCULOSIS
1 2 FEVER/MALARIA
35 1 ACCIDENT
42 2 COUGH/COLD/CHEST INFECTION
3 2 FEVER/MALARIA
7 1 MENINGITIS
32 2 TUBERCULOSIS
3 1 FEVER/MALARIA
87 1 COUGH/COLD/CHEST INFECTION
85 1 FEVER/MALARIA
30 1 CEREBRAL MALARIA
58 1 DIARRHOEA AND VOMITTING
N/A
30 2 LACK OF BLOOD/ANEAMIA
16 1 LIVER INFECTION/SIDE PAIN
35 1 DIARRHOEA WITH BLOOD
23 2 TUBERCULOSIS
2 1 CEREBRAL MALARIA
60 1 COUGH/COLD/CHEST INFECTION
N/A
78 2 ASTHMA
45 1 FEVER/MALARIA
53 1 DON'T KNOW
N/A
N/A
90 1 FEVER/MALARIA
69 2 DIARRHOEA WITH BLOOD
84 2 OTHER (SPECIFY) old age
42 1 OTHER (SPECIFY) drowned
0 1 FEVER/MALARIA
1 2 DON'T KNOW
18 2 FEVER/MALARIA
74 2 PNEUMONIA/CHEST PAIN
26 1 SUICIDE
8 1 DIARRHOEA AND VOMITTING
8 1 DIARRHOEA AND VOMITTING
5 1 COUGH/COLD/CHEST INFECTION
76 1 HEADACHE
2 2 CEREBRAL MALARIA
38 1 OTHER (SPECIFY) swelling of the intire body
2 2 ABDOMINAL PAINS
50 1 COUGH/COLD/CHEST INFECTION
35 2 PARALYSIS OF ANY KIND
68 2 STROKE
42 1 TUBERCULOSIS
6 2 BRONCHITIS
6 2 FEVER/MALARIA
49 1 FEVER/MALARIA
90 1 COUGH/COLD/CHEST INFECTION
15 2 FEVER/MALARIA
17 2 OTHER (SPECIFY) body swelling.
25 2 DIARRHOEA WITHOUT BLOOD
87 2 TUBERCULOSIS
38 1 ASTHMA
9 2 COUGH/COLD/CHEST INFECTION
48 1 ACCIDENT
42 2 ACCIDENT
29 2 DIARRHOEA AND VOMITTING
56 2 OTHER (SPECIFY) soles at heart
29 1 DON'T KNOW
36 1 OTHER (SPECIFY) striked by a lightening
39 1 LACK OF BLOOD/ANEAMIA
84 1 FEVER/MALARIA
45 2 FEVER/MALARIA
1 1 FEVER/MALARIA
90 1 FEVER/MALARIA
27 1 DON'T KNOW
12 1 ASTHMA
46 1 DON'T KNOW
50 1 COUGH/COLD/CHEST INFECTION
30 1 ACCIDENT
18 2 HEADACHE
89 2 ASTHMA
78 2 FEVER/MALARIA
19 1 PNEUMONIA/CHEST PAIN
0 1 OTHER (SPECIFY) Just found the child is dead in the morning
6 1 FEVER/MALARIA
15 1 LACK OF BLOOD/ANEAMIA
30 1 CANCER OF ANY KIND
0 2 OTHER (SPECIFY) premature baby
87 2 HYPERTENSION
45 1 TUBERCULOSIS
68 1 ASTHMA
39 2 FEVER/MALARIA
9 2 COUGH/COLD/CHEST INFECTION
35 2 FEVER/MALARIA
4 1 ACCIDENT
63 1 LIVER INFECTION/SIDE PAIN
56 1 MURDERED
65 1 TUBERCULOSIS
26 1 ACCIDENT
80 1 COUGH/COLD/CHEST INFECTION
21 2 CEREBRAL MALARIA
75 1 FEVER/MALARIA
30 2 CONSTIPATION/STOMACH UPSET
60 1 CONSTIPATION/STOMACH UPSET
38 2 FEVER/MALARIA
N/A
79 2 OTHER (SPECIFY) Old age
25 1 DIARRHOEA AND VOMITTING
54 1 CANCER OF ANY KIND
N/A
54 1 FEVER/MALARIA
78 1 PARALYSIS OF ANY KIND
33 2 ABDOMINAL PAINS
3 2 ABDOMINAL PAINS
2 2 DIARRHOEA AND VOMITTING
18 1 PNEUMONIA/CHEST PAIN
3 1 BRONCHITIS
8 2 OTHER (SPECIFY) siclecel
56 1 FEVER/MALARIA
8 1 OTHER (SPECIFY) pass in the rib cage
24 1 SUICIDE
N/A
2 2 BOILS
29 2 SUICIDE
25 2 COUGH/COLD/CHEST INFECTION
34 2 CEREBRAL MALARIA
58 2 DIABETES/SUGAR DISEASE
35 1 HEADACHE
36 1 FEVER/MALARIA
1 1 ACCIDENT
7 2 LIVER INFECTION/SIDE PAIN
48 2 VOMITTING
N/A
3 2 CEREBRAL MALARIA
36 1 FEVER/MALARIA
20 2 LACK OF BLOOD/ANEAMIA
28 2 OTHER (SPECIFY) maternal death (died while giving birth.)
28 2 DIARRHOEA WITH BLOOD
25 1 FEVER/MALARIA
29 1 FEVER/MALARIA
18 2 ACCIDENT
11 1 DIARRHOEA WITH BLOOD
77 1 FEVER/MALARIA
60 2 CANCER OF ANY KIND
84 1 OTHER (SPECIFY) nothing
6 1 FEVER/MALARIA
N/A
85 1 COUGH/COLD/CHEST INFECTION
10 1 MURDERED
3 2 FEVER/MALARIA
19 2 TUBERCULOSIS
66 2 HYPERTENSION
2 1 FEVER/MALARIA
1 1 FEVER/MALARIA
72 2 TUBERCULOSIS
48 1 ASTHMA
35 1 FEVER/MALARIA
17 2 ABDOMINAL PAINS
72 2 HYPERTENSION
30 2 OTHER (SPECIFY) swollen feet
1 2 DON'T KNOW
80 1 STROKE
33 2 TUBERCULOSIS
42 2 TUBERCULOSIS
1 2 FEVER/MALARIA
1 1 FEVER/MALARIA
1 1 LACK OF BLOOD/ANEAMIA
52 1 FEVER/MALARIA
54 1 FEVER/MALARIA
20 2 ABDOMINAL PAINS
70 2 LACK OF BLOOD/ANEAMIA
1 1 HEADACHE
30 1 FEVER/MALARIA
25 1 ACCIDENT
39 2 MURDERED
3 2 DON'T KNOW
27 1 COUGH/COLD/CHEST INFECTION
39 2 FEVER/MALARIA
45 2 FEVER/MALARIA
35 1 COUGH/COLD/CHEST INFECTION
40 1 LIVER INFECTION/SIDE PAIN
62 1 DIABETES/SUGAR DISEASE
59 2 VOMITTING
72 2 OTHER (SPECIFY) coronary thrombosis
52 1 PARALYSIS OF ANY KIND
1 2 DON'T KNOW
8 2 FEVER/MALARIA
49 2 OTHER (SPECIFY) Swelling of Body
63 2 ABDOMINAL PAINS
54 1 DIARRHOEA WITH BLOOD
2 2 DON'T KNOW
90 2 PARALYSIS OF ANY KIND
40 1 TUBERCULOSIS
60 2 OTHER (SPECIFY) Swollen of the head and neck
70 1 COUGH/COLD/CHEST INFECTION
24 2 LACK OF BLOOD/ANEAMIA
24 2 CANCER OF ANY KIND
81 2 PARALYSIS OF ANY KIND
52 1 CANCER OF ANY KIND
86 1 VOMITTING
68 1 MENINGITIS
55 2 ABDOMINAL PAINS
6 1 OTHER (SPECIFY) just died whilst he was sleeping
35 2 FEVER/MALARIA
N/A
45 2 CANCER OF ANY KIND
53 1 PNEUMONIA/CHEST PAIN
38 2 HYPERTENSION
37 2 TUBERCULOSIS
60 2 STROKE
83 1 PILES/HAEMOROIDS
2 2 DON'T KNOW
1 1 DON'T KNOW
22 1 BOILS
50 1 FEVER/MALARIA
34 1 TUBERCULOSIS
70 2 PNEUMONIA/CHEST PAIN
75 2 STROKE
90 1 HEADACHE
25 1 PILES/HAEMOROIDS
72 1 SUICIDE
36 1 FEVER/MALARIA
19 2 COUGH/COLD/CHEST INFECTION
0 1 DON'T KNOW
28 1 ACCIDENT
40 2 TUBERCULOSIS
52 2 FEVER/MALARIA
3 2 DIARRHOEA WITHOUT BLOOD
71 1 ASTHMA
1 2 FEVER/MALARIA
50 2 TUBERCULOSIS
75 1 FEVER/MALARIA
39 1 FEVER/MALARIA
85 1 FEVER/MALARIA
74 1 CANCER OF ANY KIND
47 1 COUGH/COLD/CHEST INFECTION
29 1 FEVER/MALARIA
50 2 CONSTIPATION/STOMACH UPSET
62 2 STROKE
40 2 STROKE
34 1 COUGH/COLD/CHEST INFECTION
32 2 FEVER/MALARIA
41 1 LACK OF BLOOD/ANEAMIA
2 2 FEVER/MALARIA
20 1 COUGH/COLD/CHEST INFECTION
9 2 COUGH/COLD/CHEST INFECTION
6 2 COUGH/COLD/CHEST INFECTION
50 2 FEVER/MALARIA
71 2 HYPERTENSION
90 2 COUGH/COLD/CHEST INFECTION
42 2 LACK OF BLOOD/ANEAMIA
3 2 MENINGITIS
39 1 FEVER/MALARIA
76 2 ASTHMA
0 1 BRONCHITIS
0 1 DON'T KNOW
4 2 FEVER/MALARIA
6 2 FEVER/MALARIA
N/A
45 1 MENINGITIS
90 1 FEVER/MALARIA
47 2 TUBERCULOSIS
26 2 ASTHMA
49 2 CEREBRAL MALARIA
N/A
N/A
23 2 LACK OF BLOOD/ANEAMIA
4 1 OTHER (SPECIFY) burnt to death
90 1 LIVER INFECTION/SIDE PAIN
75 1 STROKE
27 1 CANCER OF ANY KIND
1 2 COUGH/COLD/CHEST INFECTION
72 1 TUBERCULOSIS
35 1 FEVER/MALARIA
38 2 HEADACHE
29 1 ACCIDENT
60 2 TUBERCULOSIS
52 2 OTHER (SPECIFY) Bewitched
70 2 OTHER (SPECIFY) Bewitched
40 2 CEREBRAL MALARIA
18 2 PARALYSIS OF ANY KIND
24 1 PNEUMONIA/CHEST PAIN
4 2 CEREBRAL MALARIA
1 1 VOMITTING
72 1 STROKE
22 2 CEREBRAL MALARIA
7 1 DIARRHOEA AND VOMITTING
17 1 FEVER/MALARIA
19 2 FEVER/MALARIA
15 2 PNEUMONIA/CHEST PAIN
90 2 OTHER (SPECIFY) Heart problem
24 2 ASTHMA
22 2 HEADACHE
39 1 TUBERCULOSIS
23 1 FEVER/MALARIA
13 2 SKIN RASH/SKIN INFECTION
10 2 SKIN RASH/SKIN INFECTION
50 2 COUGH/COLD/CHEST INFECTION
73 1 CANCER OF ANY KIND
38 1 OTHER (SPECIFY) headache
80 1 ABDOMINAL PAINS
10 1 ABDOMINAL PAINS
N/A
62 1 OTHER (SPECIFY) S. ANEMIA
62 1 COUGH/COLD/CHEST INFECTION
N/A
90 2 ASTHMA
37 1 ACCIDENT
27 1 SUICIDE
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-tip/0.7.0/d3-tip.min.js">
</script>
<link rel="stylesheet"
href="//rawgithub.com/Caged/d3-tip/master/examples/example-styles.css">
<style>
.tick line {
display: none;
}
rect.bar {
stroke: white;
}
.male {
fill: #af8dc3;
}
.female {
fill: #7fbf7b;
}
</style>
</head>
<body>
<script>
d3.csv("deaths.csv")
.row(row)
.get(visualize)
;
function row(d) {
var gender = { "1": "male", "2": "female" };
d.SEX = gender[d.SEX];
return d;
} // row()
function visualize(error, data) {
if(error) throw error;
var nested = d3.nest()
// First group by CAUSE
.key(function(d) { return d.CAUSE; })
.rollup(function(leaves) {
var nested = d3.nest()
.key(function(d) { return d.SEX; })
.rollup(function(leafs) { return leafs.length; })
.object(leaves)
;
nested["all"] = d3.sum(d3.values(nested))
return nested;
})
.map(data)
.entries()
.sort(function (a, b) {
return d3.descending(a.value.all, b.value.all);
})
, causes = d3.stack()
.keys(["female", "male"])
.value(function(d, key) { return d.value[key] || 0; })
(nested)
;
var margin = { top: 20, bottom: 200, left: 40, right: 20 }
, height = 500 - margin.top - margin.bottom
, width = 960 - margin.left - margin.right
, x = d3.scaleBand()
.domain(nested.map(function(d) { return d.key; }))
.rangeRound([0, width])
, xAxis = d3.axisBottom().scale(x)
, y = d3.scaleLinear()
.domain([0, d3.max(nested, function(d) { return d.value.all; })])
.range([height, 0])
, yAxis = d3.axisLeft().scale(y)
;
var tip = d3.tip().attr('class', 'd3-tip').direction('s')
, svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.call(tip)
.append("g")
.attr("transform", "translate("+margin.left+","+margin.top+")")
, layers = svg
.append("g")
.attr("class", "bars")
.selectAll("g")
.data(causes)
;
layers
.enter().append("g")
.attr("class", function(d) { return d.key; })
.each(function(layer) {
var rect = d3.select(this).selectAll("rect")
.data(function(d) { return d; })
;
rect
.enter().append("rect")
.attr("class", "bar")
.attr("x", function(d) { return x(d.data.key); })
.attr("width", x.bandwidth())
.attr("y", function(d) { return y(d[1]); })
.attr("height", function(d) { return y(d[0]) - y(d[1]); })
.on("mouseover", function(d) {
tip
.html("<h4>" + d.data.key + "</h4>"
+ "<ul>"
+ "<li>"
+ layer.key + ": " + d.data.value[layer.key]
+ "</li>"
+ "<li>Total: " + d.data.value.all + "</li>"
+ "</ul>"
)
.show()
;
})
.on("mouseout", tip.hide)
;
})
;
var axes = svg
.append("g")
.attr("class", "axes")
;
axes
.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.selectAll(".tick text")
.attr("y", 0)
.attr("x", -10)
.attr("dy", ".35em")
// Rotate the axis tick labels a little bit more gently
.attr("transform", "rotate(-60)")
.style("text-anchor", "end")
;
axes
.append("g")
.attr("class", "y axis")
.call(yAxis)
;
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment