关于 r:使用 plotly 更改条形图中的颜色

Change color in barplot with plotly

我有一个如下所示的条形图,它是由 plotly 包绘制的。
可以看到下面的数据和代码:

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#CODE

#Data


        test_data_set<-structure(list(Name = c("Cars_sale_1","Cars_sale_2","Cars_sale_3",
       "Cars_sale_4","Cars_sale_5","Cars_sale_6","Cars_sale_7","Cars_sale_8",
       "Cars_sale_9","Cars_sale_10","Cars_sale_11","Cars_sale_12",
       "Cars_sale_13"), First = c(156300.824706096, 10006.2467099491,
        3212.0722933848, 3319.03842779435, 9658.39620986138, 8434.32181084401,
        1367.81891559923, 717.880329882435, 260.817687313564, 196.525706264257,
        1042.98999824531, 7036.46253728724, 14974.7002155131), Second = c(227324.372696964,
        16086.4713107563, 6318.58220740481, 21832.8829619231, 15740.5860677312,
        10538.8313739252, 4399.92981224776, 2872.64432356554, 1391.68275135989,
        0, 1979.57536409896, 12618.0733462011, 20694.7337436906), Third = c(277421.301982804,
        18264.5376381821, 10922.6180031584, 30805.9659589402, 23327.3205825583,
        14162.2038954203, 9179.99649061239, 5272.22319705212, 3019.19635023688,
        0, 3587.71714335848, 17227.7241621337, 21867.2276106995), Fourth = c(307141.042288121,
        27274.1182663625, 15141.1826636252, 51266.257238112, 25035.1289699947,
        18876.8555886998, 13549.8859449026, 12045.9027899632, 4577.92595192139,
        0, 9101.66695911564, 19369.2928583962, 30971.5263285415), Fifth = c(345904.895595719,
        35406.3519915775, 21022.9163011055, 70233.5146516933, 28311.4932444288,
        22832.3565537814, 21108.8261098438, 14801.7546938059, 4776.69766625724,
        56.1502017897877, 11680.6457273206, 24203.544481488, 25989.4022630561
        ), Sixth = c(375676.013335673, 38199.2630286015, 34954.3428671697,
        96511.528338305, 33332.4442884717, 27694.4025267591, 27706.1940691349,
        26899.0349184067, 8709.73855062292, 224.600807159151, 16098.5436041411,
        31910.4404281453, 32467.4847713049), Seventh = c(433176.346727496,
        47455.623793648, 51832.251272153, 121340.024565713, 41695.1745920337,
        31331.5318476926, 44969.8543604141, 24795.9291103702, 10157.0100017547,
        828.215476399368, 27548.4120021056, 41680.0140375504, 35955.6910763933
        ), Eight = c(501520.687839972, 55052.4653447973, 74202.4916652044,
        162651.693279523, 45550.4474469205, 40385.1903842779, 54554.132303913,
        43609.6157220565, 16360.2035444815, 4171.95999298123, 45789.3665555361,
        53713.5637831198, 29226.7897579876), Ninth = c(567436.251974031,
        65858.0101772241, 104945.288647131, 238514.82716266, 60495.6659062993,
        52381.4002456571, 100849.973679593, 61956.6941568696, 27927.4258641867,
        4159.60694858747, 77211.5809791192, 69056.0449201614, 29472.1253015506
        ), Tenth = c(755730.057904896, 89047.2012633796, 208602.210914195,
        544052.500438673, 195334.760484295, 129515.213195297, 220957.50131602,
        119074.083172486, 115559.080540446, 36932.7952272328, 156449.622740832,
        120385.751886296, 33197.0639513509)), row.names = c(NA, -13L), class = c("tbl_df",
       "tbl","data.frame"))


# Draw bar chart
library(plotly)
t_df <- data.frame(t(test_data_set[,-1]))
colnames(t_df) <- test_data_set$Name
t_df$Number <- factor(row.names(t_df),levels=row.names(t_df),ordered=TRUE)

p <- plot_ly(t_df, x = ~Number, y = ~Cars_sale_1, type = 'bar', name = 'Cars_sale_1') %>%
  add_trace(y = ~Cars_sale_2, name = 'Cars_sale_2') %>%
  add_trace(y = ~Cars_sale_3, name = 'Cars_sale_3') %>%
  add_trace(y = ~Cars_sale_4, name = 'Cars_sale_4') %>%
  add_trace(y = ~Cars_sale_5, name = 'Cars_sale_5') %>%
  add_trace(y = ~Cars_sale_6, name = 'Cars_sale_6') %>%
  add_trace(y = ~Cars_sale_7, name = 'Cars_sale_7') %>%
  add_trace(y = ~Cars_sale_8, name = 'Cars_sale_8') %>%
  add_trace(y = ~Cars_sale_9, name = 'Cars_sale_9') %>%
  add_trace(y = ~Cars_sale_10, name = 'Cars_sale_10') %>%
  add_trace(y = ~Cars_sale_11, name = 'Cars_sale_11') %>%
  add_trace(y = ~Cars_sale_12, name = 'Cars_sale_12') %>%
  add_trace(y = ~Cars_sale_13, name = 'Cars_sale_13') %>%
  layout(yaxis = list(title = 'Count'), barmode = 'stack')
p

enter


我重新编辑了代码,让您更容易看到手动选择颜色的可能方法。另外,不要手动添加尽可能多的跟踪,因为如果你有 100 个,你会发疯的。按照以下方式进行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
library(plotly)
library(reshape2)
t_df <- data.frame(t(test_data_set[,-1]))

colnames(t_df) <- test_data_set$Name
t_df$Number <- factor(row.names(t_df),levels=row.names(t_df),ordered=TRUE)

t_df<-melt(t_df, id.vars = c("Number"))
t_df$color <- factor(t_df$variable, labels = c("blue","red","green","yellow","brown",
                                              "black","orange","purple","grey","white",
                                              "chocolate","coral","cornflowerblue"))

p <- plot_ly(t_df, x = ~Number, y = ~value,
             type = 'bar',
             marker = list(color = ~color), name = ~variable) %>%
  layout(yaxis = list(title = 'Count'), barmode = 'stack')
p

enter