1

I have a dataframe, which you can get with this code:

x = data.frame(metrics=c("type1", "type1", "type1","type1", "type1", "type1", "orders", "orders", "orders","orders", "orders", "orders", "mean","mean","mean","mean","mean","mean"), hr=c(6,7,8,6,7,8,6,7,8,6,7,8,6,7,8,6,7,8), actual=c(14,20,34,22,24,27,56,12,34,11,15,45,56,78,89,111,123,156), time_pos=c("today", "yesterday", "today", "yesterday", "today", "yesterday"))

Based on a previous question I ended up with the following:

plot <- function(df) {

  subplotList <- list()
  for(metric in unique(df$metrics)){
    subplotList[[metric]] <- df[df$metrics == metric,] %>%
      plot_ly(
        x = ~ hr,
        y = ~ actual,
        name = ~ paste(metrics, " - ", time_pos),
        colors = ~ time_pos,
        hoverinfo = "text",
        hovertemplate = paste(
          "<b>%{text}</b><br>",
          "%{xaxis.title.text}: %{x:+.1f}<br>",
          "%{yaxis.title.text}: %{y:+.1f}<br>",
          "<extra></extra>"
        ),
        type = "scatter",
        mode = "lines+markers",
        marker = list(
          size = 7,
          color = "white",
          line = list(width = 1.5)
        ),
        width = 700,
        height = 620
      ) %>% layout(autosize = T,legend = list(font = list(size = 8)))
  }
  subplot(subplotList, nrows = length(subplotList), margin = 0.05)
}

and

plot(x)

gives me this: enter image description here

As you see it builded subplots for each type of values in column "metrics" and also, on each subplot there are two scatterplots for each type of value in column "time" (today, yesterday). But they are different colours. How to make "today" and "yesterday" values same colour in each subplot? So there be only two types: "today" and "yesterday" in legend and each subplot be titled as "type1", "orders", "mean"

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
french_fries
  • 1,149
  • 6
  • 22

1 Answers1

0

This could be achieved like so:

  1. To get the same colors in all plots map time_pos on color instead of colors, .i.e color = ~time_pos. Using colors I set the color palette to the default colors (otherwise I get a bunch of warnings).

  2. The tricky part is to get only two legend entries for today and yesterday. First map time_pos on legendgroup so that the traces in all subplots get linked. Second use showlegend to show the legend only for one of the plots, e.g. the first metric.

  3. To add titles to the subplots I make use of add_annotations following this post

    library(plotly)
    
    plot <- function(df) {
      .pal <- RColorBrewer::brewer.pal(3, "Set2")[c(1, 3)]
      subplotList <- list()
      for(metric in unique(df$metrics)){
        showlegend <- metric == unique(df$metrics)[1]
        subplotList[[metric]] <- df[df$metrics == metric,] %>%
          plot_ly(
            x = ~ hr,
            y = ~ actual,
            color = ~ time_pos,
            colors = .pal,
            legendgroup = ~time_pos,
            showlegend = showlegend,
            hoverinfo = "text",
            hovertemplate = paste(
              "<b>%{text}</b><br>",
              "%{xaxis.title.text}: %{x:+.1f}<br>",
              "%{yaxis.title.text}: %{y:+.1f}<br>",
              "<extra></extra>"
            ),
            type = "scatter",
            mode = "lines+markers",
            marker = list(
              size = 7,
              color = "white",
              line = list(width = 1.5)
            ),
            width = 700,
            height = 620
          ) %>% 
          add_annotations(
            text = ~metrics,
            x = 0.5,
            y = 1,
            yref = "paper",
            xref = "paper",
            xanchor = "center",
            yanchor = "bottom",
            showarrow = FALSE,
            font = list(size = 15)
          ) %>% 
          layout(autosize = T, legend = list(font = list(size = 8)))
      }
      subplot(subplotList, nrows = length(subplotList), margin = 0.05)
    }
    
    plot(x)
    
    

enter image description here

stefan
  • 90,330
  • 6
  • 25
  • 51