2.3 mbar.rb Draw bar graph

The command creates a bar graph. By specifying the attributes assigned to x axis and y axis, users can create 1 dimensional or 2 dimensional bar matrix. The output of the graph is saved as HTML file which can be displayed in popular browsers.

The input data in CSV format is shown in Table 2.4. The fields that construct the bar graph can be specified at f= parameter. The attributes assigned to x-axis and y-axis can be specified in k= parameter. When one field is specified at k= parameter, one-dimensional bar graph matrix is created, if two fields are specified at k= parameter, two-dimensional bar graph matrix is created. When k= parameter is not specified, one bar graph is created.

The bar graph is created with the JavaScript library D3.js (Data-Driven Documents). Please refer to the official website of D3.js (http://d3js.org/) for more details. Note that Nysol/mcmd library is required for this command.

Table 2.4: Number of individuals by age and prefecture

Pref

Age

Population

Nara

10

310504

Nara

20

552339

Nara

30

259034

Nara

40

450818

Nara

50

1231572

Nara

60

1215966

Nara

70

641667

Hokkaido

10

310504

Hokkaido

20

252339

Hokkaido

30

859034

Hokkaido

40

150818

Hokkaido

50

9231572

Hokkaido

60

4215966

Hokkaido

70

341667


2.3.1 Format

  mbar.rb [i=] f= v= [o=] [k=] [title=] [width=] [height=] [cc=] [--help]

i=

Input data file name (CSV format)

f=

Specify the field name of component.

 

Null data is ignored.

v=

Specify the field name of component ratio (item that determines the height of the bar graph).

 

Data is treated as zero if it consists of null value.

 

Accepts minus, decimal values.

 

Values started with 0 is ignored. Values except numeric characters returns error.

o=

Output file name (HTML file)

k=

Specify two or less field names assigned to x-axis and y-axis.

 

Create one bar graph when this parameter is not specified.

 

Create a one-dimensional bar graph when one field is specified,

 

create two-dimensional bar graph when two fields are specified.

title=

Specify the title of the graph.

width=

Specify the width of drawing frame for the bar graph (default is 250, 1 bar graph is 600) .

height=

Specify the height of drawing frame for the bar graph (default is 250, 1 bar graph is 400).

cc=

Specify the maximum number of bars to be displayed in a row (default is 5) .

 

Specify for one-dimension pie chart matrix.

--help

Show help

Noted that in mbar.rb command, the fields specified in f= parameter and k= parameter do not have automatic sorting function. It is necessary to sort the data prior to this command as needed.

2.3.2 Example

例1: Basic Example

Using the data dat1.csv, set Age as the unit of composition, draw a one dimensional bar graph showing the population. The output of the bar chart can be viewed in the web browser. Note that in the bar chart, when you place the mouse cursor over the bar chart, details of the unit of composition is shown in a pop-up box. The graph can also be moved by dragging the mouse, and the size of the graph can be scaled by scrolling the mouse.

$ more dat1.csv
Age,Population
10,310504
20,552339
30,259034.5555
40,0450818
50,1231572
60,1215966
70,641667
$ mbar.rb i=dat1.csv v=Population f=Age o=result1.html
#END# /usr/bin/mbar.rb i=dat1.csv v=Population f=Age o=result1.html
$ head result1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
  font: 10px sans-serif;
}
\includegraphics[scale=0.5]{figure/mbar1.eps}

例2: Draw a one dimensional bar graph matrix

Using the data dat2.csv, set Age as the unit of composition, draw the bar graph showing the population. Specify Pref at ¥verb|k=| parameter, which designates pref on the x-axis, extended horizontally as a one-dimensional bar graph. Specify the title of the graph at title= parameter.

$ more dat2.csv
Pref,Age,Population
Nara,10,310504
Nara,20,552339
Nara,30,259034
Nara,40,450818
Nara,50,1231572
Nara,60,1215966
Nara,70,641667
Hokkaido,10,310504
Hokkaido,20,252339
Hokkaido,30,859034
Hokkaido,40,150818
Hokkaido,50,9231572
Hokkaido,60,4215966
Hokkaido,70,341667
$ mbar.rb i=dat2.csv k=Pref v=Population f=Age o=result2.html title='Population of Nara and Hokkaido by Age Group'
#END# /usr/bin/mbar.rb i=dat2.csv k=Pref v=Population f=Age o=result2.html title='Population of Nara and Hokkaido by Age Group'
$ head result2.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
  font: 10px sans-serif;
}
\includegraphics[scale=0.5]{figure/mbar2.eps}

例3: Draw a two dimensional bar graph matrix

Using the data dat3.csv, set ThemePark as the unit of composition, draw the bar graph showing the number item, and set the width= at 200, and height= at 150. Specify Gender and Age at k= parameter, which designates Gender on the x-axis extended horizontally, and Age on y-axis extended vertically.

$ more dat3.csv
Gender,Age,ThemePark,Number
Male,30,Disney,100
Male,30,UFJ,59
Male,30,Umeyashiki,180
Male,40,Disney,200
Male,40,UFJ,3
Male,40,Umeyashiki,10
Male,50,Disney,110
Male,50,UFJ,40
Female,30,Umeyashiki,100
Female,30,Disney,80
Female,30,UFJ,200
Female,40,Disney,90
Female,40,UFJ,80
Female,40,Umeyashiki,120
Female,50,Disney,99
Female,50,UFJ,80
Female,50,Umeyashiki,110
$ mbar.rb i=dat3.csv k=Gender,Age v=Number f=ThemePark o=result3.html
#END# /usr/bin/mbar.rb i=dat3.csv k=Gender,Age v=Number f=ThemePark o=result3.html
$ head result3.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
  font: 10px sans-serif;
}
\includegraphics[scale=0.5]{figure/mbar3.eps}