在使用ggplot2进行循环时,最后一行代码出现错误。

我试图使用ggplot中的循环为数据集中的每个区域创建一个图。然而,在最后一步–当我运行图形函数时–它给了我以下错误。

在for (. in i) seq_along(region_list)中出错:4个参数传递给’for’,需要3个参数。

这,这是我的代码。

# Create graphing function
region.graph <- function(df, na.rm = TRUE, ...){

  # Create list of regions in data to loop over 
  region_list <- unique(df_stock$Region)

  # Create for loop to produce ggplot2 graphs 
  df_stock %>%
    gather(Stock, value, TotalStock, Total_predicted, FirstStock, First_predicted, SecondStock) %>%

    for (i in seq_along(region_list)) {
      plot <-
        ggplot(df_stock, subset(Region==region_list[i]), aes(x=Year, y=value, colour=Stock)) +
        geom_point()+ 
        geom_line()+
        labs(title = "Housing Stock in number of homes, real and predicted",
        x = "Year",
        y = "Number of homes") +
        theme_bw() +
        theme(axis.text.x = element_text(colour = "grey20", size = 10, angle = 90, hjust = 0.5, 
                                   vjust = 0.5),
                        axis.text.y = element_text(colour = "grey20",
                                                   size = 10),text = element_text(size = 12))+
        scale_x_continuous(breaks=seq(1998, 2018, 1))
      print(plot)
    }
}

# Run graphinc function
region.graph(df_stock)

你能帮我看看代码吗?

PS:我试着把gather命令改成:df_stock <- df_stock %>% gather ,然而,这没有什么区别。

structure(list(Region = structure(c(1L, 1L, 1L, 1L, 1L, 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, 2L, 2L, 
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 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, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
5L, 5L, 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, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 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, 8L, 8L, 8L, 8L, 
8L, 9L, 9L, 9L, 9L, 9L, 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, 10L, 10L, 10L, 10L, 10L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 
12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 
13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 
14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 
15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 
17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L), .Label = c("Andalusia", 
"Aragon", "Asturias", "Balearic Islands", "Basque Country", "Canary Islands", 
"Cantabria", "Castile-La Mancha", "Castile and Leon", "Catalonia", 
"Extremadura", "Galicia", "La Rioja", "Madrid", "Murcia", "National", 
"Navarre", "Valencian Community"), class = "factor"), Year = c(2001, 
2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 1998, 1999, 2000, 2001, 2002, 
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 
2014, 2015, 2016, 2017, 2018, 1998, 1999, 2000, 2001, 2002, 2003, 
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 
2015, 2016, 2017, 2018, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 
2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 
2016, 2017, 2018, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 
2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 
2017, 2018, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 
2018, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 
2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 
1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 
2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 1998, 
1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 1998, 1999, 
2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 1998, 1999, 2000, 
2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 
2012, 2013, 2014, 2015, 2016, 2017, 2018, 1998, 1999, 2000, 2001, 
2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 
2013, 2014, 2015, 2016, 2017, 2018, 1998, 1999, 2000, 2001, 2002, 
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 
2014, 2015, 2016, 2017, 2018, 1998, 1999, 2000, 2001, 2002, 2003, 
2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 
2015, 2016, 2017, 2018, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 
2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 
2016, 2017, 2018, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 
2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 
2017, 2018, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 
2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 
2018, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 
2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 
1998, 1999, 2000), TotalStock = c(3554198L, 3661570L, 3772509L, 
3869035L, 3957925L, 4067012L, 4159934L, 4248653L, 4299768L, 4330256L, 
4362573L, 4384954L, 4394515L, 4401817L, 4407793L, 4413250L, 4422047L, 
4432048L, NA, NA, NA, 657555L, 671516L, 685787L, 696640L, 709810L, 
722305L, 734445L, 752169L, 765180L, 773915L, 779117L, 785857L, 
787938L, 790341L, 792179L, 794012L, 795925L, 798945L, NA, NA, 
NA, 524336L, 532501L, 541156L, 552308L, 564047L, 576333L, 586204L, 
596656L, 602723L, 609935L, 615163L, 619310L, 621278L, 621859L, 
622383L, 622769L, 624001L, 624631L, NA, NA, NA, 504041L, 515076L, 
525645L, 534557L, 544303L, 553224L, 564062L, 574529L, 581969L, 
585687L, 587234L, 590085L, 591709L, 592963L, 594469L, 595630L, 
597501L, 599270L, NA, NA, NA, 892009L, 908604L, 919083L, 931408L, 
944680L, 957045L, 971426L, 983247L, 994459L, 1003676L, 1019564L, 
1028524L, 1033938L, 1038954L, 1044572L, 1048157L, 1052012L, 1056514L, 
NA, NA, NA, 855022L, 880598L, 903527L, 926062L, 947794L, 971043L, 
990993L, 1016495L, 1030507L, 1038700L, 1042320L, 1044637L, 1045619L, 
1046356L, 1047307L, 1047838L, 1049945L, 1053241L, NA, NA, NA, 
286901L, 293927L, 302467L, 310948L, 317211L, 327094L, 336277L, 
344226L, 352750L, 356094L, 358666L, 360627L, 361673L, 362356L, 
363195L, 363852L, 364468L, 364937L, NA, NA, NA, 988555L, 1002094L, 
1016706L, 1035607L, 1058347L, 1092393L, 1141265L, 1187002L, 1219348L, 
1240554L, 1247642L, 1256651L, 1260106L, 1262964L, 1265793L, 1267805L, 
1270032L, 1272514L, NA, NA, NA, 1455050L, 1484464L, 1509014L, 
1535677L, 1565865L, 1604424L, 1640191L, 1674758L, 1696219L, 1709658L, 
1720382L, 1730724L, 1735314L, 1739251L, 1742855L, 1745015L, 1747011L, 
1749430L, NA, NA, NA, 3328120L, 3389370L, 3449165L, 3527738L, 
3597700L, 3662511L, 3732762L, 3807445L, 3835854L, 3851957L, 3869267L, 
3882901L, 3888233L, 3893959L, 3899624L, 3905544L, 3915370L, 3924907L, 
NA, NA, NA, 575284L, 581973L, 591382L, 598303L, 605750L, 612085L, 
622663L, 632010L, 640603L, 646269L, 650098L, 654789L, 656783L, 
658494L, 660012L, 661140L, 662378L, 663844L, NA, NA, NA, 1312496L, 
1345625L, 1374478L, 1407723L, 1440998L, 1475077L, 1512422L, 1550411L, 
1575449L, 1595995L, 1606880L, 1613132L, 1616838L, 1619573L, 1620454L, 
1621564L, 1623679L, 1626000L, NA, NA, NA, 156769L, 160891L, 164544L, 
168166L, 172723L, 178439L, 183619L, 190047L, 194533L, 197274L, 
198973L, 200183L, 200805L, 201216L, 201553L, 201969L, 202403L, 
203058L, NA, NA, NA, 2482885L, 2517059L, 2552919L, 2612769L, 
2675087L, 2741442L, 2792090L, 2831768L, 2867105L, 2885861L, 2900061L, 
2923510L, 2932915L, 2941996L, 2951813L, 2962048L, 2974747L, 2989422L, 
NA, NA, NA, 595319L, 606145L, 617967L, 636440L, 656319L, 693797L, 
722252L, 750719L, 769138L, 778071L, 779493L, 783002L, 783875L, 
784981L, 785723L, 786139L, 786733L, 787604L, NA, NA, NA, 21033759L, 
21487307L, 21926409L, 22417950L, 22927382L, 23493772L, 24034966L, 
24569715L, 24908126L, 25106251L, 25249053L, 25382415L, 25441306L, 
25492335L, 25541915L, 25586279L, 25645100L, 25712744L, NA, NA, 
NA, 261147L, 266064L, 270159L, 274267L, 279563L, 287056L, 293066L, 
300192L, 305484L, 307189L, 309418L, 312305L, 314691L, 316444L, 
317420L, 318158L, 319331L, 320860L, NA, NA, NA, 2558691L, 2622936L, 
2681679L, 2751137L, 2839518L, 2922289L, 3000215L, 3078014L, 3125409L, 
3142938L, 3149218L, 3157657L, 3161095L, 3164586L, 3170272L, 3176853L, 
3182158L, 3188841L, NA, NA, NA), Total_predicted = c(3561323.44462629, 
3662417.16445901, 3768147.23306634, 3863493.09850688, 3953517.44047705, 
4063586.94882417, 4169697.67004026, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 3370753.52258001, 3419058.33863081, 3472022.34261276, 
658041.590196968, 671545.824212018, 685573.405704468, 696105.77368714, 
709479.17083284, 722338.565991808, 734973.669374747, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 630920.976278854, 637388.05582101, 
643855.135363165, 524490.527244104, 532503.751784394, 541048.465707457, 
552250.626544392, 563933.172015757, 576300.521114927, 586357.935588964, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 505806.631249655, 
510354.953852242, 514933.939303836, 504538.830380501, 515092.998730308, 
525328.950948965, 534148.08372655, 543937.018046686, 553369.853362918, 
564492.264804064, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
488332.537415506, 492787.563251606, 496962.255829311, 891691.614362846, 
909290.969575768, 919453.834233403, 931000.232017603, 943889.470777587, 
957165.284969993, 971763.594062793, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 867866.360080382, 873736.202830588, 879595.872543098, 
855718.941659047, 880577.29568558, 903088.397467795, 925521.546223447, 
947435.008135338, 970950.837815491, 991746.973013297, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 813511.708498317, 827629.867753804, 
839548.019370706, 286968.991406982, 293911.092210367, 302517.673323334, 
310972.015383595, 316970.234060204, 327058.608619508, 336426.384996004, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 265452.538624564, 
269329.559833471, 275236.435078456, 988292.933171216, 1002122.76237512, 
1016836.20118935, 1035726.70361599, 1058325.68880108, 1092578.94922528, 
1141083.76162195, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
944319.96703352, 957366.596810102, 970861.725404043, 1455840.08759773, 
1484873.55393622, 1508871.79539098, 1534488.50198694, 1564987.57842551, 
1604478.12787074, 1641145.35479187, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 1389222.26409143, 1406261.10543904, 1425035.30107694, 
3332065.8605128, 3389885.25012116, 3447636.86498265, 3523740.3751913, 
3594045.37927878, 3663055.13264827, 3736937.13726505, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 3217722.33201406, 3249712.01253715, 
3282643.00898901, 575358.627503511, 581998.247882319, 591281.203582726, 
598241.465228356, 605694.419552271, 612185.450075723, 622680.586175088, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 550106.172611672, 
557042.972701192, 564433.362875484, 1313193.28194806, 1345964.41492236, 
1373820.38258761, 1407166.80185065, 1440493.03562988, 1474848.72908369, 
1513332.35397775, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
1246924.3385821, 1263880.14498703, 1281491.97961597, 157004.321922718, 
160931.115901888, 164415.820492675, 167969.872438503, 172564.134709938, 
178467.328307764, 183798.406226505, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 149133.397125617, 150338.307419446, 152271.364941201, 
2480367.15803975, 2517602.59833994, 2555089.47500802, 2613735.10299871, 
2674956.04893936, 2740748.56520539, 2791752.05146883, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 2369156.09059112, 2403160.95452803, 
2436792.47355505, 594379.987353001, 605921.52237744, 618432.083525486, 
637009.905253422, 657250.980775606, 693408.671954929, 721835.848760112, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 562394.893735772, 
571340.306533536, 579750.585501847, 21051561.7174417, 21490785.1134156, 
21918918.1045874, 22402310.3828287, 22911770.3323138, 23489097.7770793, 
24057101.5723335, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
20110395.1126741, 20365972.3268856, 20631106.0810786, 261458.230221516, 
266071.254384021, 270033.028076524, 274103.343514028, 279274.582365104, 
287058.576077969, 293322.985360832, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 251991.83946115, 254635.606250081, 256969.253767583, 
2560149.72123885, 2623194.43622512, 2681021.32124862, 2749779.64131874, 
2838417.73099778, 2922171.2166151, 3001730.9323558, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 2435157.49850146, 2467935.54871928, 
2504305.93669963), FirstStock = c(2415143L, 2481857L, 2544027L, 
2624788L, 2741467L, 2819423L, 2898097L, 2995732L, 3016676L, 3043316L, 
3094866L, 3086326L, 3281209L, 3314344L, 3294515L, 3314568L, 3317027L, 
3323439L, NA, NA, NA, 443205L, 463807L, 468723L, 480302L, 486150L, 
499715L, 512554L, 518985L, 522069L, 519140L, 538869L, 541197L, 
568990L, 570233L, 561298L, 562161L, 561428L, 563277L, NA, NA, 
NA, 389310L, 396293L, 403383L, 403701L, 412378L, 431954L, 435128L, 
442743L, 443276L, 437773L, 460354L, 465585L, 490648L, 489769L, 
484121L, 484851L, 479930L, 481351L, NA, NA, NA, 305431L, 314311L, 
331486L, 343488L, 351392L, 369152L, 379105L, 399858L, 404281L, 
411111L, 429945L, 431621L, 445057L, 449435L, 448407L, 450824L, 
452067L, 459201L, NA, NA, NA, 741399L, 750611L, 775162L, 786532L, 
813222L, 838702L, 862331L, 866957L, 878951L, 882251L, 888017L, 
878667L, 913833L, 922274L, 917246L, 918410L, 914491L, 923918L, 
NA, NA, NA, 552351L, 570032L, 604276L, 614807L, 632023L, 669434L, 
690469L, 719747L, 724107L, 737541L, 790188L, 804207L, 834001L, 
840011L, 833415L, 843821L, 854627L, 864929L, NA, NA, NA, 182645L, 
186458L, 190693L, 194399L, 204352L, 215787L, 215045L, 218680L, 
224257L, 231669L, 235425L, 234436L, 251975L, 253382L, 249697L, 
251631L, 250665L, 252525L, NA, NA, NA, 610227L, 625556L, 643664L, 
662032L, 681229L, 696428L, 724486L, 749561L, 769028L, 781670L, 
783786L, 784306L, 829001L, 830914L, 821843L, 824105L, 818306L, 
821591L, NA, NA, NA, 889197L, 911788L, 916088L, 929562L, 950279L, 
977802L, 987169L, 999209L, 1015931L, 1025836L, 1034882L, 1020304L, 
1070215L, 1067995L, 1058455L, 1058094L, 1051940L, 1050349L, NA, 
NA, NA, 2315774L, 2380716L, 2446788L, 2536954L, 2626121L, 2718564L, 
2805015L, 2867395L, 2883159L, 2887895L, 2952777L, 2932434L, 3118750L, 
3125370L, 3109207L, 3114763L, 3161200L, 3194403L, NA, NA, NA, 
366893L, 378932L, 379706L, 388111L, 388290L, 399830L, 403426L, 
409540L, 415916L, 424619L, 426968L, 423103L, 448214L, 449713L, 
446112L, 447377L, 443915L, 442595L, NA, NA, NA, 900376L, 906883L, 
933504L, 956439L, 978484L, 989946L, 992007L, 1035741L, 1038636L, 
1055387L, 1057682L, 1069974L, 1109816L, 1116325L, 1111191L, 1118068L, 
1115982L, 1121313L, NA, NA, NA, 101439L, 103405L, 110858L, 111997L, 
114068L, 119554L, 122452L, 124185L, 125869L, 126387L, 128176L, 
128420L, 137627L, 138006L, 137281L, 137782L, 138263L, 138578L, 
NA, NA, NA, 1873671L, 1943943L, 2021109L, 2063704L, 2146322L, 
2210831L, 2269991L, 2332536L, 2413004L, 2425145L, 2468367L, 2506626L, 
2607888L, 2643769L, 2640078L, 2667416L, 2686321L, 2709764L, NA, 
NA, NA, 378211L, 386245L, 406178L, 422348L, 445942L, 467706L, 
482960L, 499204L, 508587L, 517028L, 514423L, 508870L, 517817L, 
524125L, 518735L, 520342L, 520657L, 523837L, NA, NA, NA, 14184026L, 
14587227L, 15016057L, 15442540L, 15996763L, 16508248L, 16936413L, 
17396800L, 17628199L, 17762862L, 18099406L, 18112437L, 18991031L, 
19113128L, 18976018L, 19056443L, 19120417L, 19235413L, NA, NA, 
NA, 188730L, 191407L, 197854L, 204796L, 208168L, 217490L, 227507L, 
227306L, 231275L, 232663L, 248692L, 247877L, 257775L, 260130L, 
258235L, 262190L, 261222L, 262559L, NA, NA, NA, 1492744L, 1558475L, 
1604021L, 1678980L, 1777373L, 1826234L, 1887236L, 1947266L, 1968265L, 
1977457L, 1995668L, 1999081L, 2056283L, 2064416L, 2032589L, 2026269L, 
2037408L, 2046466L, NA, NA, NA), First_predicted = c(2397674.54012695, 
2480977.44252834, 2568100.77324229, 2646667.34826112, 2720848.89965026, 
2811548.00070601, 2898984.99548503, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 2240641.76259012, 2280445.73244755, 2324088.95046144, 
446808.447737156, 458078.099426931, 469784.498863854, 478574.047275188, 
489734.512028177, 500466.028100932, 511010.366567767, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 424175.555554644, 429572.509858675, 
434969.464162706, 388586.464322128, 394516.553959468, 400839.965868671, 
409129.989137197, 417775.515228369, 426927.821939059, 434370.689545107, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 374759.673688127, 
378125.604668505, 381514.227318413, 304000.643496574, 317442.666836969, 
330479.403041472, 341711.646521887, 354179.0507974, 366192.919468464, 
380358.669837235, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
283359.948647254, 289033.968549732, 294350.949784877, 731112.452290638, 
759166.098512329, 775365.86300682, 793771.000845907, 814316.648107689, 
835478.50262752, 858748.434609096, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 693134.626018267, 702491.246752322, 711831.651505903, 
548948.794022273, 574101.550792266, 596879.256313246, 619578.085593484, 
641751.073268774, 665545.405892653, 686587.834117306, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 506241.69167378, 520527.055274856, 
532586.356081654, 180998.913484762, 186115.810687388, 192459.566283626, 
198691.109221912, 203112.287945233, 210548.246745541, 217453.065631541, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 165139.524494166, 
167997.206850019, 172351.057978569, 619645.004179506, 629799.09366854, 
640601.943957453, 654471.663482164, 671064.212424554, 696213.523128999, 
731826.559158784, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
587359.324676379, 596938.376183701, 606846.722945507, 890699.194636394, 
906422.286212848, 919418.548652475, 933291.291886082, 949808.085514862, 
971194.216887976, 991051.376209368, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 854622.271288625, 863849.666314138, 874016.843524307, 
2309831.41457882, 2380301.83565497, 2450689.65269496, 2543444.80708356, 
2629132.72726681, 2713241.99253665, 2803289.57018424, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 2170469.22733991, 2209458.33241043, 
2249594.71576105, 369735.828586971, 374594.732386855, 381388.040761958, 
386481.591270671, 391935.696478315, 396685.861559474, 404366.248955757, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 351255.969494125, 
356332.35073146, 361740.67121543, 900739.543115826, 917814.436924411, 
932328.35959509, 949702.99745883, 967067.117987301, 984967.622614472, 
1005018.92230407, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
866211.131428987, 875045.693054617, 884222.068076477, 102269.899667489, 
105365.150513427, 108111.929409822, 110913.370572812, 114534.745734727, 
119187.87117862, 123390.032923105, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 96065.7324089642, 97015.4892910511, 98539.1999723848, 
1899569.74880908, 1944935.59687233, 1990607.78276781, 2062058.76139331, 
2136647.38205797, 2216805.78149037, 2278945.94660914, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1764075.59731917, 1805505.46838655, 
1846480.4741879, 382817.910128799, 392547.281524453, 403093.529967109, 
418754.404099586, 435817.380626174, 466297.867454662, 490261.626199219, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 355854.871480319, 
363395.743921687, 370485.505157993, 14197574.010092, 14607149.1357408, 
15006382.476362, 15457145.0152276, 15932215.6165118, 16470572.5503589, 
17000235.1957069, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
13319937.6320264, 13558262.9821402, 13805499.79075, 187537.633350613, 
193157.722071261, 197984.386501936, 202943.288314273, 209243.454997756, 
218726.764368396, 226358.750395764, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, 176004.644564207, 179225.56936041, 182068.67306615, 
1500700.62953454, 1557886.31929354, 1610339.09519084, 1672707.39934982, 
1753107.95787911, 1829077.86028552, 1901243.73846663, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 1387324.48891268, 1417056.32941778, 
1450046.65579029), SecondStock = c(1139055L, 1179713L, 1228482L, 
1244247L, 1216458L, 1247589L, 1261837L, 1252921L, 1283092L, 1286940L, 
1267707L, 1298628L, 1113306L, 1087473L, 1113278L, 1098682L, 1105020L, 
1108609L, NA, NA, NA, 214350L, 207709L, 217064L, 216338L, 223660L, 
222590L, 221891L, 233184L, 243111L, 254775L, 240248L, 244660L, 
218948L, 220108L, 230881L, 231851L, 234497L, 235668L, NA, NA, 
NA, 135026L, 136208L, 137773L, 148607L, 151669L, 144379L, 151076L, 
153913L, 159447L, 172162L, 154809L, 153725L, 130630L, 132090L, 
138262L, 137918L, 144071L, 143280L, NA, NA, NA, 198610L, 200765L, 
194159L, 191069L, 192911L, 184072L, 184957L, 174671L, 177688L, 
174576L, 157289L, 158464L, 146652L, 143528L, 146062L, 144806L, 
145434L, 140069L, NA, NA, NA, 150610L, 157993L, 143921L, 144876L, 
131458L, 118343L, 109095L, 116290L, 115508L, 121425L, 131547L, 
149857L, 120105L, 116680L, 127326L, 129747L, 137521L, 132596L, 
NA, NA, NA, 302671L, 310566L, 299251L, 311255L, 315771L, 301609L, 
300524L, 296748L, 306400L, 301159L, 252132L, 240430L, 211618L, 
206345L, 213892L, 204017L, 195318L, 188312L, NA, NA, NA, 104256L, 
107469L, 111774L, 116549L, 112859L, 111307L, 121232L, 125546L, 
128493L, 124425L, 123241L, 126191L, 109698L, 108974L, 113498L, 
112221L, 113803L, 112412L, NA, NA, NA, 378328L, 376538L, 373042L, 
373575L, 377118L, 395965L, 416779L, 437441L, 450320L, 458884L, 
463856L, 472345L, 431105L, 432050L, 443950L, 443700L, 451726L, 
450923L, NA, NA, NA, 565853L, 572676L, 592926L, 606115L, 615586L, 
626622L, 653022L, 675549L, 680288L, 683822L, 685500L, 710420L, 
665099L, 671256L, 684400L, 686921L, 695071L, 699081L, NA, NA, 
NA, 1012346L, 1008654L, 1002377L, 990784L, 971579L, 943947L, 
927747L, 940050L, 952695L, 964062L, 916490L, 950467L, 769483L, 
768589L, 790417L, 790781L, 754170L, 730504L, NA, NA, NA, 208391L, 
203041L, 211676L, 210192L, 217460L, 212255L, 219237L, 222470L, 
224687L, 221650L, 223130L, 231686L, 208569L, 208781L, 213900L, 
213763L, 218463L, 221249L, NA, NA, NA, 412120L, 438742L, 440974L, 
451284L, 462514L, 485131L, 520415L, 514670L, 536813L, 540608L, 
549198L, 543158L, 507022L, 503248L, 509263L, 503496L, 507697L, 
504687L, NA, NA, NA, 55330L, 57486L, 53686L, 56169L, 58655L, 
58885L, 61167L, 65862L, 68664L, 70887L, 70797L, 71763L, 63178L, 
63210L, 64272L, 64187L, 64140L, 64480L, NA, NA, NA, 609214L, 
573116L, 531810L, 549065L, 528765L, 530611L, 522099L, 499232L, 
454101L, 460716L, 431694L, 416884L, 325027L, 298227L, 311735L, 
294632L, 288426L, 279658L, NA, NA, NA, 217108L, 219900L, 211789L, 
214092L, 210377L, 226091L, 239292L, 251515L, 260551L, 261043L, 
265070L, 274132L, 266058L, 260856L, 266988L, 265797L, 266076L, 
263767L, NA, NA, NA, 6849733L, 6900080L, 6910352L, 6975410L, 
6930619L, 6985524L, 7098553L, 7172915L, 7279927L, 7343389L, 7149647L, 
7269978L, 6450275L, 6379207L, 6565897L, 6529836L, 6524683L, 6477331L, 
NA, NA, NA, 72417L, 74657L, 72305L, 69471L, 71395L, 69566L, 65559L, 
72886L, 74209L, 74526L, 60726L, 64428L, 56916L, 56314L, 59185L, 
55968L, 58109L, 58301L, NA, NA, NA, 1065947L, 1064461L, 1077658L, 
1072157L, 1062145L, 1096055L, 1112979L, 1130748L, 1157144L, 1165481L, 
1153550L, 1158576L, 1104812L, 1100170L, 1137683L, 1150584L, 1144750L, 
1142375L, NA, NA, NA)), class = "data.frame", row.names = c(NA, 
-378L))

解决方案:

试试这个。正如 @Gavin Kelly 所指出的,在用管道传输以下结果时存在问题 gather进入 for 循环。第二个。循环之后 gathering你忘了把结果分配给一个变量。第三。还有一个bug,就是在 subset 里边 ggplot. 终于… 我换了 df_stock 在您的函数中使用 df.

# Create graphing function
region.graph <- function(df, na.rm = TRUE, ...){

  # Create list of regions in data to loop over 
  region_list <- unique(df$Region)

  # Create for loop to produce ggplot2 graphs 
  df <- df %>%
    gather(Stock, value, TotalStock, Total_predicted, FirstStock, First_predicted, SecondStock)

    for (i in seq_along(region_list)) {
      plot <-
        ggplot(subset(df, Region==region_list[i]), aes(x=Year, y=value, colour=Stock)) +
        geom_point()+ 
        geom_line()+
        labs(title = "Housing Stock in number of homes, real and predicted",
             x = "Year",
             y = "Number of homes") +
        theme_bw() +
        theme(axis.text.x = element_text(colour = "grey20", size = 10, angle = 90, hjust = 0.5, 
                                         vjust = 0.5),
              axis.text.y = element_text(colour = "grey20",
                                         size = 10),text = element_text(size = 12))+
        scale_x_continuous(breaks=seq(1998, 2018, 1))
      print(plot)
    }
}

# Run graphinc function
region.graph(df_stock)

考查情节。

enter image description here

给TA打赏
共{{data.count}}人
人已打赏
未分类

在预构建事件中设置环境变量,并在编译步骤中使用。

2022-9-9 2:12:16

未分类

创建过程总是失败

2022-9-9 2:12:18

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索