使用plotly分组和堆叠的条形图

时间:2020-08-19 13:20:02

标签: r plotly bar-chart

我是R的新手,对R不太好。我试图做堆栈图,最后得到了一个非常麻烦的代码,我相信可以使用RColorbrewer和ggplot2简化堆叠的条形图,从而简化了工作,但是我不确定该怎么做。 以下是我使用的数据,它们位于名为data2

的data.frame中

           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

这是我使用的代码

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

是否有更简单的方法通过使用Rcolorbrewer而不是每种颜色进行编码?是否可以将堆叠的条形图分组为第1组(滑翔机,AUV,rov,usv),第2组(CORer,towed_ew,地震_eq,drill_rig)和第3组(雷达,manned_sub,其他)?stack_plot

1 个答案:

答案 0 :(得分:0)

您可以通过融合数据来尝试这种方法:

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

现在输入代码:

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

输出:

enter image description here