mercredi 27 février 2019

How to include IF statement for ignoring NA cases in ggplot2

Hi everyone and thanks for reading my question,

I have tried to find a solution via similar topics, but haven't found anything suitable. This may be due to the search terms I have used. If I have missed something, please accept my apologies.

Here is my data (a bit shortened, but reproduce-able):

country year        sector      UN              ETS
BG      2000        Energy      24076856.07     NA
BG      2001        Energy      27943916.88     NA
BG      2002        Energy      25263464.92     NA
BG      2003        Energy      27154117.22     NA
BG      2004        Energy      26936616.77     NA
BG      2005        Energy      27148080.12     NA
BG      2006        Energy      27444820.45     NA
BG      2007        Energy      30789683.97     31120644
BG      2008        Energy      32319694.49     30453798
BG      2009        Energy      29694118.01     27669012
BG      2010        Energy      31638282.52     29543392
BG      2011        Energy      36421966.96     34669936
BG      2012        Energy      31628708.27     30777290
BG      2013        Energy      27332059.98     27070570
BG      2014        Energy      29036437.07     28583008
BG      2015        Energy      30316871.19     29935784
BG      2016        Energy      27127914.93     26531704
BG      2017        Energy      NA              27966156
CH      2000        Energy      3171899.5       NA
CH      2001        Energy      3313509.6       NA
CH      2002        Energy      3390115.69      NA
CH      2003        Energy      3387122.65      NA
CH      2004        Energy      3682404.04      NA
CH      2005        Energy      3815915.41      NA
CH      2006        Energy      4031766.36      NA
CH      2007        Energy      3718892.16      NA
CH      2008        Energy      3837098.91      NA
CH      2009        Energy      3673731.74      NA
CH      2010        Energy      3846523.62      NA
CH      2011        Energy      3598219.48      NA
CH      2012        Energy      3640743.25      NA
CH      2013        Energy      3735935.29      NA
CH      2014        Energy      3607411.44      NA
CH      2015        Energy      3292576.93      NA
CH      2016        Energy      3380402.57      NA
CY      2000        Energy      2964656.86      NA
CY      2001        Energy      2847105.45      NA
CY      2002        Energy      3008827.44      NA
CY      2003        Energy      3235739.95      NA
CY      2004        Energy      3294769.3       NA
CY      2005        Energy      3483623.91      3471844
CY      2006        Energy      3665461.17      3653380
CY      2007        Energy      3814469.11      3801667
CY      2008        Energy      3980439.76      3967293
CY      2009        Energy      4005649.27      3992467
CY      2010        Energy      3880758.22      3868001
CY      2011        Energy      3722369.39      3728038
CY      2012        Energy      3557560.24      3545929
CY      2013        Energy      2839148.88      2829732
CY      2014        Energy      2950111.64      2940320
CY      2015        Energy      3032961.55      3023003
CY      2016        Energy      3310941.55      3300001
CY      2017        Energy      NA              3287834

The code below is running smoothly and delivers what it should enter image description hereHowever, once the loop reaches a country (here CH) which ONLY has NA values in energy$ETS, the loop will just stop. What I need is to add an IF statement which allows to EITHER ignore the case described and just jumps to the next country (instead of aborting the operation) OR which plots only energy$UN (i.e. it only plots the variable ('UN') which has available data, as energy$ETS ONLY offers NA values).

IMPORTANT: I do not want to exclude all NA values, but I need the loop keep operating if it encounters a country which has no values for energy$ETS

ctry <- unique(energy$country)

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

for(i in (1:length(ctry))){

plot.df <- energy[energy$country==ctry[i],]
ets.initial <- min(plot.df$year)
x <- plot.df$UN[plot.df$year>=ets.initial & plot.df$year<2017]
y <- plot.df$ETS[plot.df$year>=ets.initial & 
plot.df$year<2017]
m1 <- round(summary(lm(y~x))$r.squared,3)
m2 <- round(lm(y~x-1)$coef,3)

p <- ggplot() +
geom_line(data=plot.df,aes(x=plot.df$year, y=plot.df$UN, 
color='UN 1.A.1'), na.rm=TRUE) +
geom_line(data=plot.df,aes(x=plot.df$year, y=plot.df$ETS, 
color='ETS 20')) + annotate(geom='text',label=paste0("R^2==",m1),x=2014,y=Inf,vjust=2,hjust=0,parse=TRUE,cex=3) +
annotate(geom='text',label=paste0("beta==",m2),x=2014,y=Inf,vjust=4,hjust=-0.15,parse=TRUE,cex=3)+
labs(x="Year",y="CO2 Emissions (metric tons)",z="",title=paste("Energy sector emissions for",ctry[i])) + 
theme(plot.margin=unit(c(.5,.5,.5,.5),"cm")) +
scale_color_manual(values = cols) +
scale_y_continuous(labels = scales::comma) +
scale_x_continuous(breaks = seq(2000, 2017, by = 5)) +
labs(color="Datasets")
p ggsave(p,filename=paste("H:/figures_energy/",ctry[i],".png",sep=""),width=6.5, height=6)
}

Thank you very much for any type of help!!

Best,

Constantin

Aucun commentaire:

Enregistrer un commentaire