Closed
Description
Hello,
I would like to create a plotly-chart with annotated subplots. Unfortunately, however, the annotations in the "CombinedPlot" below are misaligned. Any help on properly placing annotations in the subplots would be greatly appreciated.
rm(list=ls())
library(dplyr)
library(plotly)
# Dataset containing the multiple of Category X and its peers
Category <- c("AAA", "AAA", "AAA", "BBB", "BBB", "BBB")
Y1 <- c(22, 19, 18, 20, 14, 12)
Y2 <- c(1.5, 1.3, 1, 2.3, 1.5, 0.9)
Y3 <- c(12, 9, 8, 10, 7, 5)
df <- data.frame(Category=Category, Y1=Y1, Y2=Y2, Y3=Y3)
df <- df %>% group_by(Category)
# Summarize the data
SY1 <- df %>% summarise(Min=min(Y1), Median=median(Y1), Max=max(Y1))
SY2 <- df %>% summarise(Min=min(Y2), Median=median(Y2), Max=max(Y2))
SY3 <- df %>% summarise(Min=min(Y3), Median=median(Y3), Max=max(Y3))
### Plotly chart
# Y1-Chart
Y1_Chart <- plot_ly(data=SY1) %>%
add_trace(x = ~Min, y = ~Category, type = 'bar', name = 'Minimum',
marker = list(color = 'rgba(1,1,1, 0.0)', line = list(width = 0))) %>%
add_trace(x = ~(Max-Min), y=~Category, name = 'Multiple-Range', type='bar',
marker = list(color = 'rgba(230, 159, 0, 0.2)', line = list(width = 0))) %>%
layout(barmode = 'stack',
xaxis = list(title = "Y1-Chart", zeroline = FALSE),
yaxis = list(title = "", type = 'category'), showlegend = FALSE) %>%
add_trace(x = ~Median, y = ~Category, type = 'scatter', mode = 'markers',
name = 'Median', marker=list(size=15, symbol="diamond"), color=I('black')) %>%
add_annotations(x = ~Median,
y = ~Category,
text = paste(" ", SY1$Median, sep=""),
xref = "x1",
yref = "y1",
showarrow = FALSE,
xanchor = "left",
font = list(size = 16))
Y1_Chart
# Y2-Chart
Y2_Chart <- plot_ly(data=SY2) %>%
add_trace(x = ~Min, y = ~Category, type = 'bar', name = 'Minimum',
marker = list(color = 'rgba(1,1,1, 0.0)', line = list(width = 0))) %>%
add_trace(x = ~(Max-Min), y=~Category, name = 'Multiple-Range', type='bar',
marker = list(color = 'rgba(230, 159, 0, 0.2)', line = list(width = 0))) %>%
layout(barmode = 'stack',
xaxis = list(title = "Y2-Chart", zeroline = FALSE),
yaxis = list(title = "", type = 'category'), showlegend = FALSE) %>%
add_trace(x = ~Median, y = ~Category, type = 'scatter', mode = 'markers',
name = 'Median', marker=list(size=15, symbol="diamond"), color=I('black')) %>%
add_annotations(x = ~Median,
y = ~Category,
text = paste(" ", SY2$Median, sep=""),
xref = "x2",
yref = "y2",
showarrow = FALSE,
xanchor = "left",
font = list(size = 16))
Y2_Chart
# Y3-Chart
Y3_Chart <- plot_ly(data=SY3) %>%
add_trace(x = ~Min, y = ~Category, type = 'bar', name = 'Minimum',
marker = list(color = 'rgba(1,1,1, 0.0)', line = list(width = 0))) %>%
add_trace(x = ~(Max-Min), y=~Category, name = 'Multiple-Range', type='bar',
marker = list(color = 'rgba(230, 159, 0, 0.2)', line = list(width = 0))) %>%
layout(barmode = 'stack',
xaxis = list(title = "Y3-Chart", zeroline = FALSE),
yaxis = list(title = "", type = 'category'), showlegend = FALSE) %>%
add_trace(x = ~Median, y = ~Category, type = 'scatter', mode = 'markers',
name = 'Median', marker=list(size=15, symbol="diamond"), color=I('black')) %>%
add_annotations(x = ~Median,
y = ~Category,
text = paste(" ", SY3$Median, sep=""),
xref = "x3",
yref = "y3",
showarrow = FALSE,
xanchor = "left",
font = list(size = 16))
Y3_Chart
# Combine the three plots
CombinedPlot <- subplot(Y1_Chart, Y2_Chart, Y3_Chart,
nrows=2, shareX = FALSE, shareY = FALSE, titleX = TRUE, titleY = FALSE, margin = 0.1)
CombinedPlot
Metadata
Metadata
Assignees
Labels
No labels