1

I am trying to plot a long column of subplots using subplot from the plotly package. Right now, the graphs seem to be overlapping and only the first graph is being displayed on top. I am coding in a Rmd file and exporting to an HTML file through the flexdashboard package.

I believe the issue stems from the number of subplots there are.

set.seed(123)

# 61 unique IDS
ids <- c(letters, LETTERS, paste0('id', 1:9))


df <- data.frame(
  ID = rep(ids, each = 5),
  HOUR = rep(1:5, times = length(ids)),
  ARRIVALS = sample(1:50, size = 5*length(ids), replace = TRUE)
)

plots <- lapply(unique(df$ID), function(var) {
    loc_df <- df[df$ID == var,]
    return (plot_ly(loc_df, x = ~HOUR, y = ~ARRIVALS, type="scatter", mode="line"))
})

subplot(plots, nrows = length(unique(df$ID)))

Current view:

divibisan
  • 11,659
  • 11
  • 40
  • 58
Nam Nguyen
  • 11
  • 2
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Aug 07 '23 at 20:30
  • I think it has to do with the number of rows. Here are a couple of options: `subplot(plots, nrows = ceiling(sqrt(length(unique(df$ID)))))` or `subplot(plots, nrows = ceiling(length(unique(df$ID))/2))` or you get the picture, ya? – Kat Aug 08 '23 at 23:54

0 Answers0