【问题标题】:ggplot2 - annotate text with superscript AND functionggplot2 - 使用上标 AND 函数注释文本
【发布时间】:2018-11-28 03:40:33
【问题描述】:

我正在使用 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 =

这就是现在的样子:

我试过这个:

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

提前感谢您的任何帮助。

北湖

【问题讨论】:

  • 您应该使用bquote 构建您想要显示的表达式,以混合您想要评估和未评估的部分。我没有添加完整的答案,因为没有可重复的示例来测试和验证它是否适用于您的情况。
  • 谢谢@MrFlick。我已经尝试过这种方法,但没有奏效。我已经用更多信息扩展了我的初始帖子。

标签: r ggplot2 annotations label


【解决方案1】:

我猜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() 会更容易。

【讨论】:

  • 再次感谢@MrFlick。您的解释和提供的代码有效且有用。非常感谢!
猜你喜欢
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多