绘制相同颜色的子图线
plotly subplot lines with same color
我有一些跨多个类别的时间序列数据,其中每个类别都有一组产品的一个子集,我想将它们绘制在一个 plotly subplot 中,以便每个产品线具有相同的颜色。我该怎么做?
我试过在颜色参数中指定一个调色板,但没有用,我也试过使用 expand_grid
用缺失的产品“填充”每个类别,但这也没有用。最后我尝试了两种方法的组合,但仍然没有用。
下面是问题的玩具数据集。正如您在图例组中看到的那样,每个类别的线条颜色不同。
data <- expand_grid(Category = c(LETTERS[1:3]), Product = letters[1:5], date = seq.Date(as.Date("2020-01-01"), as.Date("2020-12-31"), by = 7)) %>%
mutate(y_value = rnorm(nrow(.), 50, 25)) %>%
filter(!paste0(Category, Product) %in% c("Ab","Bd","Ce","Ca"))
data %>%
group_by(Category) %>%
do(
plot = plot_ly(data = ., x=~date, y = ~ y_value, color = ~Product, legendgroup = ~ Product) %>%
add_lines(hoverinfo = "text", text = ~ paste0("Category: ", Category, "<br>", "Product: ", Product)) %>%
add_annotations(text = ~Category, x = 0.5,y = ~ max(y_value), xref = "paper",showarrow = FALSE)
) %>%
subplot(nrows = 3, shareX = TRUE, shareY = FALSE)
您可以使用命名的颜色向量向量为您的产品分配颜色并将其传递给 plot_ly
的 colors
参数,如下所示:
library(plotly)
library(tidyr)
data <- expand_grid(Category = c(LETTERS[1:3]), Product = letters[1:5], date = seq.Date(as.Date("2020-01-01"), as.Date("2020-12-31"), by = 7)) %>%
mutate(y_value = rnorm(nrow(.), 50, 25)) %>%
filter(!paste0(Category, Product) %in% c("Ab","Bd","Ce","Ca"))
colors <- c("a" = "blue", "b" = "red", c = "green", d = "orange", e = "purple")
data %>%
group_by(Category) %>%
do(
plot = plot_ly(data = ., x=~date, y = ~ y_value, color = ~Product, colors = colors, legendgroup = ~ Product) %>%
add_lines(hoverinfo = "text", text = ~ paste0("Category: ", Category, "<br>", "Product: ", Product)) %>%
add_annotations(text = ~Category, x = 0.5,y = ~ max(y_value), xref = "paper",showarrow = FALSE)
) %>%
subplot(nrows = 3, shareX = TRUE, shareY = FALSE)
我有一些跨多个类别的时间序列数据,其中每个类别都有一组产品的一个子集,我想将它们绘制在一个 plotly subplot 中,以便每个产品线具有相同的颜色。我该怎么做?
我试过在颜色参数中指定一个调色板,但没有用,我也试过使用 expand_grid
用缺失的产品“填充”每个类别,但这也没有用。最后我尝试了两种方法的组合,但仍然没有用。
下面是问题的玩具数据集。正如您在图例组中看到的那样,每个类别的线条颜色不同。
data <- expand_grid(Category = c(LETTERS[1:3]), Product = letters[1:5], date = seq.Date(as.Date("2020-01-01"), as.Date("2020-12-31"), by = 7)) %>%
mutate(y_value = rnorm(nrow(.), 50, 25)) %>%
filter(!paste0(Category, Product) %in% c("Ab","Bd","Ce","Ca"))
data %>%
group_by(Category) %>%
do(
plot = plot_ly(data = ., x=~date, y = ~ y_value, color = ~Product, legendgroup = ~ Product) %>%
add_lines(hoverinfo = "text", text = ~ paste0("Category: ", Category, "<br>", "Product: ", Product)) %>%
add_annotations(text = ~Category, x = 0.5,y = ~ max(y_value), xref = "paper",showarrow = FALSE)
) %>%
subplot(nrows = 3, shareX = TRUE, shareY = FALSE)
您可以使用命名的颜色向量向量为您的产品分配颜色并将其传递给 plot_ly
的 colors
参数,如下所示:
library(plotly)
library(tidyr)
data <- expand_grid(Category = c(LETTERS[1:3]), Product = letters[1:5], date = seq.Date(as.Date("2020-01-01"), as.Date("2020-12-31"), by = 7)) %>%
mutate(y_value = rnorm(nrow(.), 50, 25)) %>%
filter(!paste0(Category, Product) %in% c("Ab","Bd","Ce","Ca"))
colors <- c("a" = "blue", "b" = "red", c = "green", d = "orange", e = "purple")
data %>%
group_by(Category) %>%
do(
plot = plot_ly(data = ., x=~date, y = ~ y_value, color = ~Product, colors = colors, legendgroup = ~ Product) %>%
add_lines(hoverinfo = "text", text = ~ paste0("Category: ", Category, "<br>", "Product: ", Product)) %>%
add_annotations(text = ~Category, x = 0.5,y = ~ max(y_value), xref = "paper",showarrow = FALSE)
) %>%
subplot(nrows = 3, shareX = TRUE, shareY = FALSE)