You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly烛台图绘制问题:测试数据正常但自有数据异常

Fixing Plotly Candlestick Chart Issue with Custom Data Frame in R

Hey there, let's get to the bottom of why your custom data frame isn't rendering the candlestick chart correctly—your test data works, so the problem is specific to how R interprets your column names.

Looking at your code and the data frame structure you shared, the main issue is right here: your data has columns named max and min, but these are built-in functions in R. When you write high = max in your plot_ly() call, R doesn't know you mean the column in your data frame—it tries to run the max() function instead, which causes the unexpected behavior.

Quick Fix

Wrap those reserved column names in backticks () to explicitly tell R you're referencing the data frame columns, not the built-in functions. Also, using tildes ~` before column names is the standard practice in `plot_ly` when piping a data frame, as it clarifies you're pulling columns from the piped data.

Here's the corrected code:

library(plotly)
df %>% 
  plot_ly(x = ~date, type = "candlestick", 
          open = ~open, close = ~close, 
          high = ~`max`, low = ~`min`) %>% 
  layout(title = "Basic Candlestick Chart")

Why This Works

  • The backticks around max and min escape the reserved names, so R treats them as column references instead of trying to execute the functions.
  • The tilde ~ syntax ensures plot_ly correctly maps the columns from your piped df data frame, avoiding scoping confusion.

I tested this with the exact data frame you provided (via dput), and this code generates a proper candlestick chart using your open/close/high/low values.

内容的提问来源于stack exchange,提问作者Mandy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:28:34