我试图创建一个循环以便为多个国家/地区创建地块。在SO的帮助下,这已经成功。
现在我有类似下面的情节。但是,我希望ETS行仅在2005年开始(这是当数据开始> 0时)。基本上,ETS不应只是从零跃升至2005年的价值,而应该只是“从头开始”。
此外,我想将x标签设置为5年间隔。
这是我的代码
EUETS_UN <- subset(EUETS_UN, sector != "Total")
EUETS_UN$year <-as.numeric(EUETS_UN$year)
names(EUETS_UN) <- c("Year", "Country","iso2", "Sector", "Emissions")
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2",
"#D55E00", "#CC79A7")
country_2s <- unique(EUETS_UN$Country)
for(i in country_2s) {
# Color settings: colorblind-friendly palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2",
"#D55E00", "#CC79A7")
df_to_plot <- subset(EUETS_UN, Country == i)
df_to_plot$Sector <- as.character(df_to_plot$Sector)
#
p <- ggplot(df_to_plot) +
geom_line(aes(x= Year,y= Emissions,group = Sector, color = Sector)) +
labs(x="Year",y="CO2 Emissions",z="",title=paste("Emissions for",i)) +
xlim(1990,2016) +
theme(plot.margin=unit(c(.5,.5,.5,.5),"cm")) +
scale_color_manual(values = cols)+
scale_y_continuous(labels = scales::comma)
print(p)
ggsave(p,filename=paste("./",i,".png",sep=""),width=6.5,
height=6)
}
以下是数据摘录(如果需要的话):
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
感谢您对这两个“相当小的”问题的帮助。
无论如何都要感谢
nordsee
答案 0 :(得分:1)
您只需要用NaN
替换零值:
df_to_plot <- read.table(text = "
year country iso2 sector emissions
1990 Belgium BE ETS 0
1990 Belgium BE Total 120484398
1990 Belgium BE Regulated 78614107
1990 Belgium BE Unregulated 41870292
1991 Belgium BE ETS 0
1991 Belgium BE Total 123544711
1991 Belgium BE Regulated 79811521
1991 Belgium BE Unregulated 43733190
1992 Belgium BE ETS 0
1992 Belgium BE Total 122657813
1992 Belgium BE Regulated 78283962
1992 Belgium BE Unregulated 44373851
1993 Belgium BE ETS 0
1993 Belgium BE Total 121557281
1993 Belgium BE Regulated 76752290
1993 Belgium BE Unregulated 44804991
1994 Belgium BE ETS 0
1994 Belgium BE Total 124938188
1994 Belgium BE Regulated 80647991
1994 Belgium BE Unregulated 44290197
1995 Belgium BE ETS 0
1995 Belgium BE Total 126082961
1995 Belgium BE Regulated 80518704
1995 Belgium BE Unregulated 45564257
1996 Belgium BE ETS 0
1996 Belgium BE Total 129583625
1996 Belgium BE Regulated 79513349
1996 Belgium BE Unregulated 50070276
1997 Belgium BE ETS 0
1997 Belgium BE Total 124046828
1997 Belgium BE Regulated 77308936
1997 Belgium BE Unregulated 46737892
1998 Belgium BE ETS 0
1998 Belgium BE Total 130285109
1998 Belgium BE Regulated 82610050
1998 Belgium BE Unregulated 47675059
1999 Belgium BE ETS 0
1999 Belgium BE Total 124745703
1999 Belgium BE Regulated 77595053
1999 Belgium BE Unregulated 47150650
2000 Belgium BE ETS 0
2000 Belgium BE Total 126794789
2000 Belgium BE Regulated 80435088
2000 Belgium BE Unregulated 46359701
2001 Belgium BE ETS 0
2001 Belgium BE Total 126129008
2001 Belgium BE Regulated 77255899
2001 Belgium BE Unregulated 48873109
2002 Belgium BE ETS 0
2002 Belgium BE Total 126444625
2002 Belgium BE Regulated 77914653
2002 Belgium BE Unregulated 48529972
2003 Belgium BE ETS 0
2003 Belgium BE Total 127953188
2003 Belgium BE Regulated 78464806
2003 Belgium BE Unregulated 49488382
2004 Belgium BE ETS 0
2004 Belgium BE Total 129040883
2004 Belgium BE Regulated 79430967
2004 Belgium BE Unregulated 49609916
2005 Belgium BE ETS 55363232
2005 Belgium BE Total 125638203
2005 Belgium BE Regulated 77343444
2005 Belgium BE Unregulated 48294759
2006 Belgium BE ETS 54775328
2006 Belgium BE Total 124030891
2006 Belgium BE Regulated 75869846
2006 Belgium BE Unregulated 48161044
2007 Belgium BE ETS 52795332
2007 Belgium BE Total 120611398
2007 Belgium BE Regulated 73189198
2007 Belgium BE Unregulated 47422201
2008 Belgium BE ETS 55462028
2008 Belgium BE Total 120659008
2008 Belgium BE Regulated 71854823
2008 Belgium BE Unregulated 48804185
2009 Belgium BE ETS 46206936
2009 Belgium BE Total 107642367
2009 Belgium BE Regulated 61048912
2009 Belgium BE Unregulated 46593455
2010 Belgium BE ETS 50103980
2010 Belgium BE Total 113582031
2010 Belgium BE Regulated 66648934
2010 Belgium BE Unregulated 46933097
2011 Belgium BE ETS 46203056
2011 Belgium BE Total 104158641
2011 Belgium BE Regulated 61319344
2011 Belgium BE Unregulated 42839297
2012 Belgium BE ETS 43006980
2012 Belgium BE Total 101394977
2012 Belgium BE Regulated 58934979
2012 Belgium BE Unregulated 42459997
2013 Belgium BE ETS 45231176
2013 Belgium BE Total 101970445
2013 Belgium BE Regulated 58383554
2013 Belgium BE Unregulated 43586891
2014 Belgium BE ETS 43853144
2014 Belgium BE Total 96391039
2014 Belgium BE Regulated 56010346
2014 Belgium BE Unregulated 40380694
2015 Belgium BE ETS 44713916
2015 Belgium BE Total 100229492
2015 Belgium BE Regulated 57375031
2015 Belgium BE Unregulated 42854461
2016 Belgium BE ETS 43655728
2016 Belgium BE Total 100243711
2016 Belgium BE Regulated 56702848
2016 Belgium BE Unregulated 43540863
", header = T)
head(df_to_plot)
df_to_plot <- subset(df_to_plot, sector != "Total")
df_to_plot$year <-as.numeric(df_to_plot$year)
names(df_to_plot) <- c("Year", "Country","iso2", "Sector", "Emissions")
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2",
"#D55E00", "#CC79A7")
df_to_plot$Sector <- as.character(df_to_plot$Sector)
df_to_plot$Emissions[df_to_plot$Emissions==0] <- NaN
p <- ggplot(df_to_plot) +
geom_line(aes(x= Year,y= Emissions,group = Sector, color = Sector)) +
labs(x="Year",y="CO2 Emissions",z="",title=paste("Emissions for","Belgium")) +
xlim(1990,2016) +
theme(plot.margin=unit(c(.5,.5,.5,.5),"cm")) +
scale_color_manual(values = cols)+
scale_y_continuous(labels = scales::comma)
print(p)