使用Plotly添加不同的颜色

问题描述 投票:0回答:1

这段代码生成累积条形图,如下图所示。这是我的数据集的一个小例子。这组包含约52周的每周数据。您可以使用以下代码查看数据:

# CODE
     #Data
library(tidyverse)
library(plotly)
      ARTIFICIALDATA<-dput(structure(list(week = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
        13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 
        29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 
        45, 46, 47, 48, 49, 50, 51, 52), `2019 Series_1` = c(534.771929824561, 
        350.385964912281, 644.736842105263, 366.561403508772, 455.649122807018, 
        533.614035087719, 829.964912280702, 466.035087719298, 304.421052631579, 
        549.473684210526, 649.719298245614, 537.964912280702, 484.982456140351, 
        785.929824561404, 576.736842105263, 685.508771929824, 514.842105263158, 
        464.491228070175, 608.245614035088, 756.701754385965, 431.859649122807, 
        524.315789473684, 739.40350877193, 604.736842105263, 669.684210526316, 
        570.491228070175, 641.649122807018, 649.298245614035, 664.210526315789, 
        530.385964912281, 754.315789473684, 646.80701754386, 764.070175438596, 
        421.333333333333, 470.842105263158, 774.245614035088, 752.842105263158, 
        575.368421052632, 538.315789473684, 735.578947368421, 522, 862.561403508772, 
        496.526315789474, 710.631578947368, 584.456140350877, 843.19298245614, 
        563.473684210526, 568.456140350877, 625.368421052632, 768.912280701754, 
        679.824561403509, 642.526315789474), `2020 Series_1` = c(294.350877192983, 
        239.824561403509, 709.614035087719, 569.824561403509, 489.438596491228, 
        561.964912280702, 808.456140350877, 545.157894736842, 589.649122807018, 
        500.877192982456, 584.421052631579, 524.771929824561, 367.438596491228, 
        275.228070175439, 166.736842105263, 58.2456140350878, NA, NA, 
        NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
        NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
        NA, NA)), row.names = c(NA, -52L), class = c("tbl_df", "tbl", 
        "data.frame")))
colnames(ARTIFICIALDATA) <- c('week', 'series1', 'series2')

colnames(ARTIFICIALDATA) <- c("week", "series1", "series2")

ARTIFICIALDATA %>%
  # Drop rows with NA 
  drop_na() %>% 
  # Convert to long format
  pivot_longer(-week, names_to = "series") %>%
  # Set the labels for the plot. If you want other lables simply adjust
  mutate(label = case_when(
      series == "series1" ~ "2019 Series_1",
      series == "series2" ~ "2020 Series_1")) %>%  
  # Compute sum by sereis
  group_by(label) %>%
  summarise(sum = sum(value, na.rm = TRUE)) %>% 
  ungroup() %>%
  # Plot
  plot_ly(x = ~label, y = ~sum) %>% 
  add_bars() %>%
  # Remove title for xaxis. But can you can label it as you like
  layout(xaxis = list(title = ""))

enter image description here

但是我的意图是使用两种不同颜色的条形图,第一个条形为蓝色,例如示例中的颜色,第二个条形为橙色。那么有人可以帮助我如何解决颜色问题吗?

r plotly
1个回答
0
投票

用[替换add_bars()

add_bars(marker = list(color = c("blue", "orange")))
© www.soinside.com 2019 - 2024. All rights reserved.