简体   繁体   English

plot如何在R中分多张图

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

I have a dataset of over 300K rows and over 20 years.我有一个超过 300K 行和超过 20 年的数据集。 I'm trying to create a Load Duration Curve for every year for XX years (so # of MW used every hour of the year (8760 hours for every year or 8784 for leap year). Currently I make a new dataframe by filtering by year and then reordering by descending order of MW used (descending order for the curve) and then create another column to match the row order so that I can use that column as a placeholder for the x-axis. Seems pretty inefficient and could be difficult to update if needed (see playground for what I've been doing). I also don't want to use facet_wrap() because the graphs are too small for what is needed.我正在尝试为 XX 年的每一年创建一个负载持续时间曲线(因此一年中每小时使用的 MW 数量(每年 8760 小时或闰年 8784 小时)。目前我通过按年份过滤制作了一个新的 dataframe然后按使用的 MW 的降序重新排序(曲线的降序),然后创建另一列以匹配行顺序,以便我可以将该列用作 x 轴的占位符。看起来效率很低并且可能很难如果需要更新(请参阅游乐场了解我一直在做的事情)。我也不想使用 facet_wrap() 因为图表太小而无法满足需要。

Dummy_file: Where hrxhr is the running total of hours in a given year. Dummy_file:其中 hrxhr 是给定年份的运行总小时数。

YEAR MONTH DAY HOUR OF DAY一天中的时间 MW兆瓦 Month_num Month_num Date日期 Date1日期 1 hrxhr呵呵
2023 2023年 Dec十二月 31 31 22 22 2416 2416 12 12 2023-12-31 2023-12-31 365 365 8758 8758
2023 2023年 Dec十二月 31 31 23 23 2412 2412 12 12 2023-12-31 2023-12-31 365 365 8759 8759
2023 2023年 Dec十二月 31 31 24 24 2400 2400 12 12 2023-12-31 2023-12-31 365 365 8760 8760
2024 2024年 Jan 01 01 1 1个 2271 2271 12 12 2024-01-01 2024-01-01 1 1个 1 1个
2023 2023年 Jan 01 01 2 2个 2264 2264 12 12 2024-01-01 2024-01-01 1 1个 2 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")

Current Output for CY2023: Yaxis Megawatts used (MW) and Xaxis is a placeholder (placeholder) and then I just repeat the playground code for the rest of the years, but change 2023 to 2024, then 2025, etc. CY2023 的当前 Output:Yaxis Megawatts used (MW) 和 Xaxis 是一个占位符(placeholder)然后我只是重复 playground code for the years rest,但将 2023 更改为 2024,然后 2025,等等。

Sorry if this is a long post, tmi, or not enough information.对不起,如果这是一篇很长的帖子,tmi,或者信息不够。 I'm fairly new to R and this community.我对 R 和这个社区还很陌生。 Many thanks for your help!非常感谢您的帮助!

Simply generalize your playground process in a user-defined method, then iterate through years with lapply .只需在用户定义的方法中概括您的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)

Consider even by (object-oriented wrapper to tapply and roughly equivalent to split + lapply ) and avoid the year indexing.考虑甚至by (面向对象的包装器来tapply并且大致相当于split + lapply )并避免年份索引。 Notice input parameter changes below and variables used in title and filename:请注意下面的输入参数更改以及标题和文件名中使用的变量:

# 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)

Counterparts in tidyverse would be purrr.map : 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