Сustomize R plotly warna dalam subplot
Saya memiliki dataframe, yang bisa Anda dapatkan dengan kode ini:
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"))
Berdasarkan pertanyaan sebelumnya, saya menyimpulkan sebagai berikut:
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)
}
dan
plot(x)
berikan saya ini:
Seperti yang Anda lihat, subplot dibuat untuk setiap jenis nilai di kolom "metrik" dan juga, di setiap subplot ada dua diagram sebar untuk setiap jenis nilai di kolom "waktu" (hari ini, kemarin). Tapi warnanya berbeda. Bagaimana membuat nilai "hari ini" dan "kemarin" memiliki warna yang sama di setiap subplot? Jadi hanya ada dua jenis: "hari ini" dan "kemarin" dalam legenda dan setiap subplot diberi judul sebagai "type1", "orders", "mean"
Jawaban
Ini dapat dicapai seperti ini:
Untuk mendapatkan warna yang sama di semua plot peta
time_pospadacolorbukancolors, Yaitucolor = ~time_pos. Menggunakancolorssaya mengatur palet warna ke warna default (jika tidak, saya mendapatkan banyak peringatan).Bagian yang sulit adalah mendapatkan hanya dua entri legenda untuk
todaydanyesterday. Peta pertamatime_pospadalegendgroupsehingga jejak di semua subplot mendapatkan link. Penggunaan keduashowlegenduntuk menampilkan legenda hanya untuk salah satu plot, misalnya metrik pertama.Untuk menambahkan judul ke subplot, saya memanfaatkan posting
add_annotationsinilibrary(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)