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

在R语言Plotly中无法在箱线图上叠加散点图的问题求助

问题:Plotly手动指定分位数的箱线图叠加散点时报错lower_fence未找到

Error in eval(expr, data, expr_env) : object 'lower_fence' not found

我想用Plotly在手动指定分位数的箱线图上叠加散点数据,希望散点和箱线图用相同配色,但不管加不加配色都会报上述错误。本质上我只是想添加Plotly自动计算分位数时会生成的异常值点,请问该怎么解决?

用户提供的报错代码:

library(plotly)

# create presomputed quantiles
stats_df <- data.frame(
  site = c("site 2", "site 1", "site 3", "site 4"),
  site_type = c("b_type", "b_type", "a_type", "c_type"),
  lower_fence = c(1, 1.5, 2,  2.5),
  q1 = c(2, 3, 4, 5),
  median = c(6, 6.5, 7, 7.5),
  q3 = c(7, 8, 9, 10),
  upper_fence = c(11, 12, 13, 14)
)

# create box plot
fig <- plot_ly(
  data = stats_df,
  x = ~factor(site),
  color = ~ factor(site_type),
  colors = c("purple", "yellow", "red"),
  type="box",
  lowerfence = ~ lower_fence,
  q1 = ~ q1,
  median = ~ median,
  q3 = ~ q3,
  upperfence = ~ upper_fence)


# create scatter data
filtered_df_sub <- stats_df <- data.frame(
  site = c("site 2", "site 1", "site 3", "site 4"),
  site_type = c("b_type", "b_type", "a_type", "c_type"),
  value = c(1,2,3,4)
)

# add scatter points
fig <- fig %>% 
  add_trace(
    data = filtered_df_sub,
    x = ~factor(site),  # Use the same x variable for consistency
    y = ~value,  # Original values for scatter points
    color = ~ factor(site_type),
    colors = c("purple", "yellow", "red"),
    type = "scatter",
    mode = "markers",
    showlegend = FALSE  # Hide legend for scatter points if desired
  )

# add axis labels
fig <- fig %>%
        layout(title = "Box Plot with Precomputed Statistics",
               yaxis = list(title = "Value"),
               xaxis = list(title = "Sites"))

fig

解决方案

报错的核心原因是你在创建散点数据时错误覆盖了原始的stats_df数据框:

filtered_df_sub <- stats_df <- data.frame(...)

这行链式赋值把原本包含lower_fence、q1等统计列的stats_df,重新替换成了只有site、site_type、value的新数据框,导致后续Plotly渲染箱线图时找不到所需的统计字段。

修复步骤

  1. 保留原始统计数据:创建散点数据时仅赋值给filtered_df_sub,不要修改stats_df。
  2. 模拟真实异常值:散点的value应该设置为超出lower_fence或upper_fence的数值,符合Plotly自动生成异常值的逻辑。
  3. 统一配色:散点复用箱线图的color映射规则,自动继承相同配色,无需重复指定colors参数(指定也需保持一致)。

修复后的完整代码

library(plotly)

# 创建预计算的分位数数据
stats_df <- data.frame(
  site = c("site 2", "site 1", "site 3", "site 4"),
  site_type = c("b_type", "b_type", "a_type", "c_type"),
  lower_fence = c(1, 1.5, 2,  2.5),
  q1 = c(2, 3, 4, 5),
  median = c(6, 6.5, 7, 7.5),
  q3 = c(7, 8, 9, 10),
  upper_fence = c(11, 12, 13, 14)
)

# 创建箱线图
fig <- plot_ly(
  data = stats_df,
  x = ~factor(site),
  color = ~factor(site_type),
  colors = c("purple", "yellow", "red"),
  type = "box",
  lowerfence = ~lower_fence,
  q1 = ~q1,
  median = ~median,
  q3 = ~q3,
  upperfence = ~upper_fence
)

# 创建散点数据(模拟异常值,不覆盖stats_df)
filtered_df_sub <- data.frame(
  site = c("site 2", "site 1", "site 3", "site 4"),
  site_type = c("b_type", "b_type", "a_type", "c_type"),
  value = c(0.5, 1.2, 1.8, 14.5)  # 超出上下栅栏的数值,模拟真实异常值
)

# 添加散点
fig <- fig %>% 
  add_trace(
    data = filtered_df_sub,
    x = ~factor(site),
    y = ~value,
    color = ~factor(site_type),
    type = "scatter",
    mode = "markers",
    showlegend = FALSE
  )

# 设置布局
fig <- fig %>%
  layout(title = "带预计算统计量的箱线图(叠加异常值散点)",
         yaxis = list(title = "数值"),
         xaxis = list(title = "站点"))

fig

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 13:33:12