我的数据框有 12 个扇区的 6 列数据:
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))
其中
wd_bin
是我的30度宽度的扇区。即扇区 1 是 [345,15)
.
在
plotly
中使用R
我想创建一个极坐标条形图并在一个图中显示所有数据:
以下方法在某种程度上有效,但它弄乱了垃圾箱中心,我无法修复它:
theta_positions <- (as.numeric(df$wd_bin) - 1) * 30
fig <- plot_ly()
fig <- fig %>%
add_trace(
type = 'barpolar',
r = df$rmse_ECMWF_IFS,
theta = theta_positions,
name = "ECMWF-IFS RMSE Error",
hoverinfo = 'text',
text = paste0("ECMWF-IFS Error: ", round(df$rmse_ECMWF_IFS, 2)),
marker = list(color = 'rgba(31, 119, 180, 0.7)')
)
# Add trace for NCEP-GFS
fig <- fig %>%
add_trace(
type = 'barpolar',
r = df$rmse_NCEP_GFS,
theta = theta_positions +3 , # Small offset to move the bars slightly right
name = "NCEP-GFS RMSE Error",
hoverinfo = 'text',
text = paste0("NCEP-GFS Error: ", round(df$rmse_NCEP_GFS, 2)),
marker = list(color = 'rgba(255, 127, 14, 0.7)')
)
# Add trace for CMC-GEM
fig <- fig %>%
add_trace(
type = 'barpolar',
r = df$rmse_CMC_GEM,
theta = theta_positions + 6, # Slightly more offset
name = "CMC-GEM RMSE Error",
hoverinfo = 'text',
text = paste0("CMC-GEM Error: ", round(df$rmse_CMC_GEM, 2)),
marker = list(color = 'rgba(44, 160, 44, 0.7)')
)
fig <- fig %>%
add_trace(
type = 'barpolar',
r = df$rmse_DWD_ICON_EU,
theta = theta_positions + 9, # Further offset
name = "DWD-ICON-EU RMSE Error",
hoverinfo = 'text',
text = paste0("DWD-ICON-EU Error: ", round(df$rmse_DWD_ICON_EU, 2)),
marker = list(color = 'rgba(214, 39, 40, 0.7)')
)
fig <- fig %>%
add_trace(
type = 'barpolar',
r = df$rmse_UKMO_UM10,
theta = theta_positions + 12, # More offset to separate the bars
name = "UKMO-UM10 RMSE Error",
hoverinfo = 'text',
text = paste0("UKMO-UM10 Error: ", round(df$rmse_UKMO_UM10, 2)),
marker = list(color = 'rgba(148, 103, 189, 0.7)')
)
fig <- fig %>%
add_trace(
type = 'barpolar',
r = df$rmse_MM_EURO1K,
theta = theta_positions + 15, # Further offset
name = "MM-EURO1K RMSE Error",
hoverinfo = 'text',
text = paste0("MM-EURO1K Error: ", round(df$rmse_MM_EURO1K, 2)),
marker = list(color = 'rgba(255, 127, 127, 0.7)')
)
fig <- fig %>%
layout(
polar = list(
angularaxis = list(
tickvals = seq(0, 330, by = 30),
ticktext = seq(0, 360, by = 30),
direction = "clockwise",
rotation = 90
)
)
)
fig
通过在每个步骤中添加手动移位,我创建了绘图,但它看起来不太好,因为箱中心发生了移位,而且如果我删除手动移位,所有内容都在彼此之上,并且缩放并不真正可见! 任何解决方案或建议将非常感激!
将数据修改为长格式通常更容易,这样就可以避免重复。
pivot_longer()
中的 tidyr
函数对于此目的很有用。然后,您可以创建一个 θ 值向量,使每个条形均匀分布。也许是这样的:
library(tidyr)
library(plotly)
# Pivot data so all variables in single column, values in another
df1 <- df |>
pivot_longer(-wd_bin)
# Create theta_positions column
df1$theta_positions <- seq(0, by = 360 / nrow(df1), length.out = nrow(df1))
# Define colour palette based on your values
custom_colours <- c(
"rmse_ECMWF_IFS" = rgb(31, 119, 180, maxColorValue = 255),
"rmse_NCEP_GFS" = rgb(255, 127, 14, maxColorValue = 255),
"rmse_CMC_GEM" = rgb(44, 160, 44, maxColorValue = 255),
"rmse_DWD_ICON_EU" = rgb(214, 39, 40, maxColorValue = 255),
"rmse_UKMO_UM10" = rgb(148, 103, 189, maxColorValue = 255),
"rmse_MM_EURO1K" = rgb(255, 127, 127, maxColorValue = 255)
)
plot_ly(df1,
type = "barpolar",
r = ~value,
theta = ~theta_positions,
text = ~paste(name, value),
hoverinfo = "text",
color = ~name,
colors = custom_colours,
alpha = 0.7
) |>
layout(
polar = list(
angularaxis = list(
tickvals = seq(0, 330, by = 30),
ticktext = seq(0, 360, by = 30),
rotation = 90,
direction = "clockwise"
),
radialaxis = list(showticklabels = TRUE,
ticksuffix = "")
),
showlegend = TRUE
)