【问题标题】:ggplot2: add RMSE values of two models to each facetggplot2:将两个模型的 RMSE 值添加到每个方面
【发布时间】:2017-01-15 07:46:53
【问题描述】:

使用这个data.frame

数据

#import_data
df <- read.csv(url("https://www.dropbox.com/s/1fdi26qy4ozs4xq/df_RMSE.csv?raw=1"))

还有这个脚本

library(ggplot2)
ggplot(df, aes( measured, simulated, col = indep_cumulative))+
  geom_point()+
  geom_smooth(method ="lm", se = F)+
  facet_grid(drain~scenario)

我得到了这个情节

我想在每个方面的左上角为两个模型(独立和累积;仅两个值)中的每一个添加RMSE

我试过了

geom_text(data = df , aes(measured, simulated, label= RMSE))

这导致将 RMSE 值添加到构面中的每个点。

如果能将两个 RMSE 值仅添加到每个构面的左上角,我将不胜感激。

【问题讨论】:

    标签: r ggplot2 geom-text


    【解决方案1】:

    如果您想在每个方面绘制两个数字,则需要进行一些数据准备以避免文本重叠。

    library(dplyr)
    df <- df %>%
      mutate(label_vjust=if_else(indep_cumulative == "accumulative",
                                 1, 2.2))
    

    在您的问题中,您明确告诉ggplot2x=measuredy=simulated 处添加label=RMSE。要在左上角添加标签,您可以使用 x=-Infy=Inf。所以代码将如下所示:

    ggplot(df, aes(measured, simulated, colour = indep_cumulative)) +
      geom_point() +
      geom_smooth(method ="lm", se = F) +
      geom_text(aes(x=-Inf, y=Inf, label=RMSE, vjust=label_vjust),
                hjust=0) +
      facet_grid(drain~scenario)
    

    【讨论】:

    • @aelwan ?sprintf / ?paste (如果你真的查了一些东西而不是依赖别人来快速回答,你会在 R 中做得更好)
    • @hrbrmstr 感谢您的建议
    • @aelwan 可能会查找 geom_text() 的其他美学参数,例如文本对齐和轻推
    • @evgeniC 谢谢。我添加了 RMSE = value ton/ha/yr 如下geom_text(aes(x=-Inf, y=Inf, label= paste("RMSE= ", RMSE, "ton/ha/yr"), vjust=label_vjust, hjust=0))
    • @aelwan 不客气。是的,这就是添加自定义标签的方式。
    猜你喜欢
    • 2022-12-21
    • 2014-08-26
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    相关资源
    最近更新 更多