How to Draw Interactive Steamgraph in R
Streamgraph is a type of stacked area graph which is displaced around a central axis, resulting in a flowing, organic shape. Streamgraphs and their use were popularized by Lee Byron in a February 2008 New York Times article on movie box office revenues.
Data
Here we have census data about GDP values in some counties in southeast China.
Convert to 1-dimensional table
We have a 2-dimensional table above. Before drawing streamgraph, we need to convert 2-dimensional table to 1-dimensional table. We can use Microsoft Excel
to convert it.
Press Alt + D + P
, then we have the widget.
Select the data need to be ploted.
Hit complate.
Now we have a new table, double click
the bottom right sum value. Then we will have a 1-dimensional table.
Draw the graph in Rstudio
Because read_csv
can not read Chinese characters correctly, we use read_excel
to input the data.
1 | library(readxl) |
Install streamgraph
package from github.
1 | devtools::install_github("hrbrmstr/streamgraph") |
Draw the graph;
1 | streamgraph(df, key="name", value="gdp", date="year" , offset="zero") %>% |
References
GitHub | hrbrmstr | streamgraph
Art from data | #155 INTERACTIVE STREAMGRAPH | CHANGE OFFSET