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

如何在R中批量绘制每年独立的负荷持续曲线(LDC)

问题

我有一个超30万行、覆盖20余年的数据集,需要为每年绘制负荷持续曲线(Load Duration Curve,LDC),展示全年每小时的MW使用量(平年8760小时,闰年8784小时)。

当前做法是按年份筛选生成新数据框,再按MW降序排序,添加占位列作为X轴绘图,但效率极低且难以维护;同时因为图表尺寸要求,不想使用facet_wrap()函数。


示例数据

其中hrxhr为当年的累计小时数:

YEARMONTHDAYHOUR OF DAYMWMonth_numDateDate1hrxhr
2023Dec31222416122023-12-313658758
2023Dec31232412122023-12-313658759
2023Dec31242400122023-12-313658760
2024Jan0112271122024-01-0111
2023Jan0122264122024-01-0112

现有实现代码

### ------------ Load in source ------------ ###
dummy_file <- 'Dummydata.csv'
forecast_df <- read_csv(dummy_file)

### ---- Order df by MW (load) and YEAR ---- ###
ordered_df <- forecast_df[order(forecast_df$MW, decreasing = TRUE), ]
ordered_df <- ordered_df[order(ordered_df$YEAR, decreasing = FALSE), ]

### -------------- Playground -------------- ###
## Create a dataframe for the forecast for calendar year 2023
cy23_df <- ordered_df[ordered_df$YEAR == 2023,]

## Add placeholder column for graphing purposes (add order number)
cy23_df$placeholder <- row.names(cy23_df)
## Check df structure and change columns as needed
str(cy23_df)
# Change placeholder column from character to numeric for graphing purposes
cy23_df$placeholder <- as.numeric(cy23_df$placeholder)
# Check if changed correctly
class(cy23_df$placeholder) #YES

## Load duration curve - Interactive
LF_cy23_LDC <- plot_ly(cy23_df, 
                       x= ~placeholder, 
                       y= ~MW, 
                       type= 'scatter', 
                       mode = 'lines',
                       hoverinfo = 'text',
                       text = paste("Megawatts: ", cy23_df$MW,
                                    "Date: ", cy23_df$MONTH, cy23_df$DAY,
                                    "Hour: ", cy23_df$hrxhr)) %>% 
  layout(title = 'CY2023 Load Forecast - LDC')
# "Hour: ", orderby_MW$yrhour)) 

saveWidget(LF_cy23_LDC, "cy23_LDC.html")

当前输出

2023年的图表Y轴为MW使用量,X轴为占位列;目前需要重复修改代码中的年份,才能生成其他年份的图表,我是R新手,求高效的优化方案。


解决方案

1. 数据预处理优化

用dplyr按年份分组处理,一次性完成排序和占位列生成,避免重复筛选:

library(dplyr)
library(plotly)
library(htmlwidgets)

# 读取数据
forecast_df <- read_csv('Dummydata.csv')

# 按年份分组,每组内按MW降序排序,生成占位列(排序后的行号)
processed_df <- forecast_df %>%
  group_by(YEAR) %>%
  arrange(desc(MW), .by_group = TRUE) %>%
  mutate(placeholder = row_number()) %>%
  ungroup()

2. 批量生成图表

通过循环遍历所有年份,自动生成并保存对应图表:

# 获取所有唯一年份
years <- unique(processed_df$YEAR)

# 遍历生成每个年份的LDC图表
for (year in years) {
  # 筛选当前年份数据
  year_data <- filter(processed_df, YEAR == year)
  
  # 绘制交互式LDC
  ldc_plot <- plot_ly(year_data,
                      x = ~placeholder,
                      y = ~MW,
                      type = 'scatter',
                      mode = 'lines',
                      hoverinfo = 'text',
                      text = paste("Megawatts: ", year_data$MW,
                                   "<br>Date: ", year_data$MONTH, " ", year_data$DAY,
                                   "<br>Hour of Year: ", year_data$hrxhr)) %>%
    layout(title = paste0('CY', year, ' Load Forecast - LDC'),
           xaxis = list(title = 'Hour Rank (Descending MW)'),
           yaxis = list(title = 'MW Usage'))
  
  # 保存为HTML文件
  saveWidget(ldc_plot, paste0("cy", year, "_LDC.html"))
}

优化说明

  • 用dplyr分组操作替代手动筛选,代码更简洁,处理大数据集效率更高
  • 循环自动处理所有年份,无需重复修改代码
  • 直接用row_number()生成占位列,省去行名转数值的繁琐步骤
  • 悬停文本用<br>换行,提升可读性
  • 给X/Y轴添加明确标题,图表信息更完整

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 00:01:10