6

I want to plot a bar together with a line chart in R with plotly.

My first attempt was

p <- plot_ly(
  x = c(1,2,3,4,5),
  y = c(1,2,1.5,3,2),
  type='scatter',
  mode='lines',
  line = list(color = 'black')
)
add_trace(
  p,
  x = c(1,2,3,4,5),
  y = c(0.5,0.7,0.6,0.9,0.8),
  type='bar',
  marker = list(color = 'red')
)

The result is right, but I get the following warning:

Warning message: The following attributes don't exist: 'mode', 'line'

I guess cause the bar plot in add_trace() cannot handle the line and mode parameter from the plot_ly() function. So I changed the order:

p <- plot_ly(
  x = c(1,2,3,4,5),
  y = c(0.5,0.7,0.6,0.9,0.8),
  type='bar',
  marker = list(color = 'red')
)
add_trace(
  p,
  x = c(1,2,3,4,5),
  y = c(1,2,1.5,3,2),
  type='scatter',
  mode='lines',
  line = list(color = 'black')
)

This time I get the following message and red markers are displayed on the black line chart.

A marker object has been specified, but markers is not in the mode Adding markers to the mode...

How can I fix this? (I'm using the R package plotly 4.1.0)

elcombato
  • 473
  • 1
  • 4
  • 16
  • First, `plotly v4.1.0` doesn't exist, the most recent version on CRAN is `v3.6.0`. Second, I ran the first code block above and did not get any errors at all. Try updating plotly? – Matt Sandgren Aug 02 '16 at 15:01
  • 1
    `plotly 4.1.0` exists [indeed](https://github.com/ropensci/plotly/blob/master/NEWS.md). It is the most current version on [GitHub](https://github.com/ropensci/plotly). It seems as it is a problem of this new version. But before opening an issue on GitHub I wanted to ask, if I'm doing anything wrong. – elcombato Aug 02 '16 at 15:47
  • I stand corrected; I only glanced at the releases tab on Github. Since it worked for me on 3.6.0, it may be a small bug then – Matt Sandgren Aug 02 '16 at 15:50
  • Reproducable for me. Things get even worse when using barmode = 'relative' or combining plot with subplots. These issues don't seem to be working in 3.6.0 neither. Just filed an issue (#667) on github. – Fabian Gehring Aug 02 '16 at 17:39

1 Answers1

7

I'm running plotly 4.0.1, but if I add mode='lines+markers' instead of just mode='lines' the error message goes away for me.

--edit to add full code--

For the lazy (like me), here's the full code that worked on my end:

p <- plot_ly(x = c(1,2,3,4,5),
             y = c(0.5,0.7,0.6,0.9,0.8),
             type='bar',
             marker = list(color = 'red', opacity=0)
     )

add_trace(p,
          x = c(1,2,3,4,5),
          y = c(1,2,1.5,3,2),
          type='scatter',
          mode='lines+markers',
          line = list(color = 'black')
     )