A32 Multiset Bar Chart - cimat/data-visualization-patterns GitHub Wiki

#A 3.1: MULTISET BAR CHART

Description

The diagram let the user inspect and compare several sets of quantitative data while occupying significantly less display space than a set of multiple conventional bar charts (Behrens, 2008).

Required Data

Use a multiset bar chart to display various datasets of quantitative data items within one diagram. Note that you can use several independent datasets, but that you have to make sure that each set contains the same nominal class of discrete items (Behrens, 2008).

Usage

Create a Cartesian coordinate system. Identify those data items from different sets that belong to the same entity. Separate the x-axis into regular segments, and attach to each of these segments one of the data entities you want to display. For each data item belonging to a specific group, attach a vertical rectangle above the corresponding label on the x-axis and adjust its height according to the data value it represents. Visually separate the entity groups from each other and bundle their bars (Behrens, 2008).

Rationale

Multiset bar charts allow the user to compare the magnitude of data items by two separate discrete parameters. It is often used to subdivide discrete entities for further and more detailed exploration, like for instance the months in an un- employment chart that are divided into male and female figures (Behrens, 2008).

Related Patterns

  • A 3.1 Simple Bar Chart
  • A 3.3 Dot Matrix
  • A 3.5 Isometric Bar Chart

PYTHON IMPLEMENTATION

Data Set

from datos import data
d=data('mtcars')
d.head()

Dependences

  • Matplotlib
  • Seaborn
  • Pyqtgraph
  • Pandas

Code Example

Matplotlib

import numpy as np
import matplotlib.pyplot as plt
from datos import data
import pandas

d=data('mtcars')
t1 = d.pivot_table( values = 'carb',index=['cyl'], columns = ['gear'],
aggfunc = len)
bar_width = 0.15
opacity = 0.4
rects1 = plt.bar(t1.columns, t1.values[0], bar_width,
alpha=opacity,color='g')
rects2 = plt.bar(t1.columns + bar_width, t1.values[1], bar_width,
alpha=opacity, color='r')
rects3 = plt.bar(t1.columns +2* bar_width, t1.values[2], bar_width,
alpha=opacity, color='y')
plt.xlabel('Number of Gears')
plt.ylabel('Frequency')
plt.title('Car Distribution by Gear and Cylindres')
plt.legend(t1.index, title='Cylindres')
plt.show()

{width=12 cm}

Seaborn

import seaborn as sns
import matplotlib.pyplot as plt
from datos import data
import pandas

sns.set(style="whitegrid")
d=data('mtcars')
g = sns.factorplot("gear",hue="cyl", data=d,size=6, kind="count")
g.set_axis_labels( "Number of Gears", "Frequency")
plt.title('Car Distribution by Gear and Cylindres', family='Serif',
size=16)
plt.show()

{width=12 cm}

Pyqtgraph

from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg
import numpy as np
from datos import data
import pandas

d=data('mtcars')
t1 = d.pivot_table( values = 'carb',index=['cyl'], columns = ['gear'],
aggfunc = len)
bar_width = 0.25
win = pg.plot(title='Simple Bar Chart')
bg1 = pg.BarGraphItem(x=t1.columns, height=t1.values[0],
width=bar_width, brush='g')
bg2 = pg.BarGraphItem(x=t1.columns+bar_width, height=t1.values[1],
width=bar_width, brush='r')
bg3 = pg.BarGraphItem(x=t1.columns+2*bar_width, height=t1.values[2],
width=bar_width, brush='y')
win.addItem(bg1)
win.addItem(bg2)
win.addItem(bg3)
win.setTitle('Car Distribution by Gear and Cylindres ')
win.setLabel('left', "Frequency", )
win.setLabel('bottom', "Number of Gears")

if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore,
'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

{width=12 cm}

References

R IMPLEMENTATION

Data Set

counts <- table(mtcars$cyl, mtcars$gear)
counts

##    
##      3  4  5
##   4  1  8  2
##   6  2  4  1
##   8 12  0  2

Dependences

  • lattice
  • ggplot2

Code Example

Graphics

counts <- table(mtcars$cyl, mtcars$gear)
barplot(counts, main="Car Distribution by Gears and Cyl",
  xlab="Number of Gears", ylab="Frequency", col=c("darkblue","red"),
    legend = rownames(counts), beside=TRUE)

Lattice

library("lattice")
counts <- table(mtcars$gear, mtcars$cyl)
barchart(counts, stack = FALSE, horizontal="false", main="Car Distribution by Gears and Cyl",     xlab="Number of Gears", ylab="Frequency",
         auto.key = list(space="top", columns=3, title="Cyl", cex.title=1))

ggplot2

library("ggplot2")

## Warning: package 'ggplot2' was built under R version 3.2.2

g <- ggplot(mtcars, aes(factor(gear), fill=factor(cyl))) + geom_bar( position = "dodge")
g + labs(list(title = "Car Distribution by Gears and Cyl", x="Number of Gears", y="Frequency"))

References

⚠️ **GitHub.com Fallback** ⚠️