簡體   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