Reputation: 113
I am trying to add a subplot grouped legend to series of GROUPED bar charts in plotly. I've found numerous examples of subplot grouped legends for charts generally (like the last graphical example here: https://plot.ly/r/legend/), but I cannot get the "legendgroup = ~" method to work for GROUPED bar charts.
I have survey data from two different years (2017 and 2019) for a number of operating units at my company. I want to show the 2017 and 2019 survey results side-by-side for each operating unit individually in a grouped bar format with a chart for each operating unit. The only element that is not working is the legend for the survey year (2017 or 2019) which I want to work across all the graphs.
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
The way this code is written, the legend is linked to the first plot only and the second plot's legend is hidden. Could someone please help?
Upvotes: 0
Views: 483
Reputation: 1044
You can set the legendgroup
manually, like the names of traces that you did.
Just put, legendgroup = "2017"
or legendgroup = "2019"
in add_trace
and it will work fine.
Here is yours code with this little alteration:
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
Here the output:
All
Only 2017:
Only 2019:
Upvotes: 1