如何正确循环 ggplot 对象?

How to properly loop over a ggplot object?

我正在尝试为每个数据帧组(由“decil”列表示)创建一个独特的时间序列 ggplot 图:

对于此任务,我选择使用 for-loop 结构来 return 每个组的独特时间序列,

这是我目前尝试过的方法:

library(ggplot2)
library(reshape2)
library(scales)
library(ggforce)

##### defining ggplot object as p #####
p <- ggplot(df,aes(x=año,
              y=growth,
              colour=decil)) + 
  geom_line() +
  scale_x_date(date_breaks = "2 years",date_labels = "%Y") +
  facet_wrap(~decil, nrow=2, scales="free") +
  facet_wrap_paginate(~decil, nrow = 1, ncol = 1, scales = "free") +
  xlab("Año de Encuesta ENIGH")+
  ylab("Crecimiento en el Poder Adquisitivo por Decil") 


##### defining a for-loop to accomplish this task ####
required_n_pages <- n_pages(p)

for(i in 1:required_n_pages){
  
  
  p <- ggplot(df,aes(x=año,
                     y=growth,
                     colour=decil)) + 
    geom_line() +
    scale_x_date(date_breaks = "2 years",date_labels = "%Y") +
    facet_wrap(~decil, nrow=2, scales="free") +
    facet_wrap_paginate(~decil, nrow = 1, ncol = 1, scales = "free") +
    xlab("Año de Encuesta ENIGH")+
    ylab("Crecimiento en el Poder Adquisitivo por Decil") 
  
    print(p)
}


for 循环 return 下一个图 10 次对于一个唯一的组而不是对于每个组,10 表示 required_n_pages 的长度:

预期输出应该是 10 个不同的时间序列图,其中每个不同的时间序列由一个独特的组表示,而不是一个独特的组的 10 个重复图。

我错过了什么?

数据

df <- structure(list(año = structure(c(6940, 8035, 8766, 9496, 10227, 
10957, 11688, 12418, 12784, 13149, 13879, 14610, 15340, 16071, 
16801, 17532, 6940, 8035, 8766, 9496, 10227, 10957, 11688, 12418, 
12784, 13149, 13879, 14610, 15340, 16071, 16801, 17532, 6940, 
8035, 8766, 9496, 10227, 10957, 11688, 12418, 12784, 13149, 13879, 
14610, 15340, 16071, 16801, 17532, 6940, 8035, 8766, 9496, 10227, 
10957, 11688, 12418, 12784, 13149, 13879, 14610, 15340, 16071, 
16801, 17532, 6940, 8035, 8766, 9496, 10227, 10957, 11688, 12418, 
12784, 13149, 13879, 14610, 15340, 16071, 16801, 17532, 6940, 
8035, 8766, 9496, 10227, 10957, 11688, 12418, 12784, 13149, 13879, 
14610, 15340, 16071, 16801, 17532, 6940, 8035, 8766, 9496, 10227, 
10957, 11688, 12418, 12784, 13149, 13879, 14610, 15340, 16071, 
16801, 17532, 6940, 8035, 8766, 9496, 10227, 10957, 11688, 12418, 
12784, 13149, 13879, 14610, 15340, 16071, 16801, 17532, 6940, 
8035, 8766, 9496, 10227, 10957, 11688, 12418, 12784, 13149, 13879, 
14610, 15340, 16071, 16801, 17532, 6940, 8035, 8766, 9496, 10227, 
10957, 11688, 12418, 12784, 13149, 13879, 14610, 15340, 16071, 
16801, 17532), class = "Date"), decil = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 
10L), .Label = c("Decil 1", "Decil 10", "Decil 2", "Decil 3", 
"Decil 4", "Decil 5", "Decil 6", "Decil 7", "Decil 8", "Decil 9"
), class = "factor"), growth = c(-0.00518146762073594, 0.00308138265449932, 
-0.469279778645287, -0.0273923609850588, -0.00116791699821611, 
0.01276823919767, -0.00591033604229833, 0.02217410209762, -0.0122771361710334, 
0.0291323734975109, 0.019403678544531, -0.01710307316049, 0.00114882675671299, 
0.0131658931355581, 0.0146477971543751, 0.00271712366085317, 
0.0134328539491741, 0.000420320020065887, -0.401490999871048, 
-0.0265571871864779, 0.0108040626539541, 0.00501353172078248, 
0.00264579769939744, 0.0345907008361297, -0.0147337410644853, 
0.00753187592344928, -0.0167914957857585, -0.00386430954714176, 
0.0118684061581655, -0.000837921856848724, 0.00271367995890336, 
-0.00244522199262534, 0.00244746086109351, 0.00274660079065288, 
-0.465074910900121, -0.0120391288467938, 0.00221116802499613, 
0.0168878772891571, -0.000553564396020631, 0.0247304289689454, 
-0.0149473828679342, 0.0206995781551323, 0.00995111688295987, 
-0.0123590101185821, -0.000513493069577327, 0.0157518955283768, 
0.0149952051319558, 0.00569875774759632, -0.000267057797736821, 
0.000595105282871455, -0.454844375744304, -0.0198229069194358, 
0.00692781223252039, 0.0145357299983422, 0.00274519154273341, 
0.0250570229399105, -0.0158798738651613, 0.018318771262172, 0.00541185894582713, 
-0.0109932567808292, 0.00159443784781767, 0.0138386952581564, 
0.0142469406477504, 0.00410413545862179, -0.000805341335460025, 
-0.00144568025477976, -0.449911674142431, -0.0188765679042184, 
0.0101321912136977, 0.0116094403156238, 0.00427582908849506, 
0.0259548917592467, -0.0191921130444488, 0.0186512952445194, 
0.000990294699338842, -0.00837785709745078, 0.00543096597486442, 
0.00741481129770512, 0.0129946597715581, 0.00622336720429403, 
0.00206256733184722, 0.00139031953354982, -0.446027046214633, 
-0.0190241726271236, 0.00690016525955506, 0.0154716844723562, 
0.00256469067826587, 0.0246741215896569, -0.0152082399890795, 
0.0146298279854535, -0.00178133254716425, -0.00570996073876508, 
0.00530083911456838, 0.00771074074546392, 0.0122148158785322, 
0.00480369193797477, -0.00179320234778936, -0.000401792821387455, 
-0.438150991019374, -0.021998371424533, 0.0106632797701266, 0.00742663665613463, 
0.00534777779842798, 0.0224439518748242, -0.0119081274931006, 
0.0126230193789639, -0.00256049568632538, -0.0054511282604089, 
0.00828289107854747, 0.00198585514462626, 0.0112490515302622, 
0.00454776270025511, 0.00377935359008342, 0.00308138951062849, 
-0.436715092679294, -0.0168702475271693, 0.00963026950989102, 
0.00924694309980609, 0.000909783913993086, 0.0219423265975474, 
-0.0104193226868249, 0.0126951414528773, -0.00599866837527287, 
-0.00504144411887512, 0.012633719573124, -0.00208565860801489, 
0.0128053568465138, 0.00223463615789722, 0.00572034514146965, 
0.000762802869455859, -0.429497886598882, -0.0221763771759914, 
0.0108353667375486, 0.0127220562258605, -0.000199740141807546, 
0.0234686607111593, -0.0101924956162359, 0.00996632035015618, 
-0.00943588762041933, -0.00311576607205403, 0.0118147547163377, 
-0.00283811580551448, 0.011151512141408, 0.00256848322836834, 
0.00698508059308152, -0.000557319323771243, -0.420314342083723, 
-0.0256639439214237, 0.0109841238497413, 0.00970528742740132, 
-0.00041290083196072, 0.0289203820342315, -0.0113995943017165, 
0.010666144712339, -0.0148427029516519, -0.00237956102817481, 
0.0118845793975459, -0.000723117556104279, 0.00578897782929168, 
0.00310574312466805)), row.names = c(NA, -160L), groups = structure(list(
    decil = structure(1:10, .Label = c("Decil 1", "Decil 10", 
    "Decil 2", "Decil 3", "Decil 4", "Decil 5", "Decil 6", "Decil 7", 
    "Decil 8", "Decil 9"), class = "factor"), .rows = structure(list(
        1:16, 17:32, 33:48, 49:64, 65:80, 81:96, 97:112, 113:128, 
        129:144, 145:160), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 10L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

在循环中您需要指定要打印的页面。

facet_wrap_paginate(~decil, nrow = 1, ncol = 1, scales = "free", page=i)

顺便说一下,由于您指定每页 1 个图,您可以通过删除对 facet_wrap()

的调用来简化代码
library(ggforce)
p <- ggplot(df,aes(x=año, y=growth, colour=decil)) + 
   geom_line() +
   scale_x_date(date_breaks = "2 years",date_labels = "%Y") +
#   facet_wrap(~decil, nrow=2, scales="free") +
   facet_wrap_paginate(~decil, nrow = 1, ncol = 1, scales = "free") +
   xlab("Año de Encuesta ENIGH")+
   ylab("Crecimiento en el Poder Adquisitivo por Decil") 


required_n_pages <- n_pages(p)

for(i in 1:required_n_pages){
   p <- ggplot(df,aes(x=año, y=growth, colour=decil)) + 
      geom_line() +
      scale_x_date(date_breaks = "5 years",date_labels = "%Y") +
  #    facet_wrap(~decil, nrow=2, scales="free") +
      facet_wrap_paginate(~decil, nrow = 1, ncol = 1, scales = "free", page=i) +
      xlab("Año de Encuesta ENIGH")+
      ylab("Crecimiento en el Poder Adquisitivo por Decil") 
   
   print(p)
}