我已经花了很长时间研究这个问题,并且得到了这个社区的帮助,但是一个问题导致了下一个问题。就目前而言,剧情有点“疯狂”
应该有三行(Plot_df$sector
ETS,受监管,不受监管)
我试图创建一个循环以便为多个国家/地区创建地块。
我的数据框:Plot_df
以下是数据摘录:
year country iso2 sector emissions
378 1990 Austria AT ETS 0
2836 1990 Austria AT Regulated 38264403
3100 1990 Austria AT Unregulated 24027828
409 1991 Austria AT ETS 0
5100 1991 Austria AT Regulated 39318197
6100 1991 Austria AT Unregulated 26581483
440 1992 Austria AT ETS 0
8100 1992 Austria AT Regulated 34823803
9100 1992 Austria AT Unregulated 25607658
471 1993 Austria AT ETS 0
11100 1993 Austria AT Regulated 35193747
12100 1993 Austria AT Unregulated 25593991
502 1994 Austria AT ETS 0
14100 1994 Austria AT Regulated 36260166
15100 1994 Austria AT Unregulated 24930182
533 1995 Austria AT ETS 0
17100 1995 Austria AT Regulated 38441281
18100 1995 Austria AT Unregulated 25764669
564 1996 Austria AT ETS 0
20100 1996 Austria AT Regulated 39489370
21100 1996 Austria AT Unregulated 28184521
595 1997 Austria AT ETS 0
23100 1997 Austria AT Regulated 41822882
24100 1997 Austria AT Unregulated 25630126
626 1998 Austria AT ETS 0
26100 1998 Austria AT Regulated 39063198
27100 1998 Austria AT Unregulated 27991169
657 1999 Austria AT ETS 0
2910 1999 Austria AT Regulated 38127128
3010 1999 Austria AT Unregulated 27493880
688 2000 Austria AT ETS 0
3210 2000 Austria AT Regulated 38294108
3310 2000 Austria AT Unregulated 27967635
719 2001 Austria AT ETS 0
3510 2001 Austria AT Regulated 40723095
3610 2001 Austria AT Unregulated 29667765
750 2002 Austria AT ETS 0
3810 2002 Austria AT Regulated 41130208
3910 2002 Austria AT Unregulated 31016331
781 2003 Austria AT ETS 0
4110 2003 Austria AT Regulated 44805099
4210 2003 Austria AT Unregulated 32959151
812 2004 Austria AT ETS 0
4410 2004 Austria AT Regulated 44921799
4510 2004 Austria AT Unregulated 33130748
1 2005 Austria AT ETS 33373156
4710 2005 Austria AT Regulated 47032869
4810 2005 Austria AT Unregulated 32333810
26 2006 Austria AT ETS 32384372
5010 2006 Austria AT Regulated 46050726
5110 2006 Austria AT Unregulated 30637735
51 2007 Austria AT ETS 31751260
5310 2007 Austria AT Regulated 43754649
5410 2007 Austria AT Unregulated 30277468
78 2008 Austria AT ETS 32078974
5610 2008 Austria AT Regulated 44826920
5710 2008 Austria AT Unregulated 28979354
107 2009 Austria AT ETS 27359832
5910 2009 Austria AT Regulated 39430745
6010 2009 Austria AT Unregulated 28052317
136 2010 Austria AT ETS 30919712
6210 2010 Austria AT Regulated 42836143
6310 2010 Austria AT Unregulated 29546998
165 2011 Austria AT ETS 30599418
6510 2011 Austria AT Regulated 42148355
6610 2011 Austria AT Unregulated 27967317
194 2012 Austria AT ETS 28387060
6810 2012 Austria AT Regulated 39494447
6910 2012 Austria AT Unregulated 28166952
223 2013 Austria AT ETS 29804410
7110 2013 Austria AT Regulated 38573958
7210 2013 Austria AT Unregulated 29427268
254 2014 Austria AT ETS 28055974
7410 2014 Austria AT Regulated 36311073
7510 2014 Austria AT Unregulated 27942255
285 2015 Austria AT ETS 29492064
7710 2015 Austria AT Regulated 38003088
7810 2015 Austria AT Unregulated 28700896
316 2016 Austria AT ETS 29000120
8010 2016 Austria AT Regulated 37651775
8110 2016 Austria AT Unregulated 29750311
347 2017 Austria AT ETS 30555226
813 1990 Belgium BE ETS 0
8310 1990 Belgium BE Regulated 78614107
8410 1990 Belgium BE Unregulated 41870292
379 1991 Belgium BE ETS 0
8610 1991 Belgium BE Regulated 79811521
8710 1991 Belgium BE Unregulated 43733190
410 1992 Belgium BE ETS 0
8910 1992 Belgium BE Regulated 78283962
9010 1992 Belgium BE Unregulated 44373851
441 1993 Belgium BE ETS 0
9210 1993 Belgium BE Regulated 76752290
9310 1993 Belgium BE Unregulated 44804991
472 1994 Belgium BE ETS 0
9510 1994 Belgium BE Regulated 80647991
9610 1994 Belgium BE Unregulated 44290197
503 1995 Belgium BE ETS 0
9810 1995 Belgium BE Regulated 80518704
9910 1995 Belgium BE Unregulated 45564257
534 1996 Belgium BE ETS 0
10110 1996 Belgium BE Regulated 79513349
10210 1996 Belgium BE Unregulated 50070276
565 1997 Belgium BE ETS 0
10410 1997 Belgium BE Regulated 77308936
10510 1997 Belgium BE Unregulated 46737892
596 1998 Belgium BE ETS 0
10710 1998 Belgium BE Regulated 82610050
10810 1998 Belgium BE Unregulated 47675059
627 1999 Belgium BE ETS 0
11010 1999 Belgium BE Regulated 77595053
11110 1999 Belgium BE Unregulated 47150650
658 2000 Belgium BE ETS 0
11310 2000 Belgium BE Regulated 80435088
11410 2000 Belgium BE Unregulated 46359701
689 2001 Belgium BE ETS 0
11610 2001 Belgium BE Regulated 77255899
11710 2001 Belgium BE Unregulated 48873109
720 2002 Belgium BE ETS 0
11910 2002 Belgium BE Regulated 77914653
12010 2002 Belgium BE Unregulated 48529972
751 2003 Belgium BE ETS 0
12210 2003 Belgium BE Regulated 78464806
12310 2003 Belgium BE Unregulated 49488382
782 2004 Belgium BE ETS 0
12510 2004 Belgium BE Regulated 79430967
12610 2004 Belgium BE Unregulated 49609916
2 2005 Belgium BE ETS 55363232
12810 2005 Belgium BE Regulated 77343444
12910 2005 Belgium BE Unregulated 48294759
27 2006 Belgium BE ETS 54775328
13110 2006 Belgium BE Regulated 75869846
13210 2006 Belgium BE Unregulated 48161044
52 2007 Belgium BE ETS 52795332
13410 2007 Belgium BE Regulated 73189198
13510 2007 Belgium BE Unregulated 47422201
79 2008 Belgium BE ETS 55462028
13710 2008 Belgium BE Regulated 71854823
13810 2008 Belgium BE Unregulated 48804185
108 2009 Belgium BE ETS 46206936
14010 2009 Belgium BE Regulated 61048912
14110 2009 Belgium BE Unregulated 46593455
137 2010 Belgium BE ETS 50103980
14310 2010 Belgium BE Regulated 66648934
14410 2010 Belgium BE Unregulated 46933097
166 2011 Belgium BE ETS 46203056
14610 2011 Belgium BE Regulated 61319344
14710 2011 Belgium BE Unregulated 42839297
195 2012 Belgium BE ETS 43006980
14910 2012 Belgium BE Regulated 58934979
15010 2012 Belgium BE Unregulated 42459997
224 2013 Belgium BE ETS 45231176
15210 2013 Belgium BE Regulated 58383554
15310 2013 Belgium BE Unregulated 43586891
255 2014 Belgium BE ETS 43853144
15510 2014 Belgium BE Regulated 56010346
15610 2014 Belgium BE Unregulated 40380694
286 2015 Belgium BE ETS 44713916
15810 2015 Belgium BE Regulated 57375031
15910 2015 Belgium BE Unregulated 42854461
317 2016 Belgium BE ETS 43655728
16110 2016 Belgium BE Regulated 56702848
16210 2016 Belgium BE Unregulated 43540863
348 2017 Belgium BE ETS 43772976
dput(head(Plot_df, 15))
提供了此功能
structure(list(year = c("1990", "1990", "1990", "1990", "1990",
"1990", "1990", "1990", "1990", "1990", "1990", "1990", "1990",
"1990", "1990"), country = c("Austria", "Austria", "Austria",
"Belgium", "Belgium", "Belgium", "Bulgaria", "Bulgaria", "Bulgaria",
"Croatia", "Croatia", "Croatia", "Cyprus", "Cyprus", "Cyprus"
), iso2 = c("AT", "AT", "AT", "BE", "BE", "BE", "BG", "BG", "BG",
"HR", "HR", "HR", "CY", "CY", "CY"), sector = c("ETS", "Regulated",
"Unregulated", "ETS", "Regulated", "Unregulated", "ETS", "Regulated",
"Unregulated", "ETS", "Regulated", "Unregulated", "ETS", "Regulated",
"Unregulated"), emissions = c(0, 38264402.6689529, 24027827.7997971,
0, 78614106.9221497, 41870291.5153503, 0, 69103153.6445618,
9569791.66793823,
0, 17530229.1374207, 5911735.70632935, 0, 3135556.17528036, 1507499.48878214
)), row.names = c("378", "2836", "3100", "813", "8310", "8410",
"558", "16410", "16510", "438", "24510", "24610", "783", "3261",
"3271"), class = "data.frame")
这是我在SO的帮助下尝试的方法:
for(country in unique(Plot_df$country)) {
# Color settings: colorblind-friendly palette
c(
"#999999", "#E69F00", "#56B4E9", "#009E73",
"#F0E442", "#0072B2", "#D55E00", "#CC79A7"
) -> cols
ggplot() +
geom_line(
data = Plot_df, # THIS IS WHAT YOU FORGOT
aes(year, emissions, group = sector),
color = cols[1]
) +
xlim(1990, 2016) +
ylim(min(Plot_df$emissions), max(Plot_df$emissions)) +
labs(
x = "Year", y = "CO2 emissions",
title = sprintf("Emissions for %s", country)
) +
theme(plot.margin = margin(.5, .5, .5, .5, "cm")) -> p
print(p)
ggsave(
plot = p,
filename = sprintf("./%s.png", country),
width = 6.5,
height = 6
)
}
知道为什么会这样吗?
无论如何都要感谢