top of page

Mastering Stream Graphs in R: A Step-by-Step Guide

Writer: Bernard KilonzoBernard Kilonzo
a person with laptop

What is a Streamgraph?

A streamgraph is a type of visualization used to display changes in data values over time, in a way that's visually engaging and often aesthetically pleasing. Unlike traditional line or bar graphs, stream graphs use flowing, organic shapes that resemble streams or rivers, making them particularly effective for showing overlapping data sets with dynamic trends.

Each "stream" in the graph represents a category or data set, and the width of the stream at any given point corresponds to the value of that data at a specific time. These streams are stacked on top of each other, creating a layered appearance. Stream graphs are commonly used in fields like data journalism, music streaming trends, and social media analytics to illustrate evolving patterns.

Step-by-step guide

To create a streamgraph in R, install and load the required packages as shown below.

installing and loading packages in r

Set your working directory and load the dataset as shown below.

setting working directory and loading dataset

In this example, I am using the “store_data” shown below.

sample dataset

Create a streamgraph using ggplot and geom_stream functions as shown below.

code on how to create a stream graph in r

Executing the above code results in the view below.

example of a stream graph

Note, by default the streams are stacked symmetrically around the x-axis. However, you can use other options such as “ridge” – which stacks the streams from the x-axis or “proportional” - which makes the streams sum up to 1.

See the codes, for the stream graph types.

code on different stream graph types

Executing the above results in.

examples of stream graph types

Using paletteer package you can customize color as shown below.

customing colors of a stream graph using paletteer package

Executing the above code results in.

stream graph with customized colors

Lastly, you can declutter your view using theme_minimal as shown below.

applying the minimal theme on a stream graph

Executing the above results in.

stream graph final view

Conclusion

Creating a stream graph in R is a straightforward and rewarding process that combines thoughtful data preparation with the powerful visualization capabilities of libraries like ggplot2 and ggstream. By ensuring your data is structured properly and exploring customization options, you can design compelling stream graphs that effectively illustrate trends over time. Whether for research, presentations, or data storytelling, stream graphs are a creative way to transform complex datasets into visually engaging narratives. With the tools and guidance provided, you're equipped to bring dynamic visualizations to life.

If you like the work we do and would like to work with us, drop us an email on our contacts page and we’ll reach out!

Thank you for reading!

Original.png

We Support You Deliver Business-Focused Solutions That Enable Data-Driven Decision Making.

  • Tableau profile
  • YouTube
  • White LinkedIn Icon
  • Facebook
  • X

QUICK LINKS

CONTACT US

+254 738 307 495

East Gate Mall, Donholm

3rd Floor Suite No. 3i

Nairobi, Kenya

Join our mailing list

bottom of page