I'm looking to use purrr::map() to create a subplot function in plotly. This previous question R: Plotly and subplot(): fastest way to create a subplot based on a factor provided map as a solution, but it looks like this doesn't work anymore with more recent versions of purrr. When I try and run the code below I get Error in : Can't convert a plotly/htmlwidget object to function.
In the example below I want 4 charts on 2 rows, with hover info, a shared legend all as a stacked 100% bar chart. Can purrr::map() work with this in plotly? Thanks in advance for your help.
library(purrr)
library(plotly)
library(dplyr)
library(tibble)
#basic dataframe
df <-
tibble::tribble(~cakes,~year,~not_sold,~sold,
"Cheese Cake", 2018, 21757, 6,
"Cheese Cake", 2019, 18115, 200,
"Cheese Cake", 2020, 16776, 2920,
"Carrot Cake", 2018, 2529, 2,
"Carrot Cake", 2019, 2142, 351,
"Carrot Cake", 2020, 1662, 298,
"Pound Cake", 2018, 7004, 21,
"Pound Cake", 2019, 8739, 316,
"Pound Cake", 2020, 6832, 2949,
"Chocolate Cake", 2018, 23761, 35,
"Chocolate Cake", 2019, 18973, 1379,
"Chocolate Cake", 2020, 14054, 6065)
#adding percentage columns
df <-
df %>%
dplyr::mutate(total = sold + not_sold,
pct_sold = (sold/total)*100,
pct_not = (not_sold/total)*100)
#subplotting
cakes <-
df %>%
split(.$cakes) %>%
purrr::map(.x = df,
.f =
plotly::plot_ly(
x = ~year,
y = ~pct_sold,
name = "Cakes sold",
type = 'bar',
text = paste0('Cakes sold: ',
format(~sold,
big.mark = ",")),
hoverinfo = 'text',
marker = list(color = 'rgb(0, 169, 244)',
width = 2))) %>%
plotly::add_trace(
y = ~pct_not,
name = "Not sold",
text = paste0('Cakes not sold: ',
format(~not_sold,
big.mark = ",")),
hoverinfo = 'text',
marker = list(color = 'rgb(230, 230, 230)'),
width = 2)) %>%
plotly::layout(barmode = 'stack') %>%
plotly::subplot(nrows = 2,
margin = .05)
cakes