1

I am trying to automatically position multiple model evaluation parameters in facetted ggplot. This answer has helped me to put the R2 and RMSE in facetted ggplot automatically using the following code

library(caret)
library(tidyverse)

summ <- iris %>% 
  group_by(Species) %>% 
  summarise(Rsq = R2(Sepal.Length, Petal.Length),
            RMSE = RMSE(Sepal.Length, Petal.Length)) %>% 
  mutate_if(is.numeric, round, digits=2)

p <- ggplot(data=iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point(color="blue",alpha = 1/3) + 
  facet_wrap(Species ~ ., scales="free") +
  geom_smooth(method=lm, fill="black", formula = y ~ x) +
  xlab("Sepal Length") +
  ylab("Petal Length") + theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) 

# Here we create our annotations data frame.
df.annotations <- data.frame()

# Rsq
df.annotations <- rbind(df.annotations,
                        cbind(as.character(summ$Species),
                              paste("Rsq", summ$Rsq,
                                    sep = " = ")))

# RMSE
df.annotations <- rbind(df.annotations,
                        cbind(as.character(summ$Species),
                              paste("RMSE", summ$RMSE,
                                    sep = " = ")))

# This here is important, especially naming the first column
# Species
colnames(df.annotations) <- c("Species", "label")


vertical_adjustment = ifelse(grepl("Rsq",df.annotations$label),1.5,3)


p + geom_text(data=df.annotations,aes(x=-Inf,y=+Inf,label=label),
              hjust = -0.1, vjust = vertical_adjustment, size=3.5)

enter image description here I have calculated NSE using hydroGOF package like

library(hydroGOF)

summ <- iris %>% 
  group_by(Species) %>% 
  summarise(Rsq = R2(Sepal.Length, Petal.Length),
            RMSE = RMSE(Sepal.Length, Petal.Length),
            NSE = NSE(Sepal.Length, Petal.Length)) %>% 
  mutate_if(is.numeric, round, digits=2)

Added the NSE to the df.annotations dataframe like

# NSE
df.annotations <- rbind(df.annotations,
                        cbind(as.character(summ$Species),
                              paste("NSE", summ$NSE,
                                    sep = " = ")))

Now, how can I place multiple model evaluation parameters in facetted ggplot2?

UseR10085
  • 7,120
  • 3
  • 24
  • 54

1 Answers1

2

This is the new part using case_when from dplyr package for aligning vertically:

library(dplyr)

vertical_adjustment = case_when(grepl("Rsq",df.annotations$label) ~ 1.5,
                                grepl("RMSE",df.annotations$label) ~ 3,
                                grepl("NSE",df.annotations$label) ~ 4.5)

p + geom_text(data=df.annotations,aes(x=-Inf,y=+Inf,label=label),
              hjust = -0.1, vjust = vertical_adjustment, size=3.5)

enter image description here

The whole code:

library(caret)
library(tidyverse)
library(hydroGOF)

summ <- iris %>% 
  group_by(Species) %>% 
  summarise(Rsq = R2(Sepal.Length, Petal.Length),
            RMSE = RMSE(Sepal.Length, Petal.Length),
            NSE = NSE(Sepal.Length, Petal.Length)) %>% 
  mutate_if(is.numeric, round, digits=2)


p <- ggplot(data=iris, aes(x = Sepal.Length, y = Petal.Length)) +
  geom_point(color="blue",alpha = 1/3) + 
  facet_wrap(Species ~ ., scales="free") +
  geom_smooth(method=lm, fill="black", formula = y ~ x) +
  xlab("Sepal Length") +
  ylab("Petal Length") + theme_bw() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank()) 

p
# Here we create our annotations data frame.
df.annotations <- data.frame()
# Rsq
df.annotations <- rbind(df.annotations,
                        cbind(as.character(summ$Species),
                              paste("Rsq", summ$Rsq,
                                    sep = " = ")))

# RMSE
df.annotations <- rbind(df.annotations,
                        cbind(as.character(summ$Species),
                              paste("RMSE", summ$RMSE,
                                    sep = " = ")))

df.annotations <- rbind(df.annotations,
                        cbind(as.character(summ$Species),
                              paste("NSE", summ$NSE,
                                    sep = " = ")))

# This here is important, especially naming the first column
# Species
colnames(df.annotations) <- c("Species", "label")


library(dplyr)

vertical_adjustment = case_when(grepl("Rsq",df.annotations$label) ~ 1.5,
                                grepl("RMSE",df.annotations$label) ~ 3,
                                grepl("NSE",df.annotations$label) ~ 4.5)

p + geom_text(data=df.annotations,aes(x=-Inf,y=+Inf,label=label),
              hjust = -0.1, vjust = vertical_adjustment, size=3.5)
TarJae
  • 72,363
  • 6
  • 19
  • 66