繁体   English   中英

plot如何在R中分多张图

[英]How to plot multiple, separate graphs in R

我有一个超过 300K 行和超过 20 年的数据集。 我正在尝试为 XX 年的每一年创建一个负载持续时间曲线(因此一年中每小时使用的 MW 数量(每年 8760 小时或闰年 8784 小时)。目前我通过按年份过滤制作了一个新的 dataframe然后按使用的 MW 的降序重新排序(曲线的降序),然后创建另一列以匹配行顺序,以便我可以将该列用作 x 轴的占位符。看起来效率很低并且可能很难如果需要更新(请参阅游乐场了解我一直在做的事情)。我也不想使用 facet_wrap() 因为图表太小而无法满足需要。

Dummy_file:其中 hrxhr 是给定年份的运行总小时数。

一天中的时间 兆瓦 Month_num 日期 日期 1 呵呵
2023年 十二月 31 22 2416 12 2023-12-31 365 8758
2023年 十二月 31 23 2412 12 2023-12-31 365 8759
2023年 十二月 31 24 2400 12 2023-12-31 365 8760
2024年 01 1个 2271 12 2024-01-01 1个 1个
2023年 01 2个 2264 12 2024-01-01 1个 2个
### ------------ 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")

CY2023 的当前 Output:Yaxis Megawatts used (MW) 和 Xaxis 是一个占位符(placeholder)然后我只是重复 playground code for the years rest,但将 2023 更改为 2024,然后 2025,等等。

对不起,如果这是一篇很长的帖子,tmi,或者信息不够。 我对 R 和这个社区还很陌生。 非常感谢您的帮助!

只需在用户定义的方法中概括您的playground过程,然后使用lapply迭代数年。

# USER DEFINED METHOD TO RUN A SINGLE YEAR
build_year_plot <- function(year) {
    ### -------------- Playground -------------- ### 
    ## Create a dataframe for the forecast for calendar year
    cy_df <- ordered_df[ordered_df$YEAR == year,] 

    ## Add placeholder column for graphing purposes (add order number) 
    cy_df$placeholder <- row.names(cy_df) 

    ## Check df structure and change columns as needed 
    str(cy_df)

    # Change placeholder column from character to numeric for graphing purposes 
    cy_df$placeholder <- as.numeric(cy_df$placeholder) 

    # Check if changed correctly 
    class(cy_df$placeholder) #YES 

    ## Load duration curve - Interactive 
    LF_cy_LDC <- plot_ly(
        cy_df, x = ~placeholder, y = ~MW, type= 'scatter', 
        mode = 'lines', hoverinfo = 'text', 
        text = paste(
            "Megawatts: ", cy_df$MW, 
            "Date: ", cy_df$MONTH, cy_df$DAY, 
            "Hour: ", cy_df$hrxhr
        )
    ) %>% layout( # USING BASE R 4.1.0+ PIPE
        title = paste0('CY', year, ' Load Forecast - LDC')
    )

    saveWidget(LF_cy_LDC, paste0("cy", year-2000, "_LDC.html"))

    return(LF_cy_LDC)
}

# CALLER TO RUN THROUGH SEVERAL YEARS
LF_cy_plots <- lapply(2023:2025, build_year_plot)

考虑甚至by (面向对象的包装器来tapply并且大致相当于split + lapply )并避免年份索引。 请注意下面的输入参数更改以及标题和文件名中使用的变量:

# USER DEFINED METHOD TO RUN A SINGLE DATA FRAME
build_year_plot <- function(cy_df) {
    ### -------------- Playground -------------- ### 
    ## Add placeholder column for graphing purposes (add order number) 
    cy_df$placeholder <- row.names(cy_df) 

    ...SAME AS ABOVE...

    ) %>% layout(
        title = paste0('CY', cy_df$YEAR[1], ' Load Forecast - LDC')
    )

    saveWidget(LF_cy_LDC, paste0("cy", cy_df$YEAR[1]-2000, "_LDC.html"))

    return(LF_cy_LDC)
}

# CALLER TO RUN THROUGH SEVERAL YEARS
LF_cy_plots <- by(ordered_df, ordered_df$YEAR, build_year_plot)

tidyverse 中的对应项是purrr.map

# METHOD RECEIVES YEAR (lapply counterpart)
LF_cy_plots <- purrr::map(2023:2025, build_year_plot)

# METHOD RECEIVES DATA FRAME (by counterpart)
LF_cy_plots <- ordered_year %>%
    split(.$YEAR) %>%
    purrr::map(build_year_plot)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM