СustomizeRは、サブプロットで色をプロットプロットします

Aug 24 2020

私はあなたがこのコードで得ることができるデータフレームを持っています:

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

前の質問に基づいて、私は次のようになりました:

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)
}

そして

plot(x)

私にこれを与えます:

ご覧のとおり、「メトリック」列の値のタイプごとにサブプロットが作成され、また、各サブプロットには、「時間」列の値のタイプごとに2つの散布図があります(今日、昨日)。しかし、それらは異なる色です。各サブプロットで「今日」と「昨日」の値を同じ色にする方法は?したがって、凡例には「今日」と「昨日」の2つのタイプしかなく、各サブプロットには「type1」、「orders」、「mean」というタイトルが付けられます。

回答

stefan Aug 24 2020 at 15:13

これは次のように実現できます。

  1. すべてのプロットで同じ色を取得するには、.ieの代わりにマップtime_posします。を使用して、カラーパレットをデフォルトの色に設定します(そうしないと、一連の警告が表示されます)。colorcolorscolor = ~time_poscolors

  2. トリッキーな部分は、todayとの2つの凡例エントリのみを取得することyesterdayです。すべてのサブプロットのトレースがリンクさtime_posれるlegendgroupように、最初にマップします。2番目の使用showlegend法は、最初のメトリックなど、プロットの1つのみの凡例を表示するために使用します。

  3. サブプロットにタイトルを追加するには、add_annotationsこの投稿をフォローすることを利用します

    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)