简体   繁体   中英

plotly bar and line chart

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 )

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')
     )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM