adriancorrendo / metrica

Prediction Performance Metrics
https://adriancorrendo.github.io/metrica/
Other
74 stars 8 forks source link

function to add up to 4 metrics to the obs-pred plot #3

Closed leombastos closed 2 years ago

leombastos commented 2 years ago

create function that allows to add up to 4 metrics to a plot

adriancorrendo commented 2 years ago

Hi, Leo! I think that using coding to do this will allow the user a lot of flexibility regarding the customization of the plots. I just added an specific example into the README.Rmd of the package description that tackles this and seems to work well. See Section 4.

4. Print metrics in a plot

df <- metrica::wheat

# A. Metrics summary
metrics.sum.wheat <- metrica::metrics.summary(data = df, obs = obs, pred = pred)
# Wide format for SMA Equation
metrics.sum.wheat.wide <- metrics.sum.wheat %>% 
  tidyr::pivot_wider(tidyr::everything(), names_from = "Metric", values_from = "Score")

# B. Create list of selected metrics
selected.metrics <- c("MAE","RMSE", "RRMSE", "R2", "CCC", "KGE", "PLA", "PLP")

# C. Create a reduced performance table for ggplot
perf.table <- metrics.sum.wheat %>%
 # Apply a filter to keep only the selected metrics
  dplyr::filter(Metric %in% selected.metrics) %>% 
  # Round numbers for clarity
  mutate_if(is.numeric,~round(.,2))

# D. Create scatter plot with PO orientation
wheat.scat.plot <- 
  metrica::scatter.plot(data = df, 
                        obs = obs, pred = pred,
                        orientation = "PO")

# PLOT, Doesn't work when using facets
wheat.scat.plot + 
  # Annotate Table
  ggpp::annotate(geom="table", 
                 # Position
                 x = min(df$obs), y = 1.05*max(df$pred),
                 # Call the table
                 label = perf.table,
                 # Align Table (left)
                 hjust = 0, vjust = 1)+
  # Add SMA equation
  geom_text(data = metrics.sum.wheat.wide, 
            aes(x=0.75*max(df$obs), 
                y= 1.25*min(df$pred),
                # Equation
                label = paste0("y = ",round(B0,2),"+",round(B1,2),"x"), hjust=0))
leombastos commented 2 years ago

Hi Adrian,

I adapted your code above and created function scatter.metrics(). Currently, this function does not allow for the user to specify which metrics they want, and provides 5 pre-selected metrics (please see code for details). I just opened a PR that includes this function and the vignette number 1.

Feel free to close this issue once you review the PR and integrates it into main.

adriancorrendo commented 2 years ago

Thanks, Leo! Finally, I adapted your code and add it as an optional component at the scatter.plot() and tiles.plot() functions. Check an example out here:

README-unnamed-chunk-3-1

Code

df <- metrica::wheat

B. Create list of selected metrics

selected.metrics <- c("MAE","RMSE", "RRMSE", "R2", "CCC", "KGE", "PLA", "PLP")

metrica::scatter.plot(data = df, obs = obs, pred = pred, print.metrics = TRUE, metrics.list = selected.metrics)

adriancorrendo commented 2 years ago

With that, we can consider this issue as addressed! Thanks for the help!