简体   繁体   English

情节酒吧和折线图

[英]plotly bar and line chart

I want to plot a bar together with a line chart in R with plotly. 我想用曲线绘制一个条形图和R中的折线图。

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' 警告消息:以下属性不存在:'mode','line'

I guess cause the bar plot in add_trace() cannot handle the line and mode parameter from the plot_ly() function. 我猜因为add_trace()中的add_trace()无法处理plot_ly()函数中的linemode参数。 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 ) (我正在使用R封装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. 我正在运行4.0.2,但是如果我添加mode='lines+markers'而不仅仅是mode='lines'那么错误信息就会消失。

--edit to add full code-- --edit添加完整代码 -

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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