Skip to content

Instantly share code, notes, and snippets.

@seemantk
Last active August 25, 2016 18:16
Show Gist options
  • Save seemantk/a75c6cf01cd341b748d4bd2f9692435a to your computer and use it in GitHub Desktop.
Save seemantk/a75c6cf01cd341b748d4bd2f9692435a to your computer and use it in GitHub Desktop.
Bar Chart I: Causes of Death in Zambia

Visualizing causes of death in Zambia: I

From the 2015 Living Conditions Survey

Introduction

In August, I presented a workshop called d3.fundamentals() at BongoHive in Lusaka, Zambia. The Zambian Government's Central Statistical Office ran a survey in 2015, to determine the Living Conditions of people throughout the country. They graciously provided anonymized datasets for us to use during the workshop.

Chinyanta Mwenya, a Zambian hacker, collaborated with me to prepare the datasets for the workshop. Chinyanta cleaned the dataset and ran statistical analysis using both Excel and R. We took some time to find a smallish dataset that was easy to understand. We settled on the unfortunately morbid Causes of Death dataset.

For the workshop I assumed a minimal knowledge of HTML and CSS, and no knowledge of Javascript (let alone any knowledge of d3). So, the workshop turned out to be, essentially From Zero to Bar Chart with d3.

Rather than a bunch of pre-done, canned examples, I opted to live-code my way through the agenda. The journey went as follows:

  • Summary/overview of basic structure of an HTML5 document
    • with a passing glance at the <style> section
  • Summary/overview of basic <svg> elements
    • <rect>angles and <circle>s
    • SVG coordinate system.
    • Use blockbuilder to draw an SVG in an HTML document
  • Overview of Javascript
    • Quick look at <script> tags in HTML documents
    • Use Chrome's developer console to interact with Javascript
      • console.log()
      • Arrays
        • Defined as a list of things
        • Addressing by array index starts at 0, not 1
        • Array.length()
      • Objects
        • Defined as a dictionary of Key/Value pairs
        • Addressing
          • . notation
          • [] notation
        • Objects can contain Arrays
        • Arrays can be a list containing Objects
  • Introduce d3 via d3.select() to manipulate the <SVG>s we drew earlier.
    • Read attributes and styles of the SVG objects (width, height, fill, stroke)
    • Change those attributes
    • Explore attributes and styles of HTML objects (<div>s)
  • Join data to a selection
    • Join an array of numbers, to a selection of <div>s
  • Draw a bar chart using a dummy array of numbers
    • First, use <div>s, as they tend to align pretty automatically
      • Use background-color and border
    • Draw it again, using SVG <rect>s instead.
      • Use fill and stroke
  • Open a data file using d3.csv()
    • Revisit Arrays and Objects

This bl.ock is the result of that demo. It is presented here with minimal cleanup (spaces/indentations and moving variable declarations near the top).

The goal with this first visualization was to introduce d3 concepts while live-coding. So we built a bar chart showing the number of people who died from each cause listed.

Fundamentals

  1. The <style> section contains styling for the axis:
  • It cleans up the lines, making them fine and sharp.
  1. We use d3.csv() to read in the data file:
  • the data file has one record per row
  • the data is read in as an array of objects (one object per record or row) that look something like:
    • {CAUSE:"FEVER/MALARIA", AGE:"38", OTHER:"", SEX:"1"}
  1. We use d3.nest() to group the array of objects by CAUSE
  • We use nest.key() to do the actual grouping. It takes a callback function that tells it how to get an object's CAUSE field.
  • We use nest.entries() to get an array of objects that look something like:
    • { key: "FEVER/MALARIA", values: [] }
      • values is an array of objects with the CAUSE specified by key
  • We use nest.rollup() to calculate the length of the values array in each object.
    • The length of thevalues array is the # of people that died of CAUSE, or key.
  1. We use scales to map from data space to screen space.
  • Causes along the x-axis using an ordinal scale
  • The bar heights correspond to the number of deaths, so we use a linear scale there.
  1. We draw an SVG <rect> for each bar in the bar chart.
  • Remember: SVG coordinates start at the top left.
    • The origin is at the top left corner of the SVG,
      • Positive x-axis points to the right.
      • Positive y-axis points down.
    • We use scale.range() to flip the y-axis (scale.range([height,0])). This allows us to present the data in a more intuitive way (with origin at bottom left).
  • We draw a <rect> for each bar, so we need to specify:
    • coordinate of top left corner of the bar:
      • x-coordinate represents the cause of deaths
      • y-coordinate represents the number of deaths
    • width of the bar:
    • height of the bar:
      • The base of each rectangle will be at the bottom of the SVG (at height in SVG (y)coordinates). So, the height is the difference between the y-coordinate of the top-left corner and y-coordinate of the base
  1. We label the bar by using the axis labels of a d3.svg.axis()
  • The tick labels are the Causes.
  • We use transform#rotate() to rotate the labels so they don't overlap each other.
    • The labels are rotated clockwise by 90 degrees (-90) to flow down-to-up

Analysis

This is a great start to visualizing this dataset. We can easily tell the relative seriousness of each cause of death. We can see that FEVER/MALARIA kills about 2 times more people as the next cause of death (OTHER, or miscellaneous, causes).

Improvements

It's a basic chart that gives us a basic insight into the dataset. By making some small changes, we can vastly improve our insight.

Next, let's make the chart easier to read.

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.v3.min.js"></script>
<style>
/*
** Style for the axis:
**
*/
.axis text {
font: 10px sans-serif;
}
.axis path,
.axis line { /* Style all lines and line segments (e.g. ticks) */
fill: none; /* Make the line thin (remove the fill). */
stroke: #000; /* Draw it in black. */
shape-rendering: crispEdges; /* Make it look sharp */
}
</style>
</head>
<body>
<script>
d3.csv("deaths.csv", function(error, data) {
if(error) throw error;
/*
** At this point, the CSV file exists as an
** array of javascript Objects, and stored
** in the 'data' variable.
**
*/
var causes = d3.nest()
// Group by Cause
.key(function(d) { return d.CAUSE; })
.rollup(function(leaves) {
// 'leaves' is an array of objects that have the same CAUSE
// The length of the array is the number of people who died
// from the same CAUSE.
return leaves.length;
})
.entries(data)
.sort(function(a, b) {
// Sort the causes in descending order by total # of deaths
return d3.descending(a.values, b.values);
})
, extent = [0, d3.max(causes, function(d) { return d.values; })]
;
var margin = { top: 20, bottom: 200, left: 40, right: 20 }
, height = 500 - margin.top - margin.bottom
, width = 700 - margin.left - margin.right
, x = d3.scale.ordinal()
.domain(causes.map(function(d) { return d.key; }))
.rangeRoundBands([0, width])
, y = d3.scale.linear()
.domain(extent)
.range([height, 0])
, xAxis = d3.svg.axis()
.orient("bottom")
.scale(x)
, svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate("+margin.left+","+margin.top+")")
, rect = svg.selectAll("rect")
.data(causes)
;
rect
.enter().append("rect")
.attr("x", function(d) { return x(d.key); })
.attr("width", x.rangeBand())
.attr("y", function(d) { return y(d.values); })
.attr("height", function(d) { return height - y(d.values); })
.style("stroke", "red")
.style("fill", "steelblue")
;
svg
.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
;
svg.select(".x.axis").selectAll(".tick text")
.attr("y", 0)
.attr("x", -10)
.attr("dy", ".35em")
.attr("transform", "rotate(-90)")
.style("text-anchor", "end")
;
})
;
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment