0
votes

I am new to plotly and not very good with R. I am trying to do stack plots and ended up with a very cumbersome code, that I am sure could be simplify using RColorbrewer and perhaps ggplot2 to group my stacked bar plots, but I am unsure on how to do it. Below is the data I used, which is in a data.frame called data2


           Nation glider radar AUV ROV USV corer towed_eq Seismic_eq Drill_rig Manned_sub Other clean
1         Belgium      0     0   1   1   1     3        0          0         0          0     0     6
2        Bulgaria      0     0   0   0   0     0        1          0         0          1     0     2
3         Croatia      0     2   1   2   0     0        0          0         0          0     0     5
4          Cyprus      3     0   0   0   0     0        0          0         0          0     0     3
5         Estonia      0     0   0   1   0     0        0          0         0          0     0     1
6         Finland      1     0   0   0   0     0        0          0         0          0     0     1
7          France     11     2   3   1   0     1        1          3         0          1     0    23
8         Germany     18     3   3   4   0     0        1          4         2          1     0    36
9          Greece      1     0   0   3   0     0        0          0         0          0     0     4
10        Ireland      0     0   0   2   0     0        0          0         0          0     0     2
11          Italy     10     8   3   2   4     0        0          1         0          0     0    28
12          Malta      0     2   0   0   0     0        0          0         0          0     0     2
13    Netherlands      0     2   0   0   0     0        0          0         0          0     0     2
14         Norway     17     3   1   3   0     1        3          1         0          0     1    30
15         Poland      0     0   0   1   0     0        0          0         0          0     0     1
16       Portugal      0     3   6   6   4     2        1          0         0          2     1    25
17        Romania      0     0   0   1   0     0        0          0         0          0     0     1
18       Slovenia      0     1   0   0   0     0        0          0         0          0     0     1
19          Spain     12    17   2   1   0     0        0          2         0          0     0    34
20         Sweden      0     2   1   3   0     0        0          0         0          0     0     6
21         Turkey      0     0   0   0   0     0        0          0         0          2     0     2
22 United Kingdom      0     0  13   4   1    11        4          2         1          0     4    40
23        Unknown      5     0   0   0   0     0        0          0         0          0     0     5

And this is the code I used

fig <- plot_ly(data2, x = ~Nation, y = ~glider, type = 'bar', name = 'Glider')
fig <- fig %>% add_trace(y = ~radar, name = 'Radar', marker=list(color='rgb(26, 118, 255)'))
fig <- fig %>% add_trace(y = ~AUV, name = 'AUV',marker=list(color='rgb(255, 128, 0)'))
fig <- fig %>% add_trace(y = ~ROV, name = 'ROV',marker=list(color='rgb(204, 0, 0)'))
fig <- fig %>% add_trace(y = ~USV, name = 'USV',marker=list(color='rgb(51, 255, 153)'))
fig <- fig %>% add_trace(y = ~corer, name = 'Corer',marker=list(color='rgb(204, 0, 204)'))
fig <- fig %>% add_trace(y = ~towed_eq, name = 'Towed equipment',marker=list(color='rgb(255, 255, 51)'))
fig <- fig %>% add_trace(y = ~Seismic_eq, name = 'Seismic equipment',marker=list(color='rgb(255, 204, 229)'))
fig <- fig %>% add_trace(y = ~Drill_rig, name = 'Drill rig',marker=list(color='rgb(102, 255, 255)'))
fig <- fig %>% add_trace(y = ~Manned_sub, name = 'Manned submersible',marker=list(color='rgb(128, 255, 0)'))
fig <- fig %>% add_trace(y = ~Other, name = 'Other equipment',marker=list(color='rgb(153, 153, 0)'))
fig <- fig %>% layout(xaxis = list(title = "",tickfont = list(size = 14)), yaxis = list(title = 'Number of assets',tickfont = list(size = 14)), barmode = 'stack')
fig

Is there an easier way to code this by using Rcolorbrewer instead of coding each color? and is it possible to group my stacked barplots Group1 (glider, auv, rov, usv), Group 2 (corer,towed_ew, seismic_eq, drill_rig) and Group 3 (radar, manned_sub, Other)?stack_plot

1
This is an open issue in plotly.jsismirsehregal

1 Answers

0
votes

You can try this approach by melting the data:

library(dplyr)
library(plotly)
library(tidyr)
library(RColorBrewer)

#Data
data <- structure(list(Nation = c("Belgium", "Bulgaria", "Croatia", "Cyprus", 
"Estonia", "Finland", "France", "Germany", "Greece", "Ireland", 
"Italy", "Malta", "Netherlands", "Norway", "Poland", "Portugal", 
"Romania", "Slovenia", "Spain", "Sweden", "Turkey", "United Kingdom", 
"Unknown"), glider = c(0, 0, 0, 3, 0, 1, 11, 18, 1, 0, 10, 0, 
0, 17, 0, 0, 0, 0, 12, 0, 0, 0, 5), radar = c(0, 0, 2, 0, 0, 
0, 2, 3, 0, 0, 8, 2, 2, 3, 0, 3, 0, 1, 17, 2, 0, 0, 0), AUV = c(1, 
0, 1, 0, 0, 0, 3, 3, 0, 0, 3, 0, 0, 1, 0, 6, 0, 0, 2, 1, 0, 13, 
0), ROV = c(1, 0, 2, 0, 1, 0, 1, 4, 3, 2, 2, 0, 0, 3, 1, 6, 1, 
0, 1, 3, 0, 4, 0), USV = c(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, 
0, 0, 0, 4, 0, 0, 0, 0, 0, 1, 0), corer = c(3, 0, 0, 0, 0, 0, 
1, 0, 0, 0, 0, 0, 0, 1, 0, 2, 0, 0, 0, 0, 0, 11, 0), towed_eq = c(0, 
1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 3, 0, 1, 0, 0, 0, 0, 0, 4, 
0), Seismic_eq = c(0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 1, 0, 0, 1, 
0, 0, 0, 0, 2, 0, 0, 2, 0), Drill_rig = c(0, 0, 0, 0, 0, 0, 0, 
2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0), Manned_sub = c(0, 
1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 
0), Other = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 
0, 0, 0, 0, 0, 4, 0), clean = c(6, 2, 5, 3, 1, 1, 23, 36, 4, 
2, 28, 2, 2, 30, 1, 25, 1, 1, 34, 6, 2, 40, 5)), row.names = c(NA, 
-23L), class = "data.frame")

Now the code:

#First reshape
df2 <- pivot_longer(data,cols = -Nation)
#Plot
p <- plot_ly(df2, x = df2$Nation,
             y = df2$value,
             type = 'bar',
             name = df2$name,
             text = df2$value,
             color = df2$name,
             colors = brewer.pal(length(unique(df2$name)),
                                 "Paired"))%>%
  layout(barmode = 'stack',hoverlabel = list(bgcolor= 'white') ,bargap = 0.5) %>%
  layout(xaxis = list(categoryorder = 'array',
                      categoryarray = df2$Nation), showlegend = T)

The output:

enter image description here