Contribution plots

Published

January 5, 2023

Contribution plot using ggplot2 and plotly

library('tibble')
library('ggplot2')
library('RColorBrewer')
library('plotly')

Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':

    last_plot
The following object is masked from 'package:stats':

    filter
The following object is masked from 'package:graphics':

    layout
theme_set(theme_minimal())
#help(package = 'RColorBrewer')
nr_of_clones <- 11
nr_of_timepoins <- 20

dat <- tibble(t = rep(1:nr_of_timepoins, each = nr_of_clones),
              p = abs(rnorm(nr_of_clones * nr_of_timepoins)),
              s = rep(1:nr_of_clones, nr_of_timepoins))

dat$p <- dat$p / rep(tapply(dat$p, dat$t, sum), each = nr_of_clones)

# my_cols <- rainbow(nr_of_clones)
my_cols <- brewer.pal(nr_of_clones, 'Spectral')

(g <- ggplot(dat, aes(x = t, y = p, group = factor(s), fill = factor(s))) + 
  geom_area(alpha = 0.65) + 
  scale_fill_manual(name = 'State', values = my_cols) +
  xlab('Time') + ylab('Relative contibution'))

# ggplotly(g)

Another plotly plot

(g <- ggplot(cars, aes(dist, speed)) + geom_point())

# ggplotly(g)