ggplot2-使用上标AND功能注释文本

时间:2018-11-27 16:11:16

标签: r ggplot2 annotations label

我正在使用ggplot2为大型数据集创建一个循环。我看过这个主题ggplot2 - annotate text with superscript,但是将这种方法与我的命令round((...结合起来是行不通的。

这是我的数据帧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

这是我的代码:

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)
}

一切运行顺利,但是我无法在注释函数中添加“ R ^ 2 =“:

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

label正在做我需要做的事情,除了在R ^ 2的值之前添加R^2 =

这是现在的样子:

enter image description here

我已经尝试过了:

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) ,但会导致错误

我需要添加R ^ 2-> R^2 = round((...并在此特定图上(这是一个循环)R^2 = 0.998

在此先感谢您的帮助。

Nordsee

1 个答案:

答案 0 :(得分:3)

我猜annotate()实际上并不接受基本绘图函数之类的表达式,因此,除了传递表达式作为表达式之外,您还需要传递它的字符串版本并让ggplot为您解析它。您可以使用

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)

大多数时候,如果您想要一个表达式,可以使用bquote来制作一个

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

您可以deparse()来获取字符串版本,但是在这种情况下,我想使用paste()会更容易。