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.
I'm trying to plot ETSemissions
and UNemissions
over time by country and sector. I have used the same code before (provided below) and now I don't know what the issue could be.
Here is an excerpt of the data (please ignore that the figures are the same for both countries):
country iso2 year sector UNemissions ETSemissions
Austria AT 2005 1 - Energy 16194772.33 16539659
Austria AT 2006 1 - Energy 15039192.77 15275065
Austria AT 2007 1 - Energy 13757091.05 14124646
Austria AT 2008 1 - Energy 13582006.99 14572511
Austria AT 2009 1 - Energy 12526267.29 12767555
Austria AT 2010 1 - Energy 13852187.50 15506112
Austria AT 2011 1 - Energy 13666544.68 15131551
Austria AT 2012 1 - Energy 12256272.25 13121434
Austria AT 2013 1 - Energy 11224625.46 8074514
Austria AT 2014 1 - Energy 9499544.19 6426135
Austria AT 2015 1 - Energy 10623550.19 7514263
Austria AT 2016 1 - Energy 10448925.88 7142937
Austria AT 2017 1 - Energy 9255425.88 7795277
Belgium BE 2005 1 - Energy 16194772.33 16539659
Belgium BE 2006 1 - Energy 15039192.77 15275065
Belgium BE 2007 1 - Energy 13757091.05 14124646
Belgium BE 2008 1 - Energy 13582006.99 14572511
Belgium BE 2009 1 - Energy 12526267.29 12767555
Belgium BE 2010 1 - Energy 13852187.50 15506112
Belgium BE 2011 1 - Energy 13666544.68 15131551
Belgium BE 2012 1 - Energy 12256272.25 13121434
Belgium BE 2013 1 - Energy 11224625.46 8074514
Belgium BE 2014 1 - Energy 9499544.19 6426135
Belgium BE 2015 1 - Energy 10623550.19 7514263
Belgium BE 2016 1 - Energy 10448925.88 7142937
Belgium BE 2017 1 - Energy 9255425.88 7795277
What I have already checked:
- both
data_plot$UNemissions
anddata_plot$ETSemissions
are numeric - No NA values exist
- it is not the color scales
- it is not that
UNemissions
has decimal places
I'm getting Error: Discrete value supplied to continuous scale
straight after labs(color="Datasets")p
in the codeline.
This code used to work (same data), but I had to create a new dataframe with a different design.
ctry <- unique(data_plot$country)
cols <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
for(i in (1:length(ctry))){
plot.df <- data_plot[data_plot$country==ctry[i],]
ets.initial <- min(plot.df$year)
x <- plot.df$UNemissions[plot.df$year>=ets.initial & plot.df$year<2017]
y <- plot.df$ETSemissions[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$UNemissions, color='UN 1.A.1'), na.rm=TRUE) +
geom_line(data=plot.df,aes(x=plot.df$year, y=plot.df$ETSemissions, 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,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(2005, 2017, by = 5)) +
labs(color="Datasets")
p
ggsave(p,filename=paste("./figures_energy/",ctry[i],".png",sep=""),width=6.5, height=6)
}
The result will look like this (different sector of same dataset)
Thank you very much for you help!!
Best,
Constantin