2

I have data of sales by year and model. I have been looking for a package to build a ribbon chart in R in the same way as visualized in PowerBI but could not find any good one, all of the are quite ugly. I wonder if this possible to build the same plot as this below:

df <- data.frame (model  = c("A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J"),
                          
    Year = c(2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018),
    sales = c(450,678,456,344,984,456,234,244,655,789,234,567,234,567,232,900,1005,1900,450,345,567,235,456,345,144,333,555,777,111,444,222,223,445,776,331,788,980,1003,456,434))

enter image description here

*here is the same question but nearly 5 years have gone since then.

  • 3
    Did you try the solutions from the linked post? ggalluvial ? – zx8754 Jul 05 '22 at 09:18
  • 2
    Instead of asking the question new, you can also put a bounty on the other question. This avoids creating duplicates. However, duplicates are generally not bad - Please don't delete this current question as it may increase the visibility of the linked thread – tjebo Jul 05 '22 at 10:03
  • also related https://stackoverflow.com/questions/66229164/r-connect-bar-graphs-with-lines-filled-in-with-matching-bar-color and https://stackoverflow.com/q/70837348/7941188 – tjebo Jul 05 '22 at 10:06

1 Answers1

1

You may make use of this package.

In the following you can make use of smooth argument geom_sankey_bump to control the look/feel of the chart as in ribbon chart of Power BI.

df <- data.frame (model  = c("A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J","A","B","C","D","E","F","G","H","I","J"),
                  
                  Year = c(2015,2015,2015,2015,2015,2015,2015,2015,2015,2015,2016,2016,2016,2016,2016,2016,2016,2016,2016,2016,2017,2017,2017,2017,2017,2017,2017,2017,2017,2017,2018,2018,2018,2018,2018,2018,2018,2018,2018,2018),
                  sales = c(450,678,456,344,984,456,234,244,655,789,234,567,234,567,232,900,1005,1900,450,345,567,235,456,345,144,333,555,777,111,444,222,223,445,776,331,788,980,1003,456,434))
          

#install.packages("remotes")
#remotes::install_github("davidsjoberg/ggsankey")
library(ggsankey)
library(tidyverse)

ggplot(df, aes(x = Year,
               node = model,
               fill = model,
               value = sales)) +
  geom_sankey_bump(space = 0, type = "alluvial", color = "transparent", smooth = 15) +
  scale_fill_viridis_d(option = "A", alpha = .8) +
  theme_sankey_bump(base_size = 16) +
  labs(x = NULL,
       y = "Sales ($ ths)",
       fill = "Model",
       color = NULL) +
  theme(legend.position = "bottom") +
  labs(title = "Sales per model per year")

Created on 2022-07-05 by the reprex package (v2.0.1)

AnilGoyal
  • 25,297
  • 4
  • 27
  • 45