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

R语言ggplot使用pivot_longer透视后数据绘图报错找不到area如何解决

报错原因

你在执行pivot_longer长表转换时,错误将area列纳入了被透视的列范围,导致转换后的fire_cat2不存在独立的area字段,ggplot映射y轴时找不到该对象因此抛出报错。

从你提供的数据集结构可以看到,fire_cat的列索引对应关系为:

  1. X
  2. Y
  3. month
  4. day
  5. FFMC
  6. DMC
  7. DC
  8. ISI
  9. temp
  10. RH
  11. wind
  12. rain
  13. area

你原代码里写的cols = colnames(fire_cat)[5:13]将索引为13的area列也包含进了透视列,原本需要保留为独立列的area被合并到了variable字段中,自然无法被调用。

修正方案

仅需要调整pivot_longer的列范围,排除area即可,两种写法可选:

写法1:调整索引范围

fire_cat2 <- fire_cat %>% 
  pivot_longer(
    cols = colnames(fire_cat)[5:12],
    names_to = 'variable',
    values_to = 'value'
  )

写法2:直接排除不需要透视的列(更稳妥,避免数错索引)

fire_cat2 <- fire_cat %>% 
  pivot_longer(
    cols = -c(X, Y, month, day, area),
    names_to = 'variable',
    values_to = 'value'
  )

修正后再运行你原来的ggplot绘图代码即可正常出图。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 03:27:02