Search code examples
rplotlyr-plotly

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. 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 ! Also for legend I would like to see the project + country as legend ! enter image description here


Solution

  • 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 (i.e., lapply, for, etc.)

    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"))
    

    enter image description here

    In this next option, I let Plotly choose the colors. Instead of designating color variable (and Plotly potentially catching on that the color groups have the same name), I use split.

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

    enter image description here