A23 Stacked Area Chart - cimat/data-visualization-patterns GitHub Wiki

A 2.3: Stacked Area Chart

Description

An area chart displays the development of quantitative values over an interval. It resembles a line chart as it uses lines connecting data points with each other. The specific characteristic of the area chart is that the area below the line is filled with a certain color or texture. When more lines are added to the graphic, they are “stacked” on top of the previous line. This makes the area chart a preferred diagram type for displaying several data sets within the same chart with stress on a total magnitude several variables add to.

Required Data

Use stacked area charts for the display of sets of one or more quantitative values observed against a continuous interval, such as time. More than one value per dataset is possible (and even desirable for this type of display), but they should all be scaled identically. The crucial reason why you decide in favor of this display type (and against line charts) is that not only the magnitudes of the single variables count. Instead, the total amount the several variables contribute takes center stage.

Usage

Create a Cartesian coordinate grid. Label and subdivide the axes according to the value range of your underlying data. Define a color or texture for each of your variables to tell them clearly apart. Display the first set of variables as a line over the interval on the x-axis. With the second variable, for each x-value add the amount of the first variable and draw the data point accordingly, as if to “stack” the data points of the second set on top of the first one. Proceed likewise with the remaining variables until you have a stack of lines. Now fill the areas between two respective lines with the color or texture assigned to the upper line. Make it clearly distinguishable from its neighbors.

Rationale

Stacked area charts combine the widespread understanding of line charts with the strong visual power of filled areas. While the values of the single variables are often only approximately to tell (since the stacked areas have no common and even measurement base), the total magnitude of the accumulated variables becomes directly visible.

Related Patterns

A 2.1 Simple Line Chart

A 2.2 Multiset Line Chart

A 5.1 Sankey Diagram

Python Implementation Pattern

Like charts Simple Line Chart, charts Stacked Area show a line graph with the area painted increase, this area allows you to view a mirrored by completing a total area colored advance, this kind of graphs can show commonly two or more comparisons on a graph

Data Set

For this example it will be used Data Set called mtcars, this data set is the R default data set, to use this data set, was used a Python module called rpy2, which is used to use data sets of R in python. This data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).

Dependencies

List of Modules that are required for implementation

Matplotlib

Plotly Plotly for Python an R is an interactive, browser-based charting library built on the open source JavaScript graphing library, plotly.js. It works entirely locally, through the HTML widgets framework. To use this depndence you nedd to have a plotly account. [https://plot.ly/python/]

Seaborn

Code example

Code Example With Matplotlib and Plotly

This example show a graph between mpg(miles per gallon) and Cyl(Cylinders) from data set Mtcars The orange area belongs to data Mpg and blue area belongs to data Cyl


import numpy as np
import matplotlib.pyplot as plt
import plotly.plotly as py
from datos import data

d=data('mtcars')
carb=d.cyl
wt= d.mpg
y0 = carb
y1 = wt

fig, ax = plt.subplots()
ax.plot(y0, label='y0')
ax.plot(y1, label='y1')

update = {'data':[{'fill': 'tonexty'}]}
plot_url = py.plot_mpl(fig, update=update, strip_style=True, filename
='mpl-stacked-line')

Code Example With Seaborn


import matplotlib.pyplot as plt
import seaborn as sns
from datos import data
import numpy as np

d=data('mtcars')
c=d.cyl
m=d.mpg

y = np.row_stack((c,m))
x = np.arange(32)

y1, y2 = c, m


fig, ax = plt.subplots()
ax.stackplot(x, y1, y2)
sns.set_style("whitegrid")
plt.title('Motor Trend Car Road Tests Mpg and Cyl')
plt.show()

R Implementation Pattern

like charts Simple Line Chart, charts Stacked Area show a line graph with the area painted increase, this area allows you to view a mirrored by completing a total area colored advance, this kind of graphs can show commonly two or more comparisons on a graph.

Data Set

For this example it will be used Data Set called mtcars, this data set is the R default data set this data was extracted from the 1974 Motor Trend US magazine, and comprises fuel consumption and 10 aspects of automobile design and performance for 32 automobiles (1973–74 models).

head(mtcars)

##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

Dependencies

Graphics - default package on R

For this example in graphics it will be used other dependence called plot_area.R this dependence is hosted in this link: [https://gist.github.com/fawda123/6589541/raw/8de8b1f26c7904ad5b32d56ce0902e1d93b89420/plot_area.r]

Ggplot2

Code example

Code Example With Graphics

source('Continuous Quantities/plot_area.r')
datos<-data.frame(mtcars$cyl,mtcars$mpg)
plot.area(datos,prop=F,horiz=T)

Code Example With Ggplot2

library(ggplot2)
data.set <- data.frame(
  Type = c(rep(mtcars$mpg, 2),rep(mtcars$cyl, 2)),
  Comparacion = rep(c('Millas por galón', 'Cilindros'), 2),
  Value = rpois(32, 20)
)

qplot(Type, Value, data = data.set, fill = Comparacion, geom = "area")

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