使用循环创建图[ggplot2]:错误“ data”必须是数据框

时间:2018-11-10 08:52:35

标签: r loops ggplot2

我试图创建一个循环以便为多个国家/地区创建地块。

我的数据框:EUETS_UN

以下是数据摘录:

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

我只显示了一个国家/地区的全部数据,因为每个国家/地区看起来都一样(除了不同的数字EUETS_UN$emissions

我想做的是(大多数您将从下面的代码中看到):

  • 创建循环,因此将为所有国家/地区创建地块
  • x轴=年; y =(EUETS_UN$iso2中一个国家的排放量
  • 3个组(曲线)应来自EUETS_UN$sector:受监管,不受监管,ETS
  • 受管制和不受管制应在1990:2016之间; 2005:2017之间的ETS。这几年是具有数据可用性的年份

这是我尝试过的:

# Sets up the loop to run from i=1 through a list of countries from vector 
'iso2'
for(i in (1:length(EUETS_UN$iso2))){

# Color settings: colorblind-friendly palette
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", 
"#D55E00", "#CC79A7")

# Plotting code where DATA, YEAR, etc need to be handed the right vectors
p <- ggplot() +
geom_line(EUETS_UN$emissions,aes(x=year,y=emissions,group=sector),
color=cols[1]) +
labs(x="Year",y="CO2 emissions",z="",title=paste("TITLE for",iso2[i])) + 
xlim(1990,max(year.aux)) +
ylim(-50,50) +
theme(plot.margin=unit(c(.5,.5,.5,.5),"cm"))
p

# Save plot, where the file name automatically gets a country name suffix
ggsave(p,filename=paste("./FILENAME",iso2[i],".png",sep=""),width=6.5, 
height=6)
}

但是,我得到这个“错误:data必须是数据帧或fortify()可以强制执行的其他对象,而不是类uneval的S3对象 您是否偶然将aes()传递给data参数?”

有什么主意我可以运行循环来创建绘图吗?

无论如何都要感谢!

Nordsee

1 个答案:

答案 0 :(得分:3)

尝试一下:

cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", 
          "#D55E00", "#CC79A7")

iso_2s <- unique(EUETS_UN$iso2)

for(i in iso_2s) {

  # Color settings: colorblind-friendly palette
  cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", 
            "#D55E00", "#CC79A7")

  df_to_plot <- subset(EUETS_UN, iso2 == i)
  df_to_plot <- df_to_plot[df_to_plot$sector != "Total"]
  df_to_plot$sector <- as.character(df_to_plot$sector)

  # Plotting code where DATA, YEAR, etc need to be handed the right vectors
  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("TITLE for",i)) + 
    xlim(1990,max(df_to_plot$year)) +
    #ylim(-50,50) +
    theme(plot.margin=unit(c(.5,.5,.5,.5),"cm")) +
    scale_color_manual(values = cols)

  print(p)

  ggsave(p,filename=paste("./FILENAME",i,".png",sep=""),width=6.5, 
         height=6)

}

这是我从您的测试示例中获得的信息:

enter image description here

您可能还想禁用y轴上的科学计数法。在这种情况下,只需在最后一个ggplot语句之后添加+ scale_y_continuous(labels = scales::comma),即在这种情况下在scale_color_manual(values = cols)之后添加。然后,您将得到:

enter image description here

我还检查了人为添加另一个国家的情况,该循环会根据需要生成2个图表。