0

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 and data_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)

enter image description here

Thank you very much for you help!!

Best,

Constantin

Constantin
  • 23
  • 1
  • 4
  • As far as I can see you're not saying anywhere what you're trying to achieve, and the NAs you mention in the title do not seem to present an issue either. – Chris Ruehlemann Dec 24 '18 at 14:45
  • @ChrisRuehlemann Thanks for pointing this out. Plot has been added (same code with use in different dataframe) – Constantin Dec 24 '18 at 14:48
  • What names do the columns in your data have? And, again, even the edit does not make clear what your goal is (well, that can probably be guessed, namely get rid of the error) and what the role of the NAs is. – Chris Ruehlemann Dec 24 '18 at 14:53
  • @ChrisRuehlemann sorry, I hope it is clearer now. All rows with NA values have been removed. Therefore, NAs don't play any role anymore. – Constantin Dec 27 '18 at 10:57
  • Have a look at this: https://stackoverflow.com/questions/25937000/ggplot2-error-discrete-value-supplied-to-continuous-scale?rq=1 – Chris Ruehlemann Dec 27 '18 at 12:19
  • @ChrisRuehlemann Thank you. I have seen this. The confusing part is, that the code used to work. All I have done is to merge to dataframes into one and since then this code isn't working anymore – Constantin Dec 27 '18 at 12:32

1 Answers1

0

I've run the code, after making it reproducible, just for a single country, "C" in my mock data, and it runs smoothly:

# Data:
df <- data.frame(
  country =   c(sample(LETTERS[1:5], 50, replace = T)),
  year = c(sample(2005:2015, 50, replace= T)),
  UNemissions = c(rnorm(50, 10000000, 100)),
  ETSemissions = c(rnorm(50, 10000000, 500))
)

Skipping the for loop I've selected country "C" to be plotted:

# just country C:  
  plot.df <- df[df$country=="C",]
  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

Hence, the problem may be in the forloop! enter image description here

Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
  • Thank you for this. I shall have a further look what causes the error, as the code used to work with another dataframe. – Constantin Dec 28 '18 at 16:15