簡體   English   中英

R 中 ggplot2 的 For 循環

[英]For Loop for ggplot2 in R

我有這個數據框,我想在一個循環中同時創建多個圖,但是當我運行代碼時它給了我一個錯誤。 誰能告訴我我做錯了什么!

數據:

structure(list(Date = structure(c(289094400, 297043200, 304992000, 
312854400, 320716800, 328665600), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), NORTH = c(4.06976744186047, 5.51675977653633, 7.2799470549305, 
4.75015422578655, 4.59363957597172, 3.15315315315317), YORKSANDTHEHUMBER = c(4.0121120363361, 
5.45851528384282, 9.52380952380951, 6.04914933837431, 3.03030303030299, 
5.42099192618225), NORTHWEST = c(6.57894736842105, 6.95256660168939, 
6.50060753341436, 5.5904164289789, 4.59211237169096, 4.70041322314051
), EASTMIDS = c(4.98489425981872, 8.20143884892085, 6.91489361702127, 
5.22388059701494, 5.61465721040189, 4.64465584778958), WESTMIDS = c(4.65838509316771, 
4.74777448071216, 8.66855524079319, 6.56934306569344, 3.22896281800389, 
3.17535545023698), EASTANGLIA = c(6.74525212835624, 8.58895705521476, 
8.47457627118643, 10.7291666666667, 4.8447789275635, 4.84522207267835
), OUTERSEAST = c(6.7110371602884, 7.53638253638255, 9.47317544707589, 
8.56512141280351, 3.82269215128102, 2.11515863689776), OUTERMET = c(4.54545454545458, 
6.58505698607005, 7.36633663366336, 7.08225746956843, 4.3747847054771, 
1.68316831683168), LONDON = c(8.11719500480309, 10.3065304309196, 
6.32299637535239, 7.65151515151515, 1.30190007037299, 2.1535255296978
), SOUTHWEST = c(6.17577197149644, 7.71812080536912, 7.63239875389407, 
9.45489628557649, 2.46804759806079, 2.19354838709679), WALES = c(6.09418282548476, 
8.35509138381203, 7.40963855421687, 7.01065619742007, 1.15303983228513, 
3.47150259067357), SCOTLAND = c(5.15222482435597, 4.12026726057908, 
5.40106951871658, 8.67579908675796, -0.280112044817908, 2.94943820224719
), NIRELAND = c(4.54545454545454, 4.94752623688156, 4.42857142857145, 
2.96397628818967, 6.06731620903454, 0.0835073068893502), UK = c(5.76890543055322, 
7.20302836425676, 7.39543442582184, 7.22885986848197, 3.23472252213347, 
2.95766398929048)), row.names = c(NA, -6L), class = c("tbl_df", 
"tbl", "data.frame"))

代碼:

for (i in 2:ncol(data2)) {  # Printing ggplot within for-loop
  print(ggplot(data2, aes(x = Date, y = data2[, i])) + # Basic ggplot2 plot of x & y's
          geom_line() + 
          labs(title = "Uk States", 
               y = "",
               x = "") + 
          theme_bw() +
          geom_hline(yintercept = 0))
  Sys.sleep(1)
}

錯誤:

Don't know how to automatically pick scale for object of type tbl_df/tbl/data.frame. Defaulting to continuous.
Error in is.finite(x) : default method not implemented for type 'list'

我建議循環列名而不是值。 然后,您可以使用.data作為 y 索引。

library(tidyverse)

for(i in names(data2)[-1]) {  # Printing ggplot within for-loop
  # Basic ggplot2 plot of x & y's
  print(ggplot(data2, aes(x = Date, y = .data[[i]])) + 
          geom_line()+ labs(title = "Uk States", 
                            y = "",
                            x = "")+ 
          theme_bw()+
          geom_hline(yintercept = 0))
  Sys.sleep(1)
}

您也可以嘗試facet_wrap將多個圖組合在一起。

data2 %>%
  pivot_longer(cols = -Date) %>%
  ggplot(aes(Date, value)) + 
  geom_line() + facet_wrap(~name) + 
  labs(title = "Uk States", x = "", y = "") +
  theme_bw() +
  geom_hline(yintercept = 0)

在此處輸入圖像描述

在循環中生成ggplot的另一種方法是使用lapply ,我們循環獲取colnames並使用aes_string作為美學映射。

在這里,結果保存到列表ggplot_list ,您可以在其中通過索引提取單個 plot (例如, NORTH的 plot 存儲在ggplot_list[[1]]中)

請注意,我已更改labs(title = i)以便 plot 標題將是您的列名。

library(ggplot2)

ggplot_list <- lapply(colnames(data2[-1]), \(i) {
  ggplot(data2, aes_string("Date", x)) + 
    geom_line() + 
    labs(title = i, y = "", x = "") + 
    theme_bw() +
    geom_hline(yintercept = 0)
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM