Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ribbon chart in R

I search in R implementation (may be html widget on java script) a stacked bar chart in ribbon style, which allows you to see the rating change for each category in the dynamics.

It's look like ribbon chart in power bi desktop enter image description here

Search rseek.org gave no results.

like image 329
Edvardoss Avatar asked Feb 01 '18 07:02

Edvardoss


2 Answers

First off: Not a fan of that ribbon-styled stacked bar chart at all; while colourful and stylish, it's difficult to synthesise the relevant information. But that's just my opinion.

You could try building a similar plot in ggplot2 using geom_ribbon. See below for a minimal example:

# Sample data
set.seed(2017);
one <- sample(5:15, 10);
two <- rev(one);
df <- cbind.data.frame(
    x = rep(1:10, 2),
    y = c(one, two),
    l = c(one - 1, two - 1),
    h = c(one + 1, two + 1),
    id = rep(c("one", "two"), each = 10));


require(ggplot2);
ggplot(df, aes(x = x, y = y)) +
    geom_ribbon(aes(ymin = l, ymax = h, fill = id), alpha = 0.4) +
    scale_fill_manual(values = c("#E69F00", "#56B4E9"));

enter image description here

If you need interactivity, you could wrap it inside plotly::ggplotly.

like image 172
Maurits Evers Avatar answered Sep 23 '22 10:09

Maurits Evers


Using ggsankey 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")


On suggestion in comments, I tried replicating some of the features of power BI chart.

# Prepare some data
set.seed(1)
df <- data.frame(
  occupation = rep(c("Clerical", "Management", "Manual", "Professional", "Skilled"), 12),
  Month = factor(rep(month.abb, 5), levels = month.abb, ordered = TRUE),
  Sales = sample(200:1000, 60, replace = TRUE)
)

df %>% 
  group_by(Month) %>% 
  mutate(Max = sum(Sales)) %>% 
  ungroup() %>% 
  mutate(Max = max(Sales)) %>% 
  ggplot(aes(x = Month,
               node = occupation,
               fill = occupation,
               value = Sales)) +
  geom_col(aes(x = Month, y = Max/1.2),
           alpha = 0.5,
           fill = 'grey',
           width = 0.4) +
  geom_sankey_bump(space = 15, 
                   type = "alluvial", 
                   color = "transparent", 
                   smooth = 8,
                   alpha = 0.8) +
  scale_fill_brewer(palette = "Set3") +
  theme_minimal() +
  labs(x = NULL,
       y = "Sales ($ ths)",
       fill = "Occupation",
       color = NULL) +
  theme(legend.position = "top") +
  labs(title = "Sales per occupation per month")

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

like image 30
AnilGoyal Avatar answered Sep 22 '22 10:09

AnilGoyal