简体   繁体   English

R中的颜色和图例

[英]color and legend in plotly in R

I have a data set as follow :我有一个数据集如下:

data = structure(list(year = c(2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 
2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L, 2021L
), month = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L), Quarter = c(1L, 
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 
2L, 3L, 3L, 3L, 4L, 4L, 4L), project = c("A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B"), value = c(102.349429992044, 
106.58161342807, 100.435891304271, 98.444807600341, 82.3101711995535, 
59.6035963287678, 69.6231234694286, 90.5898095230998, 80.6258589573775, 
115.639565579428, 104.73836165791, 107.508003106277, 90.4082358328098, 
112.579438593004, 106.624680336326, 93.9307819392979, 75.4136657889693, 
52.3110190297094, 70.3105808070076, 87.3448099614908, 68.2935766548446, 
124.204436344695, 111.619576683155, 109.225885313817), Country = c("Denmark", 
"Denmark", "Denmark", "Denmark", "Denmark", "Denmark", "Denmark", 
"Denmark", "Denmark", "Denmark", "Denmark", "Denmark", "Germany", 
"Germany", "Germany", "Germany", "Germany", "Germany", "Germany", 
"Germany", "Germany", "Germany", "Germany", "Germany"), LongTermWI = c(121.960664674364, 
104.723767102727, 109.956110038786, 94.7909742884892, 89.0611848528951, 
83.0143004308842, 78.5554847511495, 82.1932844238529, 94.8317262446894, 
109.741770216839, 109.224438221904, 121.94629475342, 124.912696115337, 
106.137678558707, 111.196799677912, 90.7373556419141, 88.5814900982324, 
78.4127049610748, 74.8773631279842, 81.5579488440033, 93.2896819041917, 
114.322908768119, 114.660984633633, 121.312387668891), MinRef = c(89.0152351848971, 
47.1805056248264, 72.920410008137, 66.0807724144165, 54.5679150901317, 
53.7844552456038, 42.6401185444772, 52.546635367643, 69.2248217126283, 
76.4144846076876, 89.4209199082177, 80.3882525480035, 90.4082358328098, 
64.6192521242945, 85.1337944481354, 69.4221826905899, 50.3506836843003, 
52.3110190297094, 40.4296442260575, 47.5775452531874, 68.2935766548446, 
71.9901338300631, 93.2483160688902, 85.5467987151896), MaxRef = c(163.771100449271, 
141.388975655703, 137.780711496641, 118.055928781909, 113.961805078013, 
114.604519185711, 104.83540276271, 101.855462747317, 119.07394843672, 
137.773221892607, 140.864382733085, 156.516066856324, 158.822912815973, 
134.265032081886, 134.231205540578, 108.891671902872, 118.091190791042, 
100.740245891658, 95.6179422824695, 101.998782325545, 132.191355352224, 
137.281168224106, 153.155278763207, 152.772666775097), Delta = c(-19.61123468232, 
1.85784632534323, -9.52021873451493, 3.6538333118518, -6.75101365334163, 
-23.4107041021164, -8.93236128172087, 8.39652509924694, -14.2058672873119, 
5.8977953625887, -4.48607656399371, -14.4382916471429, -34.5044602825272, 
6.44176003429672, -4.5721193415857, 3.19342629738384, -13.1678243092631, 
-26.1016859313654, -4.56678232097656, 5.78686111748746, -24.9961052493471, 
9.88152757657565, -3.04140795047796, -12.0865023550742)), row.names = c(NA, 
-24L), class = c("tbl_df", "tbl", "data.frame"))

I can plot the project data in a grouped bar chart in Plotly and add the LongTermWI as line.我可以在Plotly的分组条形图中绘制project数据,并将LongTermWI添加为线。 I don't know how could I plot the lines in different color that the bar chart !我不知道如何绘制条形图不同颜色的线条!

fig <- plot_ly(data , x = ~month, y = ~value, type = 'bar', color =~project)%>%
add_trace(x = ~month, y = ~LongTermWI, type = 'scatter', mode = 'lines')

fig

The colors command did not help ! colors命令没有帮助! Also for legend I would like to see the project + country as legend !同样对于传奇,我希望将project + country视为传奇! 在此处输入图像描述

There are many different ways to accomplish your goal.有许多不同的方法可以实现您的目标。 It really depends on what the ultimate goal is.这真的取决于最终目标是什么。 I'm going to show you two different ways that this could work.我将向您展示两种不同的方式来实现这一点。

In both of these approaches, I essentially make it so that each trace is independent.在这两种方法中,我基本上都使每个跟踪都是独立的。

In this method, I get to pick the exact color.在这种方法中,我可以选择准确的颜色。 If there were more than a few possible colors, I would use vectorization or a loop (ie, lapply , for , etc.)如果有多个可能的颜色,我会使用矢量化或循环(即lapplyfor等)

fig <- plot_ly() %>% 
    add_bars(data = data , x = ~month, y = ~value, color = ~project) %>%
    add_lines(data = data[data$project == "A",], x = ~month,
              y = ~LongTermWI, color = I("red")) %>% 
    add_lines(data = data[data$project == "B", ], x = ~month,
              y = ~LongTermWI, color = I("black"))

在此处输入图像描述

In this next option, I let Plotly choose the colors.在下一个选项中,我让 Plotly 选择颜色。 Instead of designating color variable (and Plotly potentially catching on that the color groups have the same name), I use split .我没有指定颜色变量(并且可能会发现颜色组具有相同的名称),而是使用split

plot_ly() %>% 
  add_bars(data = data, x = ~month, y = ~value, color = ~project) %>%
  add_lines(data = data, x = ~month, y = ~LongTermWI, split = ~project)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM