首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ggplot中添加统计值?

如何在ggplot中添加统计值?
EN

Stack Overflow用户
提问于 2020-11-12 23:17:27
回答 1查看 89关注 0票数 0

对于我的硕士论文,我需要创建5个多点图,每个点包含12个散点图。我需要添加统计值RMSE、MAE和MBE。这些公式。包括:

代码语言:javascript
复制
rmse(sim, obs, na.rm = TRUE)
mae(sim, obs, na.rm = TRUE)
mean((sim - obs), na.rm = TRUE)

我需要在绘图中自动添加这个值。我的代码是:

代码语言:javascript
复制
ggplot(data=Bad_Lauchstaedt, mapping=aes(x= `one_h_gap_L`, y= `BL 2-1`))+
  geom_smooth(method = "lm",se=FALSE, color="red")+
  geom_abline(intercept = 0, slope = 1, color="darkgray", size=1.2)+
  geom_point(color="darkblue", shape=1)+
  labs(y="measured data", x="gap filled data by lysimeters", title = "best fit lysimeters")+
  theme_bw()+
  theme(plot.title = element_text(hjust = 0.5, size = 20))+
  theme(axis.title.x = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank())+
  theme(axis.title.y = element_blank(),axis.text.y = element_text(size = 14), axis.ticks.y = element_blank())+
  xlim(0,0.8)+
  ylim(0,0.8)+
  theme(plot.margin = unit(c(0,0,0,0),"pt"))+
  stat_poly_eq(formula = R_sqr,
               rr.digits = 3,parse = TRUE,
               size=7)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-13 17:44:31

对于我的代码,我找到了这个答案:

代码语言:javascript
复制
label <- df%>% 
  summarize(RMSE = rmse(sim, obs, na.rm = TRUE),
            MAE = mae(sim, obs, na.rm = TRUE),
            MBE = mean( (sim - obs), na.rm = TRUE)) %>%
  mutate(
    posx = 0.5, posy = 0.05,
    label = glue("RMSE = {round(RMSE, 3)} <br> MAE = {round(MAE, 3)} <br> MBE = {round(MBE, 3)} ")) 

p +
  geom_richtext(
    data = label,
    aes(posx, posy, label = label),
    hjust = 0, vjust = 0,
    size = 4,
    fill = "white", label.color = "black")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64806671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档