Black Lives Matter. Please consider donating to Black Girls Code today.
Learn how to use COVID-19 data in open source Dash apps. Register for the Sept 23rd webinar with IQT!

Error when add_trace histogram type

Hello guys, Good afternoon.

I think we have a bug when try to create a Histogram trace intoa scatter plot.
in the older versions I was able to create without put one of the axis (x for horizontal and y for vertical).
with this new version I need to put both axis for any trace and that is a problem because for Histagram is only one or another.

in my case I just need to declare the x because I want my histogram in horizontal but I receive the follow error:

Warning: Error in : Variables must be length 1 or 827.
Problem variables: ‘y’

Can you help me please?

I’m using:
Windows 7 64bits
R: 3.3.3
Plotly 4.6.0

tks

Hi @fepshampshire, can you share your code?

yes, sure:

Tkss!

the error is exactly in this part:
PS: I just tried to put a “mode” as well but without success.

add_trace(
  x        = DATA,
  type     = "histogram",
  name     = "",
  text     = "",
  histnorm = "probability",
  marker   = list(color = 'salmon'),
  yaxis    = 'y2',
  showlegend = FALSE,
  xaxis = "x1",
  yaxis = "y2",
  hoverinfo = "x+y+text",
  xbins =
    list(
      start = DATA.min,
      end = DATA.max,
      size = (DATA.max - DATA.min)/( 1.5* sqrt(length(DATA)) ) )
) %>%

the entire plot_ly is this:

p<-plot_ly(
x = X,
y = Y,
type = “scatter”,
mode = “markers”,
marker =
list( symbol = c(“star” ,“diamond”),
size = c(15 ,12),
color = c(“darkmagenta” ,“red”)),
opacity = c(1 ,1),
text = c(“mean” ,“last”),
hoverinfo = “x+text”,
showlegend = FALSE,
xaxis = “x1”,
yaxis = “y1”
) %>%
add_trace(
x = rep(DATA.q05,3),
y=seq(-DATA.q05_h,DATA.q05_h,length.out = 3),
text = “q05%”,
line = list(color = “black”),
marker = list(opacity = 0),
hoverinfo = “x+text” ,
showlegend = FALSE,
xaxis = “x1”,
yaxis = “y1”
)%>%
add_trace(
x = rep(DATA.q25,3),
y=seq(-DATA.q25_h,DATA.q25_h,1),
text = “q25%”,
line = list(color = “black”),
marker = list(opacity = 0),
hoverinfo = “x+text” ,
showlegend = FALSE,
xaxis = “x1”,
yaxis = “y1”
) %>%
add_trace(
x = rep(DATA.median,10),
y=seq(-DATA.median_h,DATA.median_h,length.out = 10),
text = “median”,
line = list(color = “darkred”, dash = “dot”),
marker = list(opacity = 0),
hoverinfo = “x+text” ,
showlegend = FALSE,
xaxis = “x1”,
yaxis = “y1”
) %>%
add_trace(
x = rep(DATA.q75,3),
y=seq(-DATA.q75_h,DATA.q75_h,1),
text = “q75%”,
line = list(color = “black”),
marker = list(opacity = 0),
hoverinfo = “x+text” ,
showlegend = FALSE,
xaxis = “x1”,
yaxis = “y1”
) %>%
add_trace(
x = rep(DATA.q95,3),
y=seq(-DATA.q95_h,DATA.q95_h,length.out = 3),
text = “q95%”,
line = list(color = “black”),
marker = list(opacity = 0),
hoverinfo = “x+text” ,
showlegend = FALSE,
xaxis = “x1”,
yaxis = “y1”
) %>%
add_trace(
x = rep(DATA.min,10),
y = seq(-0.5DATA.min_h,DATA.min_h,length.out = 10),
text = “min”,
line = list(color = “darkblue”, dash = “dot”),
marker = list(opacity = 0),
hoverinfo = “x+text” ,
showlegend = FALSE,
xaxis = “x1”,
yaxis = “y1”
) %>%
add_trace(
x = rep(DATA.max,10),
y = seq(-0.5
DATA.max_h,DATA.max_h,length.out = 10),
text = “max”,
line = list(color = “darkblue”, dash = “dot”),
marker = list(opacity = 0),
hoverinfo = “x+text” ,
showlegend = FALSE,
xaxis = “x1”,
yaxis = “y1”
) %>%
add_trace(
x = DATA,
type = “histogram”,
name = “”,
text = “”,
histnorm = “probability”,
marker = list(color = ‘salmon’),
yaxis = ‘y2’,
showlegend = FALSE,
xaxis = “x1”,
yaxis = “y2”,
hoverinfo = “x+y+text”,
xbins =
list(
start = DATA.min,
end = DATA.max,
size = (DATA.max - DATA.min)/( 1.5* sqrt(length(DATA)) ) )
) %>%
layout(
bargap = 0.15,
yaxis=
list(
anchor = ‘x1’,
domain = c(0, 0.4),
title = “”,
zeroline = TRUE ,
showline = FALSE,
showticklabels = FALSE,
showgrid = FALSE
),
yaxis2=
list(
anchor = ‘x1’,
domain = c(0.45, 1.0),
title = “”,
zeroline = FALSE ,
showline = FALSE,
showticklabels = FALSE,
showgrid = FALSE
),
margin = m,
xaxis=
list(
title = “”,
zeroline = FALSE,
showline = TRUE,
tickwidth = 2,
tickmode = “auto”,
nticks = 8
),
shapes = list(
list(
type = ‘rectangle’,
xref = ‘x1’, x0 = DATA.q25, x1 = DATA.q75,
yref = ‘y1’, y0 = -0.8DATA.q25_h , y1 = .8DATA.q25_h ,
fillcolor = ‘rgb(50, 20, 90)’,
line = list(color = ‘rgb(50, 20, 90)’),
opacity = .2
)
),
annotations = my_annotations
)

Try adding inherit=FALSE to each add_trace() layer

Now its working!!! Tks Carson!!!
tks so much.

cheers.