我正试图在情节中的GROUPED条形图系列中添加子情节分组图例。我已经找到了许多图表的子图分组图例的示例(例如此处的最后一个图形示例:https://plot.ly/r/legend/),但是我无法使用“ legendgroup =〜”方法用于GROUPED条形图。
我有来自两个不同年份(2017年和2019年)的我公司多个运营部门的调查数据。我想以分组条形格式分别显示每个操作单元的2017年和2019年调查结果,以及每个操作单元的图表。唯一不起作用的元素是调查年度(2017或2019)的图例,我想在所有图表中使用该图例。
library(data.table)
library(plotly)
# Dummy data
data <- data.table(Group = rep(c("Business_Unit_1","Business_Unit_2"), each = 4),
Question = rep(c("Happy","Ethics","Happy", "Ethics"), each = 2),
Year = c("2017", "2019", "2017", "2019", "2017", "2019", "2017", "2019"),
Prop = c(.9, .95, .8, .75, .7, .8, .8, .97))
# Grouped bar chart 1
plot_1 <- plot_ly() %>%
add_trace(x = ~ data[Group == "Business_Unit_1" & Year == "2017", (Question)],
y = ~ data[Group == "Business_Unit_1" & Year == "2017", (Prop)],
name = "2017",
type = 'bar',
marker = list(color = 'rgb(158,202,225)',
line = list(color = 'rgb(8,48,107)', width = 1.5))) %>%
add_trace(x = ~ data[Group == "Business_Unit_1" & Year == "2019", (Question)],
y = ~ data[Group == "Business_Unit_1" & Year == "2019", (Prop)],
name = "2019",
type = 'bar',
marker = list(color = 'rgb(58,200,225)',
line = list(color = 'rgb(8,48,107)', width = 1.5))) %>%
layout(yaxis = list(title = 'Proportion'),
annotations = list(x = 0.5 , y = 1.05, text = "Business_Unit_1", showarrow = F, xref = 'paper', yref = 'paper'),
barmode = 'group')
# Grouped bar chart 2
# Right now I am just hiding the second legend
plot_2 <- plot_ly() %>%
add_trace(x = ~ data[Group == "Business_Unit_2" & Year == "2017", (Question)],
y = ~ data[Group == "Business_Unit_2" & Year == "2017", (Prop)],
name = "2017",
type = 'bar',
showlegend = FALSE,
marker = list(color = 'rgb(158,202,225)',
line = list(color = 'rgb(8,48,107)', width = 1.5))) %>%
add_trace(x = ~ data[Group == "Business_Unit_2" & Year == "2019", (Question)],
y = ~ data[Group == "Business_Unit_2" & Year == "2019", (Prop)],
name = "2019",
type = 'bar',
showlegend = FALSE,
marker = list(color = 'rgb(58,200,225)',
line = list(color = 'rgb(8,48,107)', width = 1.5))) %>%
layout(yaxis = list(title = 'Proportion'),
annotations = list(x = 0.5 , y = 1.05, text = "Business_Unit_2", showarrow = F, xref = 'paper', yref = 'paper'),
barmode = 'group')
# Create the subplot
plots <- subplot(plot_1, plot_2, shareY = TRUE, nrows = 1)
plots
此代码的编写方式,图例仅链接到第一个图,第二个图的图例被隐藏。有人可以帮忙吗?
答案 0 :(得分:0)
您可以手动设置legendgroup
,就像您所做的跟踪名称一样。
只需将legendgroup = "2017"
中的legendgroup = "2019"
或add_trace
放在library(data.table)
library(plotly)
# Dummy data
data <- data.table(Group = rep(c("Business_Unit_1","Business_Unit_2"), each = 4),
Question = rep(c("Happy","Ethics","Happy", "Ethics"), each = 2),
Year = c("2017", "2019", "2017", "2019", "2017", "2019", "2017", "2019"),
Prop = c(.9, .95, .8, .75, .7, .8, .8, .97))
# Grouped bar chart 1
plot_1 <- plot_ly() %>%
add_trace(x = ~ data[Group == "Business_Unit_1" & Year == "2017", (Question)],
y = ~ data[Group == "Business_Unit_1" & Year == "2017", (Prop)],
name = "2017",
type = 'bar',
legendgroup = "2017", ### Legend 2017
marker = list(color = 'rgb(158,202,225)',
line = list(color = 'rgb(8,48,107)', width = 1.5))) %>%
add_trace(x = ~ data[Group == "Business_Unit_1" & Year == "2019", (Question)],
y = ~ data[Group == "Business_Unit_1" & Year == "2019", (Prop)],
name = "2019",
type = 'bar',
legendgroup = "2019", ### Legend 2019
marker = list(color = 'rgb(58,200,225)',
line = list(color = 'rgb(8,48,107)', width = 1.5))) %>%
layout(yaxis = list(title = 'Proportion'),
annotations = list(x = 0.5 , y = 1.05, text = "Business_Unit_1", showarrow = F, xref = 'paper', yref = 'paper'),
barmode = 'group')
# Grouped bar chart 2
# Right now I am just hiding the second legend
plot_2 <- plot_ly() %>%
add_trace(x = ~ data[Group == "Business_Unit_2" & Year == "2017", (Question)],
y = ~ data[Group == "Business_Unit_2" & Year == "2017", (Prop)],
name = "2017",
type = 'bar',
legendgroup = "2017", ### Legend 2017
showlegend = FALSE,
marker = list(color = 'rgb(158,202,225)',
line = list(color = 'rgb(8,48,107)', width = 1.5))) %>%
add_trace(x = ~ data[Group == "Business_Unit_2" & Year == "2019", (Question)],
y = ~ data[Group == "Business_Unit_2" & Year == "2019", (Prop)],
name = "2019",
type = 'bar',
legendgroup = "2019", ### Legend 2019
showlegend = FALSE,
marker = list(color = 'rgb(58,200,225)',
line = list(color = 'rgb(8,48,107)', width = 1.5))) %>%
layout(yaxis = list(title = 'Proportion'),
annotations = list(x = 0.5 , y = 1.05, text = "Business_Unit_2", showarrow = F, xref = 'paper', yref = 'paper'),
barmode = 'group')
# Create the subplot
plots <- subplot(plot_1, plot_2, shareY = TRUE, nrows = 1)
plots
中,即可正常工作。
这是您的代码,做了一些小的改动:
print(Small_df['Date'].head())
0 2019-05-22 15:37:05
1 2019-05-22 15:40:25
2 2019-05-22 15:40:45
3 2019-05-22 15:40:45
4 2019-05-22 15:41:55
在这里输出:
全部
仅2017年:
仅在2019年: