Skip to content

R plotly: subplot annotations #1181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
IngoBrooks opened this issue Jan 18, 2018 · 4 comments
Closed

R plotly: subplot annotations #1181

IngoBrooks opened this issue Jan 18, 2018 · 4 comments

Comments

@IngoBrooks
Copy link

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
@vidaletal
Copy link

Hi,
Have you been able to fix it?
I'm facing the same problem with a different dataset

@Be-Ko
Copy link

Be-Ko commented Jul 19, 2018

i have the same problem:

plotly_annotation_fail

the top left red point is an overlapping of two data points, annotations are supposed to assign index-number, but the arrows obviously point to nowhere instead.

annotation for the bottom right red point is just slightly off, but it is off.

@cpsievert
Copy link
Collaborator

cpsievert commented Aug 31, 2018

With 4caa068, this now works, and you can remove the xref/yref arguments in add_annotations()

@cpsievert
Copy link
Collaborator

cpsievert commented Aug 31, 2018

For posterity, here is a more minimal example of the bug fix

p1 <- plot_ly(mtcars) %>%
    add_annotations(text = ~cyl, x = ~wt, y = ~mpg)
p2 <- plot_ly(mtcars) %>%
    add_annotations(text = ~am, x = ~wt, y = ~mpg)
subplot(p1, p2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants