Nordsee
Nordsee

Reputation: 81

ggplot2 - annotate text with superscript AND function

I'm creating a loop with ggplot2 for a large dataset. I had a look at this topic ggplot2 - annotate text with superscript, but combining this approach with my command round((... does not work out.

here is an excerpt of my dataframe full

   country.x    year     emissions      etsemit
   Austria      2005     16194772.5     16539659
   Austria      2006     15039192.4     15275065
   Austria      2007     13757090.8     14124646
   Austria      2008     13582006.8     14572511
   Austria      2009     12526267.6     12767555
   Austria      2010     13852187.5     15506112
   Austria      2011     13666544.9     15131551
   Austria      2012     12256272.5     13121434
   Austria      2013     11224625.0      8074514
   Austria      2014      9499543.9      6426135
   Austria      2015     10623549.8      7514263
   Austria      2016     10448925.8      7142937
   Austria      2017             NA      7795277
   Belgium      2005     29246990.2     25460856
   Belgium      2006     28136794.9     24099282
   Belgium      2007     27435552.7     23706084
   Belgium      2008     25344134.8     23166180
   Belgium      2009     25744709.0     21185552
   Belgium      2010     26341043.0     22073616
   Belgium      2011     22921875.0     18950876
   Belgium      2012     22809482.4     17463388
   Belgium      2013     21242431.6     16728267
   Belgium      2014     20375966.8     15230243
   Belgium      2015     21091058.6     16053800
   Belgium      2016     19792162.1     15027777
   Belgium      2017             NA     15093036

Here is my code:

ctry <- unique(full$country.x)

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

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

plot.df <- full[full$country.x==ctry[i],]

p <- ggplot() +
geom_line(data=plot.df,aes(x=plot.df$year, y=plot.df$emissions, color='UN 
1.A.1')) +
geom_line(data=plot.df,aes(x=plot.df$year, y=plot.df$etsemit, color='ETS 
20')) +
annotate(geom = 'text', label = round((summary(lm(emissions ~ etsemit, 
data=full))$r.squared) ,3), x = 
Inf, y = Inf, hjust = 1.5, vjust = 2) +
labs(x="Year",y="CO2 Emissions (metric tons)",z="",title=paste("Emissions 
Comparison for",ctry[i])) + 
xlim(2005,2017) +
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("h:/",ctry[i],".png",sep=""),width=6.5, height=6)
}

Everything is running smooth, but I don't manage to add "R^2 =" to the annotate function:

annotate(geom = 'text', label = round((summary(lm(emissions ~ etsemit, 
  data=full))$r.squared) ,3), x = Inf, y = Inf, hjust = 1.5, vjust = 2)

label is doing what I need apart from adding the R^2 = before the value for R^2.

This is how it looks now:

enter image description here

I have tried this:

annotate(geom = 'text', label = bquote("R^2 = "~.(round((summary(lm(emissions ~ etsemit, data=full))$r.squared) ,3))), x = Inf, y = Inf, hjust = 1.5, vjust = 2) but it results in an error

All I need is adding R^2 -> R^2 = round((... and on this specific plot (it is a loop) R^2 = 0.998

Thank you in advance for any kind of help.

Nordsee

Upvotes: 0

Views: 2519

Answers (1)

MrFlick
MrFlick

Reputation: 206167

I guess annotate() doesn't actually accept expressions like base plotting functions, so rather than passing an expression as an expression, you need to pass the string version of it and let ggplot parse it for you. You can do that with

annotate(geom = 'text', 
  label = paste0("R^2==", round(summary(lm(emissions ~ etsemit, data=full))$r.squared, 3)), 
  x = Inf, y = Inf, hjust = 1.5, vjust = 2, parse=TRUE)

Most of the time if you want an expression you would use bquote to make one

bquote(R^2==.( round(summary(lm(emissions ~ etsemit, data=full))$r.squared, 3)))

and you could deparse() that to get the string version but in this case it's easier to go with paste() I suppose.

Upvotes: 4

Related Questions