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

R语言Plotly极坐标条形图绘制问题:扇区错位与重叠解决方案咨询

解决Plotly极坐标条形图扇区错位与重叠问题

问题背景

现有数据包含12个30度风向扇区(wd_bin:扇区1对应角度范围[345°,15°)),以及6个不同模型的RMSE误差数据。使用R语言Plotly绘制极坐标条形图时,手动偏移theta值会导致扇区中心错位,不偏移则不同模型的条形重叠无法区分,需要找到正确的绘制方案。

数据结构如下:

df = structure(list(wd_bin = 1:12, rmse_ECMWF_IFS = c(89767.9005797252, 
77060.9210593033, 111852.268032843, 152670.935980594, 102768.114990758, 
104574.763828456, 142908.620677173, 117196.292103453, 97968.4047139761, 
84403.5642855608, 77599.7202949927, 70221.4610570674), rmse_NCEP_GFS = c(92160.3446737604, 
93246.6181575282, 107430.36181795, 104064.365353276, 79286.2569167962, 
95307.4267874572, 125943.094911279, 119694.636758248, 97529.5690154962, 
92779.0273981859, 107080.355420468, 95205.2350172323), rmse_CMC_GEM = c(97166.3559262995, 
74079.6805235247, 112034.359918589, 117111.729031983, 103293.175499705, 
113470.634045241, 141253.170616171, 123826.807069301, 87811.5903359745, 
95044.6902165017, 96588.7222228164, 73569.7635134733), rmse_DWD_ICON_EU = c(95694.1998002827, 
82213.910016189, 104829.529988257, 94791.7128117606, 78108.6314225577, 
100245.831042235, 109899.590959006, 105967.584208201, 87034.0294675701, 
89984.610602994, 78236.0704860403, 71070.3487787029), rmse_UKMO_UM10 = c(102191.822613825, 
83667.238396791, 100421.170626833, 96679.5614344941, 85259.843115734, 
94912.8741106779, 89497.5421634884, 112387.40461982, 87017.2544200735, 
78097.479596542, 78495.8402191684, 78208.781042165), rmse_MM_EURO1K = c(116831.471945606, 
116541.866609538, 138325.786259944, 115684.862405508, 95371.3974246316, 
125678.675634561, 127372.49646739, 127030.89967303, 143380.644310244, 
114607.436290945, 95552.056675237, 113825.836739793)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -12L))

正确解决方案

核心思路:将宽格式数据转换为长格式,利用Plotly的offset参数实现同一扇区内多个条形的有序排列,而非手动修改theta值导致错位。同时确保风向扇区的中心角度与实际范围对应。

步骤1:加载依赖包并转换数据格式

library(tidyverse)
library(plotly)

# 将宽数据转为长格式,简化批量处理逻辑
df_long <- df %>%
  pivot_longer(cols = starts_with("rmse_"), 
               names_to = "model", 
               values_to = "rmse") %>%
  # 提取模型名称(去除前缀rmse_)
  mutate(model = str_remove(model, "rmse_")) %>%
  # 计算每个扇区的中心角度:扇区1对应中心0°,依次递增30°
  mutate(theta_center = (wd_bin - 1) * 30) %>%
  # 为每个模型分配偏移量:30°扇区均分6个模型,每个偏移5°
  group_by(model) %>%
  mutate(offset = (cur_group_id() - 1) * 5) %>%
  ungroup()

步骤2:绘制极坐标条形图

# 定义模型颜色映射
model_colors <- c(
  "ECMWF_IFS" = "rgba(31, 119, 180, 0.7)",
  "NCEP_GFS" = "rgba(255, 127, 14, 0.7)",
  "CMC_GEM" = "rgba(44, 160, 44, 0.7)",
  "DWD_ICON_EU" = "rgba(214, 39, 40, 0.7)",
  "UKMO_UM10" = "rgba(148, 103, 189, 0.7)",
  "MM_EURO1K" = "rgba(255, 127, 127, 0.7)"
)

fig <- plot_ly(df_long, 
               type = "barpolar",
               r = ~rmse,
               theta = ~theta_center,
               offset = ~offset,
               name = ~model,
               hoverinfo = "text",
               text = ~paste0(model, " Error: ", round(rmse, 2)),
               marker = list(color = ~model_colors[model])) %>%
  layout(
    polar = list(
      radialaxis = list(title = "RMSE Error"),
      angularaxis = list(
        tickvals = seq(0, 330, by = 30),
        ticktext = c("0°", "30°", "60°", "90°", "120°", "150°", "180°", "210°", "240°", "270°", "300°", "330°"),
        direction = "clockwise",
        rotation = 90, # 让0°(正北)位于极坐标顶部,符合风向图展示习惯
        tickmode = "array"
      )
    ),
    legend = list(orientation = "h", x = 0.5, y = -0.1)
  )

fig

方案说明

  • 长格式数据转换:避免重复编写添加trace的代码,同时便于统一管理模型的偏移量和颜色配置。
  • offset参数使用:每个模型在同一扇区内偏移5°,确保所有条形都落在对应30°扇区范围内,不会出现扇区错位。
  • 角度校准:扇区1的中心角度设为0°,匹配[345°,15°)的实际范围,配合rotation=90让正北方向位于极坐标顶部,符合气象风向图的常规展示逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 11:34:58